+ logger.debug('In notify_change_to_smo')
+ msg_type = cmd.type
+ if msg_type == 'ResourceType':
+ _notify_resourcetype(uow, notifications, cmd.data)
+ elif msg_type == 'ResourcePool':
+ _notify_resourcepool(uow, notifications, cmd.data)
+ elif msg_type == 'Dms':
+ _notify_dms(uow, notifications, cmd.data)
+ elif msg_type == 'Resource':
+ _notify_resource(uow, notifications, cmd.data)
+ elif msg_type == 'OCloud':
+ _notify_ocloud(uow, notifications, cmd.data)
+
+
+def __get_object_type_and_value(sub_filter):
+ exprs = sub_filter.split(';')
+ for expr in exprs:
+ items = expr.strip(' ()').split(',')
+ item_key = items[1].strip()
+ if item_key == 'objectType':
+ return True, items[2].strip()
+ return False, ''
+
+
+def handle_filter(filter: str, f_type: str):
+ if not filter:
+ return
+
+ filter_list = filter.strip(' []').split('|')
+ if not filter_list:
+ return
+
+ match_type_count = 0
+ filters = []
+ for sub_filter in filter_list:
+ objectType, objectTypeValue = __get_object_type_and_value(sub_filter)
+ if objectTypeValue == f_type:
+ match_type_count += 1
+ filters.append(sub_filter)
+ elif not objectType and f_type == 'ResourceInfo':
+ match_type_count += 1
+ filters.append(sub_filter)
+
+ return match_type_count, filters
+
+
+def check_filters(filters, sub_data, uow_cls, obj_cls, attr_id, id):
+ for filter in filters[1]:
+ logger.debug(f'filter: {filter}')
+ try:
+ args = gen_orm_filter(obj_cls, filter)
+ except KeyError:
+ logger.warning(
+ 'Subscription {} filter {} has wrong attribute '
+ 'name or value. Ignore the filter.'.format(
+ sub_data['subscriptionId'],
+ sub_data['filter']))
+ continue
+ logger.debug(f'args: {args}')
+
+ if len(args) == 0 and 'objectType' in filter:
+ return False
+
+ args.append(attr_id == id)
+ obj_count, _ = uow_cls.list_with_count(*args)
+ if obj_count > 0:
+ return True
+ return False
+
+
+def _notify_resourcetype(uow, notifications, data):
+ with uow:
+ resource_type = uow.resource_types.get(data.id)
+ if resource_type is None:
+ logger.warning('ResourceType {} does not exists.'.format(data.id))
+ return
+
+ resource_type_dict = resource_type.get_notification_dict()
+
+ subs = uow.subscriptions.list()
+ for sub in subs:
+ sub_data = sub.serialize()
+ logger.debug('Subscription: {}'.format(sub_data['subscriptionId']))
+ filters = handle_filter(sub_data['filter'], 'ResourceTypeInfo')
+ logger.debug(f'filters: {filters}, sub_data: {sub_data}')
+
+ if not filters or filters[0] == 0 or check_filters(
+ filters, sub_data, uow.resource_types, ocloud.ResourceType,
+ ocloud.ResourceType.resourceTypeId, data.id):
+ callback_smo(notifications, sub, data, resource_type_dict)
+ continue
+
+ logger.info('Subscription {} filter hit, skip ResourceType {}.'
+ .format(sub_data['subscriptionId'], data.id))
+
+
+def _notify_resourcepool(uow, notifications, data):
+ with uow:
+ resource_pool = uow.resource_pools.get(data.id)
+ if resource_pool is None:
+ logger.warning('ResourcePool {} does not exists.'.format(data.id))
+ return
+
+ resource_pool_dict = resource_pool.get_notification_dict()
+
+ subs = uow.subscriptions.list()
+ for sub in subs:
+ sub_data = sub.serialize()
+ logger.debug('Subscription: {}'.format(sub_data['subscriptionId']))
+ filters = handle_filter(sub_data['filter'], 'ResourcePoolInfo')
+ logger.debug(f'filters: {filters}, sub_data: {sub_data}')
+
+ if not filters or filters[0] == 0 or check_filters(
+ filters, sub_data, uow.resource_pools, ocloud.ResourcePool,
+ ocloud.ResourcePool.resourcePoolId, data.id):
+ callback_smo(notifications, sub, data, resource_pool_dict)
+ continue
+
+ logger.info('Subscription {} filter hit, skip ResourcePool {}.'
+ .format(sub_data['subscriptionId'], data.id))
+
+
+def _notify_dms(uow, notifications, data):
+ with uow:
+ dms = uow.deployment_managers.get(data.id)
+ if dms is None:
+ logger.warning(
+ 'DeploymentManager {} does not exists.'.format(data.id))
+ return
+
+ dms_dict = dms.get_notification_dict()
+
+ subs = uow.subscriptions.list()
+ for sub in subs:
+ sub_data = sub.serialize()
+ logger.debug('Subscription: {}'.format(sub_data['subscriptionId']))
+ filters = handle_filter(
+ sub_data['filter'], 'DeploymentManagerInfo')
+ logger.debug(f'filters: {filters}, sub_data: {sub_data}')
+
+ if not filters or filters[0] == 0 or check_filters(
+ filters, sub_data, uow.deployment_managers,
+ ocloud.DeploymentManager,
+ ocloud.DeploymentManager.deploymentManagerId, data.id):
+ callback_smo(notifications, sub, data, dms_dict)
+ continue
+
+ logger.info('Subscription {} filter hit, skip '
+ 'DeploymentManager {}.'
+ .format(sub_data['subscriptionId'], data.id))
+
+
+def _notify_ocloud(uow, notifications, data):