Fix container tag file issue
[pti/o2.git] / o2ims / service / event / notify_handler.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\r
3 #  Licensed under the Apache License, Version 2.0 (the "License");\r
4 #  you may not use this file except in compliance with the License.\r
5 #  You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 #  Unless required by applicable law or agreed to in writing, software\r
10 #  distributed under the License is distributed on an "AS IS" BASIS,\r
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 #  See the License for the specific language governing permissions and\r
13 #  limitations under the License.\r
14 \r
15 import json\r
16 import redis\r
17 # import requests\r
18 import http.client\r
19 from urllib.parse import urlparse\r
20 \r
21 from o2common.config import config\r
22 from o2common.service.unit_of_work import AbstractUnitOfWork\r
23 from o2ims.domain import commands\r
24 from o2ims.domain.subscription_obj import Subscription, Message2SMO\r
25 \r
26 from o2common.helper import o2logging\r
27 logger = o2logging.get_logger(__name__)\r
28 \r
29 # Maybe another MQ server\r
30 r = redis.Redis(**config.get_redis_host_and_port())\r
31 \r
32 \r
33 def notify_change_to_smo(\r
34     cmd: commands.PubMessage2SMO,\r
35     uow: AbstractUnitOfWork,\r
36 ):\r
37     logger.info('In notify_change_to_smo')\r
38     data = cmd.data\r
39     with uow:\r
40         subs = uow.subscriptions.list()\r
41         for sub in subs:\r
42             sub_data = sub.serialize()\r
43             logger.debug('Subscription: {}'.format(sub_data['subscriptionId']))\r
44 \r
45             try:\r
46                 resource_filter = json.loads(sub_data['filter'])\r
47                 if len(resource_filter) > 0:\r
48                     resource = uow.resources.get(data.id)\r
49                     logger.debug(type(resource))\r
50                     if resource:  # TODO deal with resource is empty\r
51                         res_type_id = resource.serialize()['resourceTypeId']\r
52                         resourcetype = uow.resource_types.get(res_type_id)\r
53                         logger.debug(resourcetype.name)\r
54                         if resourcetype.name not in resource_filter:\r
55                             continue\r
56             except json.decoder.JSONDecodeError as err:\r
57                 logger.warning(\r
58                     'subscription filter decode json failed: {}'.format(err))\r
59 \r
60             callback_smo(sub, data)\r
61 \r
62 \r
63 def callback_smo(sub: Subscription, msg: Message2SMO):\r
64     sub_data = sub.serialize()\r
65     callback_data = json.dumps({\r
66         'consumerSubscriptionId': sub_data['consumerSubscriptionId'],\r
67         'notificationEventType': msg.notificationEventType,\r
68         'objectRef': msg.objectRef,\r
69         'updateTime': msg.updatetime\r
70     })\r
71     logger.info('URL: {}, data: {}'.format(\r
72         sub_data['callback'], callback_data))\r
73     # r.publish(sub_data['subscriptionId'], json.dumps({\r
74     #     'consumerSubscriptionId': sub_data['consumerSubscriptionId'],\r
75     #     'notificationEventType': msg.notificationEventType,\r
76     #     'objectRef': msg.objectRef\r
77     # }))\r
78     # try:\r
79     #     headers = {'User-Agent': 'Mozilla/5.0'}\r
80     #     resp = requests.post(sub_data['callback'], data=callback_data,\r
81     #                          headers=headers)\r
82     #     if resp.status_code == 202 or resp.status_code == 200:\r
83     #         logger.info('Notify to SMO successed')\r
84     #         return\r
85     #     logger.error('Response code is: {}'.format(resp.status_code))\r
86     # except requests.exceptions.HTTPError as err:\r
87     #     logger.error('request smo error: {}'.format(err))\r
88     o = urlparse(sub_data['callback'])\r
89     conn = http.client.HTTPConnection(o.netloc)\r
90     headers = {'Content-type': 'application/json'}\r
91     conn.request('POST', o.path, callback_data, headers)\r
92     resp = conn.getresponse()\r
93     data = resp.read().decode('utf-8')\r
94     # json_data = json.loads(data)\r
95     if resp.status == 202 or resp.status == 200:\r
96         logger.info('Notify to SMO successed, response code {} {}, data {}'.\r
97                     format(resp.status, resp.reason, data))\r
98         return\r
99     logger.error('Response code is: {}'.format(resp.status))\r