Skip to content

Commit

Permalink
cleaning up the events handler
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Pinto <[email protected]>
  • Loading branch information
christian-pinto committed Jun 25, 2024
1 parent 8c08af2 commit cfb2228
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 78 deletions.
82 changes: 37 additions & 45 deletions sunfish/events/redfish_subscription_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import json
import os
import string

from sunfish.events.subscription_handler_interface import SubscriptionHandlerInterface
from sunfish.lib.exceptions import *

Expand All @@ -14,7 +16,7 @@
## Heartbeat events
## IncludeOriginOfCondition

subscribtions = {
subscriptions = {
"RegistryPrefixes": {
# "RegistryPrefix": {
# "to_send": [ ],
Expand Down Expand Up @@ -83,40 +85,40 @@ def new_subscription(self, payload: dict):

if "RegistryPrefixes" in payload:
for prefix in payload["RegistryPrefixes"]:
if prefix in subscribtions["RegistryPrefixes"]:
subscribtions["RegistryPrefixes"][prefix]["to_send"].append(payload["Id"])
if prefix in subscriptions["RegistryPrefixes"]:
subscriptions["RegistryPrefixes"][prefix]["to_send"].append(payload["Id"])
else:
subscribtions["RegistryPrefixes"][prefix] = {
subscriptions["RegistryPrefixes"][prefix] = {
"to_send": [payload["Id"]],
"exclude": []
}

if "ExcludeRegistryPrefixes" in payload:
for exclude_prefix in payload["ExcludeRegistryPrefixes"]:
if exclude_prefix in subscribtions["RegistryPrefixes"]:
subscribtions["RegistryPrefixes"][exclude_prefix]["exclude"].append(payload["Id"])
if exclude_prefix in subscriptions["RegistryPrefixes"]:
subscriptions["RegistryPrefixes"][exclude_prefix]["exclude"].append(payload["Id"])
else:
subscribtions["RegistryPrefixes"][exclude_prefix] = {
subscriptions["RegistryPrefixes"][exclude_prefix] = {
"to_send": [],
"exclude": [payload["Id"]]
}

if "MessageIds" in payload:
for msgId in payload["MessageIds"]:
if msgId in subscribtions["MessageIds"]:
subscribtions["MessageIds"][msgId]["to_send"].append(payload["Id"])
if msgId in subscriptions["MessageIds"]:
subscriptions["MessageIds"][msgId]["to_send"].append(payload["Id"])
else:
subscribtions["MessageIds"][msgId] = {
subscriptions["MessageIds"][msgId] = {
"to_send": [payload["Id"]],
"exclude": []
}

if "ExcludeMessageIds" in payload:
for exclude_msgId in payload["ExcludeMessageIds"]:
if exclude_msgId in subscribtions["MessageIds"]:
subscribtions["MessageIds"][exclude_msgId]["exclude"].append(payload["Id"])
if exclude_msgId in subscriptions["MessageIds"]:
subscriptions["MessageIds"][exclude_msgId]["exclude"].append(payload["Id"])
else:
subscribtions["MessageIds"][exclude_msgId] = {
subscriptions["MessageIds"][exclude_msgId] = {
"to_send": [],
"exclude": [payload["Id"]]
}
Expand All @@ -126,17 +128,17 @@ def new_subscription(self, payload: dict):
origin = prefix["@odata.id"]
if "SubordinateResources" in payload and payload["SubordinateResources"]:
origin = os.path.join(origin, '*')
if origin in subscribtions["OriginResources"]:
subscribtions["OriginResources"][origin].append(payload["Id"])
if origin in subscriptions["OriginResources"]:
subscriptions["OriginResources"][origin].append(payload["Id"])
else:
subscribtions["OriginResources"][origin] = [payload["Id"]]
subscriptions["OriginResources"][origin] = [payload["Id"]]

if "ResourceTypes" in payload:
for type in payload["ResourceTypes"]:
if type in subscribtions["ResourceTypes"]:
subscribtions["ResourceTypes"][type].append(payload["Id"])
if type in subscriptions["ResourceTypes"]:
subscriptions["ResourceTypes"][type].append(payload["Id"])
else:
subscribtions["ResourceTypes"][type] = [payload["Id"]]
subscriptions["ResourceTypes"][type] = [payload["Id"]]
return

def validate_subscription(self, payload: dict):
Expand Down Expand Up @@ -168,30 +170,20 @@ def validate_subscription(self, payload: dict):

# Deletes from the subscriptions data structure the ID of the subs deleted
def delete_subscription(self, id):
for prefix in subscribtions["OriginResources"]:
list = []
list = subscribtions["OriginResources"][prefix]
if id in list:
subscribtions["OriginResources"][prefix].remove(id)
for prefix in subscribtions["ResourceTypes"]:
list = []
list = subscribtions["ResourceTypes"][prefix]
if id in list:
subscribtions["ResourceTypes"][prefix].remove(id)
for prefix in subscribtions["RegistryPrefixes"]:
list = []
list = subscribtions["RegistryPrefixes"][prefix]["to_send"]
if id in list:
subscribtions["RegistryPrefixes"][prefix]["to_send"].remove(id)
list = subscribtions["RegistryPrefixes"][prefix]["exclude"]
if id in list:
subscribtions["RegistryPrefixes"][prefix]["exclude"].remove(id)
for prefix in subscribtions["MessageIds"]:
list = []
list = subscribtions["MessageIds"][prefix]["to_send"]
if id in list:
subscribtions["MessageIds"][prefix]["to_send"].remove(id)
list = subscribtions["MessageIds"][prefix]["exclude"]
if id in list:
subscribtions["MessageIds"][prefix]["exclude"].remove(id)
for prefix in subscriptions["OriginResources"]:
if id in subscriptions["OriginResources"][prefix]:
subscriptions["OriginResources"][prefix].remove(id)
for prefix in subscriptions["ResourceTypes"]:
if id in subscriptions["ResourceTypes"][prefix]:
subscriptions["ResourceTypes"][prefix].remove(id)
for prefix in subscriptions["RegistryPrefixes"]:
if id in subscriptions["RegistryPrefixes"][prefix]["to_send"]:
subscriptions["RegistryPrefixes"][prefix]["to_send"].remove(id)
if id in subscriptions["RegistryPrefixes"][prefix]["exclude"]:
subscriptions["RegistryPrefixes"][prefix]["exclude"].remove(id)
for prefix in subscriptions["MessageIds"]:
if id in subscriptions["MessageIds"][prefix]["to_send"]:
subscriptions["MessageIds"][prefix]["to_send"].remove(id)
if id in subscriptions["MessageIds"][prefix]["exclude"]:
subscriptions["MessageIds"][prefix]["exclude"].remove(id)
return
9 changes: 5 additions & 4 deletions sunfish/events/subscription_handler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

from abc import abstractmethod


class SubscriptionHandlerInterface():
@abstractmethod
def load_subscriptions():
def load_subscriptions(self):
pass
@abstractmethod
def new_subscription():
def new_subscription(self, payload: dict):
pass

@abstractmethod
def validate_subscription():
def validate_subscription(self, payload: dict):
pass

@abstractmethod
def delete_subscription():
def delete_subscription(self, id):
pass
45 changes: 16 additions & 29 deletions sunfish_plugins/event_handlers/redfish/redfish_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

logger = logging.getLogger("RedfishEventHandler")
Expand Down Expand Up @@ -136,11 +136,11 @@ def new_event(self, payload):
to_forward = []
to_exclude = []

if prefix in subscribtions["RegistryPrefixes"]:
for id in subscribtions["RegistryPrefixes"][prefix]["exclude"]:
if prefix in subscriptions["RegistryPrefixes"]:
for id in subscriptions["RegistryPrefixes"][prefix]["exclude"]:
to_exclude.extend(id)
if messageId in subscribtions["MessageIds"]:
for id in subscribtions["MessageIds"][messageId]["exclude"]:
if messageId in subscriptions["MessageIds"]:
for id in subscriptions["MessageIds"][messageId]["exclude"]:
to_exclude.append(id)

""" ResourceTypes, OriginResources and SubordinateResources are checked only if the event
Expand All @@ -152,37 +152,24 @@ def new_event(self, payload):
type = self.check_data_type(origin)
except ResourceNotFound as e:
raise ResourceNotFound(e.resource_id)
if type in subscribtions["ResourceTypes"]:
to_forward.extend(subscribtions["ResourceTypes"][type])
if origin in subscribtions["OriginResources"]:
to_forward.extend(subscribtions["OriginResources"][origin])
if type in subscriptions["ResourceTypes"]:
to_forward.extend(subscriptions["ResourceTypes"][type])
if origin in subscriptions["OriginResources"]:
to_forward.extend(subscriptions["OriginResources"][origin])
sub = self.check_subdirs(origin)
to_forward.extend(sub)

if prefix in subscribtions["RegistryPrefixes"]:
for id in subscribtions["RegistryPrefixes"][prefix]["to_send"]:
if prefix in subscriptions["RegistryPrefixes"]:
for id in subscriptions["RegistryPrefixes"][prefix]["to_send"]:
to_forward.append(id)
if messageId in subscribtions["MessageIds"]:
for id in subscribtions["MessageIds"][messageId]["to_send"]:
if messageId in subscriptions["MessageIds"]:
for id in subscriptions["MessageIds"][messageId]["to_send"]:
to_forward.append(id)

set1 = set(to_forward)
set2 = set(to_exclude)
to_forward = list(set1 - set2)

#MemberId
# if prefix in subscribtions["RegistryPrefixes"]:
# for id in subscribtions["RegistryPrefixes"][prefix]["to_send"]:
# if messageId not in subscribtions["MessageIds"] or messageId in subscribtions["MessageIds"] and not id in subscribtions["MessageIds"][messageId]["exclude"]:
# to_forward.append(id)
# if prefix in subscribtions["MessageIds"]:
# for id in subscribtions["MessageIds"][payload["MessageId"]]:
# for x in subscribtions["RegistryPrefixes"][prefix]:
# if x not in subscribtions["RegistryPrefixes"][prefix]["exclude"]:
# to_forward.append(id)

## parameter of forward_event is a set with no duplicates
# return self.forward_event(list(set(to_forward)), payload)

return self.forward_event(to_forward, payload)

def check_data_type(self, origin):
Expand Down Expand Up @@ -229,13 +216,13 @@ def forward_event(self, list, payload):
return list

def check_subdirs(self, origin):
keylist = list(subscribtions["OriginResources"].keys())
keylist = list(subscriptions["OriginResources"].keys())
to_forward = []
for el in keylist:
if '/*' in el:
base_origin = el.replace("/*", "")
if base_origin in origin:
for id in subscribtions["OriginResources"][el]:
for id in subscriptions["OriginResources"][el]:
to_forward.append(id)

return to_forward
Expand Down

0 comments on commit cfb2228

Please sign in to comment.