INF-303 Add Infrastructure Monitoring Fault Service; INF-305 update inventory api...
[pti/o2.git] / o2app / entrypoints / redis_eventconsumer.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # import json
16 import redis
17 import json
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
23
24 from o2common.helper import o2logging
25 from o2ims.domain.subscription_obj import Message2SMO, NotificationEventEnum,\
26     RegistrationMessage
27 from o2ims.domain.alarm_obj import AlarmEvent2SMO
28 logger = o2logging.get_logger(__name__)
29
30 r = redis.Redis(**config.get_redis_host_and_port())
31
32 apibase = config.get_o2ims_api_base()
33 api_monitoring_base = config.get_o2ims_monitoring_api_base()
34
35
36 def main():
37     logger.info("Redis pubsub starting")
38     bus = bootstrap.bootstrap()
39     pubsub = r.pubsub(ignore_subscribe_messages=True)
40     pubsub.subscribe("NfDeploymentStateChanged")
41     pubsub.subscribe('ResourceChanged')
42     pubsub.subscribe('ConfigurationChanged')
43     pubsub.subscribe('OcloudChanged')
44     pubsub.subscribe('AlarmEventChanged')
45
46     for m in pubsub.listen():
47         try:
48             handle_changed(m, bus)
49         except Exception as ex:
50             logger.warning("{}".format(str(ex)))
51             continue
52
53
54 def handle_changed(m, bus):
55     logger.info("handling %s", m)
56     channel = m['channel'].decode("UTF-8")
57     if channel == "NfDeploymentStateChanged":
58         datastr = m['data']
59         data = json.loads(datastr)
60         logger.info('HandleNfDeploymentStateChanged with cmd:{}'.format(data))
61         cmd = commands.HandleNfDeploymentStateChanged(
62             NfDeploymentId=data['NfDeploymentId'],
63             FromState=data['FromState'],
64             ToState=data['ToState']
65         )
66         bus.handle(cmd)
67     elif channel == 'ResourceChanged':
68         datastr = m['data']
69         data = json.loads(datastr)
70         logger.info('ResourceChanged with cmd:{}'.format(data))
71         ref = apibase + '/resourcePools/' + data['resourcePoolId'] +\
72             '/resources/' + data['id']
73         cmd = imscmd.PubMessage2SMO(data=Message2SMO(
74             id=data['id'], ref=ref,
75             eventtype=data['notificationEventType'],
76             updatetime=data['updatetime']))
77         bus.handle(cmd)
78     elif channel == 'ConfigurationChanged':
79         datastr = m['data']
80         data = json.loads(datastr)
81         logger.info('ConfigurationChanged with cmd:{}'.format(data))
82         cmd = imscmd.Register2SMO(data=RegistrationMessage(id=data['id']))
83         bus.handle(cmd)
84     elif channel == 'OcloudChanged':
85         datastr = m['data']
86         data = json.loads(datastr)
87         logger.info('OcloudChanged with cmd:{}'.format(data))
88         if data['notificationEventType'] == NotificationEventEnum.CREATE:
89             cmd = imscmd.Register2SMO(data=RegistrationMessage(is_all=True))
90             bus.handle(cmd)
91     elif channel == 'AlarmEventChanged':
92         datastr = m['data']
93         data = json.loads(datastr)
94         logger.info('AlarmEventChanged with cmd:{}'.format(data))
95         ref = api_monitoring_base + '/alarms/' + data['id']
96         cmd = imscmd.PubAlarm2SMO(data=AlarmEvent2SMO(
97             id=data['id'], ref=ref,
98             eventtype=data['notificationEventType'],
99             updatetime=data['updatetime']))
100         bus.handle(cmd)
101     else:
102         logger.info("unhandled:{}".format(channel))
103
104
105 if __name__ == "__main__":
106     main()