From 6030017ddd994a73178d709fdd88fb30228414a1 Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 2 Feb 2024 07:20:05 +0000 Subject: [PATCH 1/6] Fix typo. Signed-off-by: MICHELE GAZZETTI --- sunfish/events/redfish_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sunfish/events/redfish_event_handler.py b/sunfish/events/redfish_event_handler.py index ba29798..f3cc264 100644 --- a/sunfish/events/redfish_event_handler.py +++ b/sunfish/events/redfish_event_handler.py @@ -25,7 +25,7 @@ def __init__(self, core): self.backend = core.storage_backend def new_event(self, payload): - """Compares event's informations with the subsribtions data structure to find the Ids of the subscribers for that event. + """Compares event's information with the subsribtions data structure to find the Ids of the subscribers for that event. Args: payload (dict): event received. From e7809b59388946c5f69cb3ed2a75804c805f90b1 Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 2 Feb 2024 07:21:45 +0000 Subject: [PATCH 2/6] Use the Context property in Event objects to identify the AggregationSource that sent the message Signed-off-by: MICHELE GAZZETTI --- sunfish/events/redfish_event_handler.py | 77 ++++++++----------- .../events/redfish_subscription_handler.py | 2 +- sunfish/lib/core.py | 6 +- 3 files changed, 36 insertions(+), 49 deletions(-) diff --git a/sunfish/events/redfish_event_handler.py b/sunfish/events/redfish_event_handler.py index f3cc264..c49a987 100644 --- a/sunfish/events/redfish_event_handler.py +++ b/sunfish/events/redfish_event_handler.py @@ -139,7 +139,7 @@ def check_subdirs(self, origin): return to_forward - def AggregationSourceDiscovered(self, event): + def AggregationSourceDiscovered(self, event,context): ### # Fabric Agents are modelled as AggregationSource objects (RedFish v2023.1 at the time of writing this comment) # Registration will happen with the OFMF receiving a and event with MessageId: AggregationSourceDiscovered @@ -179,56 +179,35 @@ def AggregationSourceDiscovered(self, event): } } - try: - resp_post = self.create_object(os.path.join(self.conf["redfish_root"], "AggregationService/AggregationSources"), connection_method_template) - except Exception: - raise Exception() - - # Update Name and ResourcesAccessed - path = hostname + os.path.join(self.conf["redfish_root"], "Fabrics") - response = requests.get(path) - if response.status_code != 200: - raise Exception("Cannot find Fabrics") - response = response.json() + resp_post = self.create_object(os.path.join(self.conf["redfish_root"], "AggregationService/AggregationSources"), connection_method_template) - update_template = { - "@odata.type": "#AggregationSource.v1_2_"+resp_post["Id"]+".AggregationSource", - "@odata.id": resp_post["@odata.id"], - "Name": "Agent "+resp_post["Id"], - "Links": { - "ConnectionMethod": { - "@odata.id": connectionMethodId - }, - "ResourcesAccessed": [ - member['@odata.id'] for member in response['Members'] - ] - } - } + aggregation_source_id = resp_post['@odata.id'] + agent_subscription_context = {"Context": aggregation_source_id.split('/')[-1]} + + resp_patch = requests.patch(f"{hostname}/redfish/v1/EventService/Subscriptions/SunfishServer", + json=agent_subscription_context) - try: - resp_patch = self.patch_object(update_template) - except Exception: - raise Exception - return resp_patch - def ResourceCreated(self, event): + def ResourceCreated(self, event, context): logging.info("New resource created") id = event['OriginOfCondition']['@odata.id'] # /redfish/v1/Fabrics/CXL - aggregation_source = RedfishEventHandler.get_aggregation_source(self, id) + aggregation_source = self.get_object( + os.path.join(self.conf["redfish_root"], "AggregationService", "AggregationSources", context)) hostname = aggregation_source["HostName"] response = requests.get(f"{hostname}/{id}") if response.status_code != 200: raise Exception("Cannot find ConnectionMethod") - response = response.json() + object = response.json() + add_aggregation_source_reference(object,aggregation_source) - self.create_object(id, response) + self.create_object(id, object) - RedfishEventHandler.bfsInspection(self, response, aggregation_source) + RedfishEventHandler.bfsInspection(self, object, aggregation_source) - self.patch_object(aggregation_source) + self.patch_object(id,aggregation_source) def bfsInspection(self, node, aggregation_source): queue = [] @@ -335,14 +314,18 @@ def createInspectedObject(self,redfish_obj, aggregation_source): elif self.get_object(file_path) != redfish_obj: warnings.warn('Resource state changed') except ResourceNotFound: - oem = { - "@odata.type": "#SunfishExtensions.v1_0_0.ResourceExtensions", - "ManagingAgent": { - "@odata.id": aggregation_source["@odata.id"] - } - } - if "Oem" not in redfish_obj: - redfish_obj["Oem"] = {} - if "Sunfish_RM" not in redfish_obj["Oem"]: - redfish_obj["Oem"]["Sunfish_RM"] = oem - self.create_object(file_path, redfish_obj) \ No newline at end of file + add_aggregation_source_reference(redfish_obj,aggregation_source) + self.create_object(file_path, redfish_obj) + + +def add_aggregation_source_reference(redfish_obj,aggregation_source): + if "Oem" not in redfish_obj: + redfish_obj["Oem"] = {} + if "Sunfish_RM" not in redfish_obj["Oem"]: + oem = { + "@odata.type": "#SunfishExtensions.v1_0_0.ResourceExtensions", + "ManagingAgent": { + "@odata.id": aggregation_source["@odata.id"] + } + } + redfish_obj["Oem"]["Sunfish_RM"] = oem diff --git a/sunfish/events/redfish_subscription_handler.py b/sunfish/events/redfish_subscription_handler.py index adfd59c..4dac7ab 100644 --- a/sunfish/events/redfish_subscription_handler.py +++ b/sunfish/events/redfish_subscription_handler.py @@ -122,7 +122,7 @@ def new_subscription(self, payload:dict): if "OriginResources" in payload: for prefix in payload["OriginResources"]: - origin = payload["OriginResources"][prefix] + origin = prefix["@odata.id"] if "SubordinateResources" in payload and payload["SubordinateResources"]: origin = os.path.join(origin, '*') if origin in subscribtions["OriginResources"]: diff --git a/sunfish/lib/core.py b/sunfish/lib/core.py index b980044..cd2805a 100644 --- a/sunfish/lib/core.py +++ b/sunfish/lib/core.py @@ -156,10 +156,14 @@ def delete_object(self, path): return self.storage_backend.remove(path) def handle_event(self, payload): + if "Context" in payload: + context = payload["Context"] + else: + context = "" for event in payload["Events"]: try: handlerfunc = getattr(RedfishEventHandler, event['MessageId'].split(".")[-1]) - handlerfunc(self, event) + handlerfunc(self, event,context) except AttributeError: pass return self.event_handler.new_event(payload) From ffb6f8599c11b6e58e69319a45c9cb8bddf3bf5b Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 2 Feb 2024 07:22:39 +0000 Subject: [PATCH 3/6] Update tests Signed-off-by: MICHELE GAZZETTI --- tests/test_sunfishcore_library.py | 2 +- tests/tests_template.py | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/test_sunfishcore_library.py b/tests/test_sunfishcore_library.py index 171c9db..9fa43a5 100644 --- a/tests/test_sunfishcore_library.py +++ b/tests/test_sunfishcore_library.py @@ -83,7 +83,7 @@ def test_put(self): # Exception put element that doesnt exists def test_put_exception(self): payload = tests_template.test_update_exception - with pytest.raises(ResourceNotFound): + with pytest.raises(PropertyNotFound): self.core.replace_object(payload) # Patch diff --git a/tests/tests_template.py b/tests/tests_template.py index 9abb5aa..a748bc4 100644 --- a/tests/tests_template.py +++ b/tests/tests_template.py @@ -177,13 +177,6 @@ test_update_exception = { "@odata.id": "/redfish/v1/Systems/-1", - "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", - "Id": "1234", - "Name": "Compute Node 1234", - "SystemType": "Physical", - "Manufacturer": "Manufacturer Name", - "Model": "Model Name", - "SKU": "", "Memory": { "TotalSystemMemoryGB": 12, "Status": { @@ -240,9 +233,9 @@ "ResourceTypes": [ "ComputerSystem" ], - "OriginResources": { + "OriginResources": [{ "@odata.id": "/redfish/v1/Systems/1" - }, + }], "SubordinateResources": "True" } From 3d5bbb64e72775e7601b2c864c1f1f4af5baabd7 Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 2 Feb 2024 07:24:52 +0000 Subject: [PATCH 4/6] Add Makefile to simplify the building, testing procedures. Signed-off-by: MICHELE GAZZETTI --- Makefile | 16 ++++++++++++++++ README.md | 20 ++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cb982ae --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +# Copyright IBM Corp. 2023 +# This software is available to you under a BSD 3-Clause License. +# The full license terms are available here: https://github.com/OpenFabrics/sunfish_library_reference/blob/main/LICENSE + +all: build + +build: + poetry build + +test: + python3 -m pytest tests/test_sunfishcore_library.py -vvvv + +clean: + rm -r dist + + diff --git a/README.md b/README.md index 1ee424f..9c6d26d 100644 --- a/README.md +++ b/README.md @@ -8,33 +8,29 @@ The library requires: - Python (version>=3.9) - Poetry -## To generate the installation file -From the directory ```sunfish_library_reference``` execute the following command: -``` -poetry build #This command will create the .whl file - -``` - ## Installation -We suggest using a Python virtual environment. +We suggest using a Python virtual environment. To install the project requirements: ``` pip install -r requirements.txt ``` -To install sunfish you need to use the file .whl: -``` -pip3 install dist/sunfish-0.1.0-py3-none-any.whl +## To generate the installation file +From the directory ```sunfish_library_reference``` execute the following command: ``` +make build +``` + ## Tests To test this Library you need ```pytest``` to be installed. To run the tests run the command ``` -python3 -m pytest test_sunfishcore_library.py -vvvv +make test ``` + ## Usage To use sunfishcorelib you need to specify the **configuration parameters** into the conf.json file, an example could be: ``` From fc3c6d61035d360216ee2d6448e425864598fec3 Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 23 Feb 2024 11:09:11 +0000 Subject: [PATCH 5/6] fix styling issues Signed-off-by: MICHELE GAZZETTI --- sunfish/events/redfish_event_handler.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sunfish/events/redfish_event_handler.py b/sunfish/events/redfish_event_handler.py index c49a987..26338ed 100644 --- a/sunfish/events/redfish_event_handler.py +++ b/sunfish/events/redfish_event_handler.py @@ -139,7 +139,7 @@ def check_subdirs(self, origin): return to_forward - def AggregationSourceDiscovered(self, event,context): + def AggregationSourceDiscovered(self, event, context): ### # Fabric Agents are modelled as AggregationSource objects (RedFish v2023.1 at the time of writing this comment) # Registration will happen with the OFMF receiving a and event with MessageId: AggregationSourceDiscovered @@ -201,13 +201,13 @@ def ResourceCreated(self, event, context): if response.status_code != 200: raise Exception("Cannot find ConnectionMethod") object = response.json() - add_aggregation_source_reference(object,aggregation_source) + add_aggregation_source_reference(object, aggregation_source) self.create_object(id, object) RedfishEventHandler.bfsInspection(self, object, aggregation_source) - self.patch_object(id,aggregation_source) + self.patch_object(id, aggregation_source) def bfsInspection(self, node, aggregation_source): queue = [] @@ -314,11 +314,11 @@ def createInspectedObject(self,redfish_obj, aggregation_source): elif self.get_object(file_path) != redfish_obj: warnings.warn('Resource state changed') except ResourceNotFound: - add_aggregation_source_reference(redfish_obj,aggregation_source) + add_aggregation_source_reference(redfish_obj, aggregation_source) self.create_object(file_path, redfish_obj) -def add_aggregation_source_reference(redfish_obj,aggregation_source): +def add_aggregation_source_reference(redfish_obj, aggregation_source): if "Oem" not in redfish_obj: redfish_obj["Oem"] = {} if "Sunfish_RM" not in redfish_obj["Oem"]: From 76ce2f5e1828ade356b95fda73d6ab262eba007f Mon Sep 17 00:00:00 2001 From: MICHELE GAZZETTI Date: Fri, 23 Feb 2024 11:18:04 +0000 Subject: [PATCH 6/6] fix styling issues Signed-off-by: MICHELE GAZZETTI --- sunfish/lib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sunfish/lib/core.py b/sunfish/lib/core.py index cd2805a..a5c0203 100644 --- a/sunfish/lib/core.py +++ b/sunfish/lib/core.py @@ -163,7 +163,7 @@ def handle_event(self, payload): for event in payload["Events"]: try: handlerfunc = getattr(RedfishEventHandler, event['MessageId'].split(".")[-1]) - handlerfunc(self, event,context) + handlerfunc(self, event, context) except AttributeError: pass return self.event_handler.new_event(payload)