X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fservice%2Fcommand%2Fnotify_handler.py;h=947942fc86e5532be8cf2404e5d43be271c44767;hb=af290d19532a5595ed22d56d0006ec486390f8de;hp=08da45ded9fc44bff865f28c1634f45e77175e12;hpb=209906139ba3d55342e5e95dd7cdb804c7b61943;p=pti%2Fo2.git diff --git a/o2ims/service/command/notify_handler.py b/o2ims/service/command/notify_handler.py index 08da45d..947942f 100644 --- a/o2ims/service/command/notify_handler.py +++ b/o2ims/service/command/notify_handler.py @@ -45,18 +45,16 @@ def notify_change_to_smo( logger.debug('In notify_change_to_smo') msg_type = cmd.type if msg_type == 'ResourceType': - _notify_resourcetype(uow, cmd) + _notify_resourcetype(uow, cmd.data) elif msg_type == 'ResourcePool': - _notify_resourcepool(uow, cmd) + _notify_resourcepool(uow, cmd.data) elif msg_type == 'Dms': - _notify_dms(uow, cmd) + _notify_dms(uow, cmd.data) elif msg_type == 'Resource': - _notify_resource(uow, cmd) + _notify_resource(uow, cmd.data) -def _notify_resourcetype(uow, cmd): - data = cmd.data - msg_type = cmd.type +def _notify_resourcetype(uow, data): with uow: resource_type = uow.resource_types.get(data.id) if resource_type is None: @@ -76,32 +74,39 @@ def _notify_resourcetype(uow, cmd): for sub in subs: sub_data = sub.serialize() logger.debug('Subscription: {}'.format(sub_data['subscriptionId'])) - if not sub_data.get('filter', None): - callback_smo(sub, data, msg_type, resource_type_dict) - continue - try: - args = gen_orm_filter(ocloud.ResourceType, sub_data['filter']) - except KeyError: - logger.warning( - 'Subscription {} filter {} has wrong attribute name ' - 'or value. Ignore the filter.'.format( - sub_data['subscriptionId'], sub_data['filter'])) - callback_smo(sub, data, msg_type, resource_type_dict) + filters = handle_filter(sub_data['filter'], 'ResourceTypeInfo') + if not filters: + callback_smo(sub, data, resource_type_dict) continue - args.append(ocloud.ResourceType.resourceTypeId == data.id) - ret = uow.resource_types.list_with_count(*args) - if ret[0] != 0: - logger.debug( - 'ResourceType {} skip for subscription {} because of the ' - 'filter.' - .format(data.id, sub_data['subscriptionId'])) + filter_effect = 0 + for filter in filters: + try: + args = gen_orm_filter(ocloud.ResourceType, filter) + except KeyError: + logger.warning( + 'Subscription {} filter {} has wrong attribute ' + 'name or value. Ignore the filter.'.format( + sub_data['subscriptionId'], + sub_data['filter'])) + continue + if len(args) == 0 and 'objectType' in filter: + filter_effect += 1 + break + args.append(ocloud.ResourceType.resourceTypeId == data.id) + ret = uow.resource_types.list_with_count(*args) + if ret[0] > 0: + logger.debug( + 'ResourcePool {} skip for subscription {} because of' + ' the filter.' + .format(data.id, sub_data['subscriptionId'])) + filter_effect += 1 + break + if filter_effect > 0: continue - callback_smo(sub, data, msg_type, resource_type_dict) + callback_smo(sub, data, resource_type_dict) -def _notify_resourcepool(uow, cmd): - data = cmd.data - msg_type = cmd.type +def _notify_resourcepool(uow, data): with uow: resource_pool = uow.resource_pools.get(data.id) if resource_pool is None: @@ -119,32 +124,39 @@ def _notify_resourcepool(uow, cmd): for sub in subs: sub_data = sub.serialize() logger.debug('Subscription: {}'.format(sub_data['subscriptionId'])) - if not sub_data.get('filter', None): - callback_smo(sub, data, msg_type, resource_pool_dict) + filters = handle_filter(sub_data['filter'], 'ResourcePoolInfo') + if not filters: + callback_smo(sub, data, resource_pool_dict) continue - try: - args = gen_orm_filter(ocloud.Resource, sub_data['filter']) - except KeyError: - logger.warning( - 'Subscription {} filter {} has wrong attribute name ' - 'or value. Ignore the filter.'.format( - sub_data['subscriptionId'], sub_data['filter'])) - callback_smo(sub, data, msg_type, resource_pool_dict) - continue - args.append(ocloud.ResourcePool.resourcePoolId == data.id) - ret = uow.resource_pools.list_with_count(*args) - if ret[0] != 0: - logger.debug( - 'ResourcePool {} skip for subscription {} because of the ' - 'filter.' - .format(data.id, sub_data['subscriptionId'])) + filter_effect = 0 + for filter in filters: + try: + args = gen_orm_filter(ocloud.ResourcePool, filter) + except KeyError: + logger.warning( + 'Subscription {} filter {} has wrong attribute ' + 'name or value. Ignore the filter.'.format( + sub_data['subscriptionId'], + sub_data['filter'])) + continue + if len(args) == 0 and 'objectType' in filter: + filter_effect += 1 + break + args.append(ocloud.ResourcePool.resourcePoolId == data.id) + ret = uow.resource_pools.list_with_count(*args) + if ret[0] > 0: + logger.debug( + 'ResourcePool {} skip for subscription {} because of' + ' the filter.' + .format(data.id, sub_data['subscriptionId'])) + filter_effect += 1 + break + if filter_effect > 0: continue - callback_smo(sub, data, msg_type, resource_pool_dict) + callback_smo(sub, data, resource_pool_dict) -def _notify_dms(uow, cmd): - data = cmd.data - msg_type = cmd.type +def _notify_dms(uow, data): with uow: dms = uow.deployment_managers.get(data.id) if dms is None: @@ -163,34 +175,41 @@ def _notify_dms(uow, cmd): for sub in subs: sub_data = sub.serialize() logger.debug('Subscription: {}'.format(sub_data['subscriptionId'])) - if not sub_data.get('filter', None): - callback_smo(sub, data, msg_type, dms_dict) + filters = handle_filter( + sub_data['filter'], 'DeploymentManagerInfo') + if not filters: + callback_smo(sub, data, dms_dict) continue - try: - args = gen_orm_filter(ocloud.Resource, sub_data['filter']) - except KeyError: - logger.warning( - 'Subscription {} filter {} has wrong attribute name ' - 'or value. Ignore the filter.'.format( - sub_data['subscriptionId'], sub_data['filter'])) - callback_smo(sub, data, msg_type, dms_dict) - continue - args.append( - ocloud.DeploymentManager.deploymentManagerId == data.id) - ret = uow.deployment_managers.list_with_count(*args) - if ret[0] != 0: - logger.debug( - 'DeploymentManager {} skip for subscription {} because of ' - 'the filter.' - .format(data.id, sub_data['subscriptionId'])) + filter_effect = 0 + for filter in filters: + try: + args = gen_orm_filter(ocloud.DeploymentManager, filter) + except KeyError: + logger.warning( + 'Subscription {} filter {} has wrong attribute ' + 'name or value. Ignore the filter.'.format( + sub_data['subscriptionId'], + sub_data['filter'])) + continue + if len(args) == 0 and 'objectType' in filter: + filter_effect += 1 + break + args.append( + ocloud.DeploymentManager.deploymentManagerId == data.id) + ret = uow.deployment_managers.list_with_count(*args) + if ret[0] > 0: + logger.debug( + 'DeploymentManager {} skip for subscription {} because' + ' of the filter.' + .format(data.id, sub_data['subscriptionId'])) + filter_effect += 1 + break + if filter_effect > 0: continue - callback_smo(sub, data) - callback_smo(sub, data, msg_type, dms_dict) + callback_smo(sub, data, dms_dict) -def _notify_resource(uow, cmd): - data = cmd.data - msg_type = cmd.type +def _notify_resource(uow, data): with uow: resource = uow.resources.get(data.id) if resource is None: @@ -198,36 +217,78 @@ def _notify_resource(uow, cmd): return res_pool_id = resource.serialize()['resourcePoolId'] logger.debug('res pool id is {}'.format(res_pool_id)) + res_dict = { + 'resourceId': resource.resourceId, + 'description': resource.description, + 'resourceTypeId': resource.resourceTypeId, + 'resourcePoolId': resource.resourcePoolId, + 'globalAssetId': resource.globalAssetId + } subs = uow.subscriptions.list() for sub in subs: sub_data = sub.serialize() logger.debug('Subscription: {}'.format(sub_data['subscriptionId'])) - if not sub_data.get('filter', None): - callback_smo(sub, data, msg_type) + filters = handle_filter(sub_data['filter'], 'ResourceInfo') + if not filters: + callback_smo(sub, data, res_dict) continue - try: - args = gen_orm_filter(ocloud.Resource, sub_data['filter']) - except KeyError: - logger.warning( - 'Subscription {} filter {} has wrong attribute name ' - 'or value. Ignore the filter.'.format( - sub_data['subscriptionId'], sub_data['filter'])) - callback_smo(sub, data, msg_type) + filter_effect = 0 + for filter in filters: + try: + args = gen_orm_filter(ocloud.Resource, filter) + except KeyError: + logger.warning( + 'Subscription {} filter {} has wrong attribute ' + 'name or value. Ignore the filter.'.format( + sub_data['subscriptionId'], + sub_data['filter'])) + continue + if len(args) == 0 and 'objectType' in filter: + filter_effect += 1 + break + args.append(ocloud.Resource.resourceId == data.id) + ret = uow.resources.list_with_count(res_pool_id, *args) + if ret[0] > 0: + logger.debug( + 'Resource {} skip for subscription {} because of ' + 'the filter.' + .format(data.id, sub_data['subscriptionId'])) + filter_effect += 1 + break + if filter_effect > 0: continue - args.append(ocloud.Resource.resourceId == data.id) - ret = uow.resources.list_with_count(res_pool_id, *args) - if ret[0] != 0: - logger.debug( - 'Resource {} skip for subscription {} because of the ' - 'filter.' - .format(data.id, sub_data['subscriptionId'])) + callback_smo(sub, data, res_dict) + + +def handle_filter(filter: str, f_type: str): + if not filter: + return + filter_strip = filter.strip(' []') + filter_list = filter_strip.split('|') + filters = list() + for sub_filter in filter_list: + exprs = sub_filter.split(';') + objectType = False + objectTypeValue = '' + for expr in exprs: + expr_strip = expr.strip(' ()') + items = expr_strip.split(',') + item_key = items[1].strip() + if item_key != 'objectType': continue - callback_smo(sub, data, msg_type) + objectType = True + objectTypeValue = items[2].strip() + if not objectType: + if f_type == 'ResourceInfo': + filters.append(sub_filter) + continue + if objectTypeValue == f_type: + filters.append(sub_filter) + return filters -def callback_smo(sub: Subscription, msg: Message2SMO, msg_type: str, - obj_dict: dict = None): +def callback_smo(sub: Subscription, msg: Message2SMO, obj_dict: dict = None): sub_data = sub.serialize() callback = { 'consumerSubscriptionId': sub_data['consumerSubscriptionId'], @@ -235,16 +296,17 @@ def callback_smo(sub: Subscription, msg: Message2SMO, msg_type: str, 'objectRef': msg.objectRef, 'updateTime': msg.updatetime } - if msg_type != 'Resource': - if msg.notificationEventType in [NotificationEventEnum.DELETE, - NotificationEventEnum.MODIFY]: - callback['priorObjectState'] = obj_dict - if msg.notificationEventType in [NotificationEventEnum.CREATE, - NotificationEventEnum.MODIFY]: - callback['postObjectState'] = obj_dict + if msg.notificationEventType in [NotificationEventEnum.DELETE, + NotificationEventEnum.MODIFY]: + callback['priorObjectState'] = json.dumps(obj_dict) + if msg.notificationEventType in [NotificationEventEnum.CREATE, + NotificationEventEnum.MODIFY]: + callback['postObjectState'] = json.dumps(obj_dict) + if msg.notificationEventType == NotificationEventEnum.DELETE: + callback.pop('objectRef') callback_data = json.dumps(callback) - logger.info('URL: {}, data: {}'.format( - sub_data['callback'], callback_data)) + logger.info('URL: {}'.format(sub_data['callback'])) + logger.debug('callback data: {}'.format(callback_data)) # Call SMO through the SMO callback url o = urlparse(sub_data['callback'])