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.
19 from o2app import bootstrap
20 from o2common.config import config
21 from o2dms.domain import commands
22 from o2ims.domain import commands as imscmd
23 from o2ims.domain.subscription_obj import Message2SMO, RegistrationMessage
24 from o2ims.domain.alarm_obj import AlarmEvent2SMO
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
29 r = redis.Redis(**config.get_redis_host_and_port())
31 apibase = config.get_o2ims_api_base()
32 api_monitoring_base = config.get_o2ims_monitoring_api_base()
36 logger.info("Redis pubsub starting")
37 bus = bootstrap.bootstrap()
38 pubsub = r.pubsub(ignore_subscribe_messages=True)
39 pubsub.subscribe("NfDeploymentStateChanged")
40 pubsub.subscribe('ResourceChanged')
41 pubsub.subscribe('OcloudChanged')
42 pubsub.subscribe('AlarmEventChanged')
44 for m in pubsub.listen():
46 handle_changed(m, bus)
47 except Exception as ex:
48 logger.warning("{}".format(str(ex)))
52 def handle_changed(m, bus):
53 logger.info("handling %s", m)
54 channel = m['channel'].decode("UTF-8")
55 if channel == "NfDeploymentStateChanged":
57 data = json.loads(datastr)
58 logger.info('HandleNfDeploymentStateChanged with cmd:{}'.format(data))
59 cmd = commands.HandleNfDeploymentStateChanged(
60 NfDeploymentId=data['NfDeploymentId'],
61 FromState=data['FromState'],
62 ToState=data['ToState']
65 elif channel == 'ResourceChanged':
67 data = json.loads(datastr)
68 logger.info('ResourceChanged with cmd:{}'.format(data))
69 ref = apibase + '/resourcePools/' + data['resourcePoolId'] +\
70 '/resources/' + data['id']
71 cmd = imscmd.PubMessage2SMO(data=Message2SMO(
72 id=data['id'], ref=ref,
73 eventtype=data['notificationEventType'],
74 updatetime=data['updatetime']))
76 elif channel == 'OcloudChanged':
78 data = json.loads(datastr)
79 logger.info('OcloudChanged with cmd:{}'.format(data))
80 cmd = imscmd.Register2SMO(data=RegistrationMessage(
81 data['notificationEventType'],
84 elif channel == 'AlarmEventChanged':
86 data = json.loads(datastr)
87 logger.info('AlarmEventChanged with cmd:{}'.format(data))
88 ref = api_monitoring_base + '/alarms/' + data['id']
89 cmd = imscmd.PubAlarm2SMO(data=AlarmEvent2SMO(
90 id=data['id'], ref=ref,
91 eventtype=data['notificationEventType'],
92 updatetime=data['updatetime']))
95 logger.info("unhandled:{}".format(channel))
98 if __name__ == "__main__":