1d38f0485f13c2a20f1c0a0e20899a6bfa594d7b
[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 from logging import log
17 import redis
18 import json
19 from o2app import bootstrap
20 from o2common.config import config
21 # from o2common.domain import commands
22 from o2dms.domain import commands
23 from o2dms.domain import events
24
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28 r = redis.Redis(**config.get_redis_host_and_port())
29
30
31 def main():
32     logger.info("Redis pubsub starting")
33     bus = bootstrap.bootstrap()
34     pubsub = r.pubsub(ignore_subscribe_messages=True)
35     pubsub.subscribe("NfDeploymentCreated")
36
37     for m in pubsub.listen():
38         try:
39             handle_dms_changed(m, bus)
40         except Exception as ex:
41             logger.warning("{}".format(str(ex)))
42             continue
43
44
45 def handle_dms_changed(m, bus):
46     logger.info("handling %s", m)
47     channel = m['channel'].decode("UTF-8")
48     if channel == "NfDeploymentCreated":
49         datastr = m['data']
50         data = json.loads(datastr)
51         logger.info('InstallNfDeployment with cmd:{}'.format(data))
52         cmd = commands.InstallNfDeployment(NfDeploymentId = data['NfDeploymentId'])
53         bus.handle(cmd)
54     else:
55         logger.info("unhandled:{}".format(channel))
56     # data = json.loads(m["data"])
57     # cmd = commands.UpdateDms(ref=data["dmsid"])
58     # bus.handle(cmd)
59
60
61 if __name__ == "__main__":
62     main()