1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from o2common.config import config, conf
18 from o2common.domain.filter import gen_orm_filter
19 from o2common.service.unit_of_work import AbstractUnitOfWork
20 from o2common.adapter.notifications import AbstractNotifications
22 from o2ims.domain import commands, ocloud as cloud
23 from o2ims.domain.subscription_obj import Message2SMO, NotificationEventEnum
25 from .notify_handler import handle_filter, callback_smo
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
30 apibase = config.get_o2ims_api_base()
31 api_monitoring_base = config.get_o2ims_monitoring_api_base()
32 inventory_api_version = config.get_o2ims_inventory_api_v1()
36 cmd: commands.Register2SMO,
37 uow: AbstractUnitOfWork,
38 notifications: AbstractNotifications,
40 logger.debug('In registry_to_smo')
42 logger.info('The Register2SMO notificationEventType is {}'.format(
43 data.notificationEventType))
45 ocloud = uow.oclouds.get(data.id)
47 logger.warning('Ocloud {} does not exists.'.format(data.id))
49 logger.debug('O-Cloud Global UUID: {}'.format(ocloud.globalCloudId))
50 # ocloud_dict = ocloud.serialize()
52 'oCloudId': ocloud.oCloudId,
53 'globalcloudId': ocloud.globalCloudId,
54 'globalCloudId': ocloud.globalCloudId,
56 'description': ocloud.description,
57 'serviceUri': ocloud.serviceUri
59 if data.notificationEventType == NotificationEventEnum.CREATE:
60 register_smo(notifications, ocloud_dict)
61 elif data.notificationEventType in [NotificationEventEnum.MODIFY,
62 NotificationEventEnum.DELETE]:
63 _notify_ocloud(uow, data, ocloud_dict)
66 class RegIMSToSMOExp(Exception):
67 def __init__(self, value):
71 def register_smo(notifications, ocloud_data):
72 call_res = call_smo(notifications, ocloud_data)
73 logger.debug('Call SMO response is {}'.format(call_res))
75 logger.info('Register to smo success response')
77 raise RegIMSToSMOExp('Register o2ims to SMO failed')
78 # TODO: record the result for the smo register
81 def _notify_ocloud(uow, data, ocloud_dict):
82 ref = api_monitoring_base + inventory_api_version
84 eventtype=data.notificationEventType, id=data.id,
85 ref=ref, updatetime=data.updatetime)
86 ocloud_dict.pop('globalCloudId')
87 subs = uow.subscriptions.list()
89 sub_data = sub.serialize()
90 logger.debug('Subscription: {}'.format(sub_data['subscriptionId']))
91 filters = handle_filter(sub_data['filter'], 'CloudInfo')
93 callback_smo(sub, msg, ocloud_dict)
96 for filter in filters:
98 args = gen_orm_filter(cloud.Ocloud, filter)
101 'Subscription {} filter {} has wrong attribute '
102 'name or value. Ignore the filter.'.format(
103 sub_data['subscriptionId'],
106 if len(args) == 0 and 'objectType' in filter:
109 args.append(cloud.Ocloud.oCloudId == data.id)
110 ret = uow.oclouds.list(*args)
115 logger.info('Subscription {} filter hit, skip oCloud {}.'
116 .format(sub_data['subscriptionId'], data.id))
118 callback_smo(sub, msg, ocloud_dict)
121 def call_smo(notifications: AbstractNotifications, reg_data: dict):
122 smo_token = conf.DEFAULT.smo_token_data
126 'smo_token_payload': smo_token,
127 'smo_token_type': 'jwt',
128 'smo_token_expiration': '',
129 'smo_token_algo': 'RS256'
133 'globalCloudId': reg_data['globalCloudId'],
134 'oCloudId': reg_data['oCloudId'],
135 'IMS_EP': config.get_api_url(),
136 'smo_token_data': smo_token_info
138 logger.info('callback URL: {}'.format(conf.DEFAULT.smo_register_url))
139 logger.debug('callback data: {}'.format(json.dumps(callback)))
140 return notifications.send(conf.DEFAULT.smo_register_url, callback)