Douglas Henrique Koerich b6457447e6 Allow subscription to all nodes with '*'
The O-RAN standard was not being followed when resource address was
being specified with '*' as the "nodeName", which means that
subscription should be created to be notified about all nodes.

Test Plan:
PASS: Submitting POST request with '*' wildcard in resource address;
PASS: Rejecting POST for specific node when there is a subscription for
      '*' in repo;
PASS: Rejecting POST for current node ('.') when there is a subscription
      for '*' in repo.

Closes-Bug: #1996929
Signed-off-by: Douglas Henrique Koerich <douglashenrique.koerich@windriver.com>
Change-Id: Ifdaa2d2b9c9a45ad41981d2a75f7101bfa2f7c9c
2022-11-21 15:19:38 -03:00

83 lines
2.6 KiB
Python

#
# Copyright (c) 2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import logging
from notificationclientsdk.repository.node_repo import NodeRepo
from notificationclientsdk.common.helpers import constants
from notificationclientsdk.common.helpers import log_helper
LOG = logging.getLogger(__name__)
log_helper.config_logger(LOG)
class NodeInfoHelper(object):
residing_node_name = None
@staticmethod
def set_residing_node(residing_node_name):
NodeInfoHelper.residing_node_name = residing_node_name
@staticmethod
def get_residing_node():
residing_node_name = NodeInfoHelper.residing_node_name
return residing_node_name
@staticmethod
def expand_node_name(node_name_pattern):
if node_name_pattern == constants.WILDCARD_CURRENT_NODE:
return NodeInfoHelper.residing_node_name
else:
return node_name_pattern
@staticmethod
def default_node_name(node_name_pattern):
if node_name_pattern == constants.WILDCARD_CURRENT_NODE:
return NodeInfoHelper.residing_node_name
else:
return node_name_pattern
@staticmethod
def match_node_name(node_name_pattern, target_node_name):
if node_name_pattern == constants.WILDCARD_ALL_NODES:
return True
elif node_name_pattern == constants.WILDCARD_CURRENT_NODE:
return NodeInfoHelper.residing_node_name == target_node_name
else:
return node_name_pattern == target_node_name
@staticmethod
def enumerate_nodes(node_name_pattern):
'''
enumerate nodes from node repo by pattern
'''
nodeinfos = []
if not node_name_pattern:
raise ValueError("node name pattern is invalid")
nodeinfo_repo = None
try:
nodeinfo_repo = NodeRepo(autocommit=True)
filter = {}
if node_name_pattern == constants.WILDCARD_ALL_NODES:
pass
elif not node_name_pattern or \
node_name_pattern == constants.WILDCARD_CURRENT_NODE:
filter = {'NodeName': NodeInfoHelper.residing_node_name}
else:
filter = {'NodeName': node_name_pattern}
nodeinfos = [x.NodeName
for x in nodeinfo_repo.get(Status=1, **filter)]
except Exception as ex:
LOG.warning("Failed to enumerate nodes:{0}".format(str(ex)))
nodeinfos = None
finally:
if nodeinfo_repo:
del nodeinfo_repo
return nodeinfos