1 # Copyright (C) 2021 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.
18 from o2app import bootstrap
19 from o2common.config import config
20 # from o2common.domain import commands
21 from o2dms.domain import commands
22 from o2ims.domain import commands as imscmd
24 from o2common.helper import o2logging
25 from o2ims.domain.subscription_obj import Message2SMO, NotificationEventEnum,\
27 logger = o2logging.get_logger(__name__)
29 r = redis.Redis(**config.get_redis_host_and_port())
31 apibase = config.get_o2ims_api_base()
35 logger.info("Redis pubsub starting")
36 bus = bootstrap.bootstrap()
37 pubsub = r.pubsub(ignore_subscribe_messages=True)
38 pubsub.subscribe("NfDeploymentStateChanged")
39 pubsub.subscribe('ResourceChanged')
40 pubsub.subscribe('ConfigurationChanged')
41 pubsub.subscribe('OcloudChanged')
43 for m in pubsub.listen():
45 handle_dms_changed(m, bus)
46 except Exception as ex:
47 logger.warning("{}".format(str(ex)))
51 def handle_dms_changed(m, bus):
52 logger.info("handling %s", m)
53 channel = m['channel'].decode("UTF-8")
54 if channel == "NfDeploymentStateChanged":
56 data = json.loads(datastr)
57 logger.info('HandleNfDeploymentStateChanged with cmd:{}'.format(data))
58 cmd = commands.HandleNfDeploymentStateChanged(
59 NfDeploymentId=data['NfDeploymentId'],
60 FromState=data['FromState'],
61 ToState=data['ToState']
64 elif channel == 'ResourceChanged':
66 data = json.loads(datastr)
67 logger.info('ResourceChanged with cmd:{}'.format(data))
68 ref = apibase + '/resourcePools/' + data['resourcePoolId'] +\
69 '/resources/' + data['id']
70 cmd = imscmd.PubMessage2SMO(data=Message2SMO(
71 id=data['id'], ref=ref,
72 eventtype=data['notificationEventType'],
73 updatetime=data['updatetime']))
75 elif channel == 'ConfigurationChanged':
77 data = json.loads(datastr)
78 logger.info('ConfigurationChanged with cmd:{}'.format(data))
79 cmd = imscmd.Register2SMO(data=RegistrationMessage(id=data['id']))
81 elif channel == 'OcloudChanged':
83 data = json.loads(datastr)
84 logger.info('OcloudChanged with cmd:{}'.format(data))
85 if data['notificationEventType'] == NotificationEventEnum.CREATE:
86 cmd = imscmd.Register2SMO(data=RegistrationMessage(is_all=True))
89 logger.info("unhandled:{}".format(channel))
92 if __name__ == "__main__":