Skip to content

Commit

Permalink
added the clear function to the events
Browse files Browse the repository at this point in the history
  • Loading branch information
rherrell committed Jul 11, 2024
1 parent c4b61f8 commit fadb467
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
44 changes: 40 additions & 4 deletions sunfish_plugins/events_handlers/redfish/redfish_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import logging
import os
import warnings
import shutil
from uuid import uuid4


import requests
from sunfish.events.event_handler_interface import EventHandlerInterface
from sunfish.events.redfish_subscription_handler import subscriptions
from sunfish.lib.exceptions import *

logger = logging.getLogger("RedfishEventHandler")
logging.basicConfig(level=logging.DEBUG)


class RedfishEventHandlersTable:
Expand All @@ -21,8 +25,8 @@ def AggregationSourceDiscovered(cls, event_handler: EventHandlerInterface, event
# 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
# The arguments of the event message are:
# - Arg1: "Redfish"
# - Arg2: "agent_ip:port"
# - Arg0: "Redfish"
# - Arg1: "agent_ip:port"
# I am also assuming that the agent name to be used is contained in the OriginOfCondifiton field of the event as in the below example:
# {
# "OriginOfCondition: [
Expand Down Expand Up @@ -98,10 +102,36 @@ def ResourceCreated(cls, event_handler: EventHandlerInterface, event: dict, cont
event_handler.core.storage_backend.patch(id, aggregation_source)


@classmethod
def ClearResources(cls, event_handler: EventHandlerInterface, event: dict, context: str):
###
# Receipt of this event will cause the core library to remove the entire Resources tree, and reload a clean initial tree
# This will happen upon the Core receiving an event with MessageId: ClearResources
# The arguments of the event message are:
# - Arg0: "<relative path_to_clean_resource_directory>
# there is no protection on the receipt of this event
# This event will not work if the backend file system is not the host's filesystem!
#
logger.info("ClearResources method called")
resource_path = event['MessageArgs'][0] # relative Resource Path
logger.info(f"ClearResources path is {resource_path}")
try:
if os.path.exists('Resources'):
shutil.rmtree('Resources')

shutil.copytree(resource_path, 'Resources')
resp = 204
except Exception:
raise Exception("ClearResources Failed")
resp = 500
return resp


class RedfishEventHandler(EventHandlerInterface):
dispatch_table = {
"AggregationSourceDiscovered": RedfishEventHandlersTable.AggregationSourceDiscovered,
"ResourceCreated": RedfishEventHandlersTable.ResourceCreated
"ResourceCreated": RedfishEventHandlersTable.ResourceCreated,
"ClearResources" : RedfishEventHandlersTable.ClearResources
}

def __init__(self, core):
Expand Down Expand Up @@ -339,11 +369,13 @@ def createInspectedObject(self,redfish_obj, aggregation_source):
logger.debug("This is a collection")

def add_aggregation_source_reference(redfish_obj, aggregation_source):
# BoundaryComponent = ["true", "false", "unknown"]
oem = {
"@odata.type": "#SunfishExtensions.v1_0_0.ResourceExtensions",
"ManagingAgent": {
"@odata.id": aggregation_source["@odata.id"]
}
},
"BoundaryComponent": "unknown"
}
if "Oem" not in redfish_obj:
redfish_obj["Oem"] = {"Sunfish_RM": oem}
Expand All @@ -362,6 +394,10 @@ def add_aggregation_source_reference(redfish_obj, aggregation_source):
logger.warning(f"""The object {redfish_obj["@odata.id"]} returned while registering agent {aggregation_source["@odata.id"]} contains already a managing agent ({redfish_obj['Oem']['Sunfish_RM']['ManagingAgent']['@odata.id']})
and this should not be happening""")

# the expected case is there is no ManagingAgent before this event handler creates the object, for now even if the Agent has
# set this value, we will over write.
redfish_obj["Oem"]["Sunfish_RM"]["ManagingAgent"] = {
"@odata.id": aggregation_source["@odata.id"]
}
if "BoundaryComponent" not in redfish_obj["Oem"]["Sunfish_RM"]:
redfish_obj["Oem"]["Sunfish_RM"]["BoundaryComponent"] = oem["BoundaryComponent"]
2 changes: 1 addition & 1 deletion sunfish_plugins/storage/file_system_backend/backend_FS.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def write(self, payload: dict):

config = utils.generate_collection(collection_type)

# if the item to be written is managed by an agent, we want the collection containing it to also be maked
# if the item to be written is managed by an agent, we want the collection containing it to also be marked
# accordingly. We do this only for collections to be created because we assume that if the collection is
# there already:
# a. The collection is a first level one that is managed by Sunfish
Expand Down

0 comments on commit fadb467

Please sign in to comment.