Move registration API to configuration
[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 logger = o2logging.get_logger(__name__)
28
29 r = redis.Redis(**config.get_redis_host_and_port())
30
31 apibase = config.get_o2ims_api_base()
32
33
34 def main():
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')
42
43     for m in pubsub.listen():
44         try:
45             handle_dms_changed(m, bus)
46         except Exception as ex:
47             logger.warning("{}".format(str(ex)))
48             continue
49
50
51 def handle_dms_changed(m, bus):
52     logger.info("handling %s", m)
53     channel = m['channel'].decode("UTF-8")
54     if channel == "NfDeploymentStateChanged":
55         datastr = m['data']
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']
62         )
63         bus.handle(cmd)
64     elif channel == 'ResourceChanged':
65         datastr = m['data']
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']))
74         bus.handle(cmd)
75     elif channel == 'ConfigurationChanged':
76         datastr = m['data']
77         data = json.loads(datastr)
78         logger.info('ConfigurationChanged with cmd:{}'.format(data))
79         cmd = imscmd.Register2SMO(data=RegistrationMessage(id=data['id']))
80         bus.handle(cmd)
81     elif channel == 'OcloudChanged':
82         datastr = m['data']
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))
87             bus.handle(cmd)
88     else:
89         logger.info("unhandled:{}".format(channel))
90
91
92 if __name__ == "__main__":
93     main()