Add the command that registers to the SMO; Make the create registration and create...
[pti/o2.git] / o2ims / service / command / registration_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 time\r
16 import json\r
17 # import asyncio\r
18 # import requests\r
19 import http.client\r
20 from urllib.parse import urlparse\r
21 from retry import retry\r
22 \r
23 from o2common.service.unit_of_work import AbstractUnitOfWork\r
24 from o2common.config import config\r
25 from o2ims.domain import commands\r
26 from o2ims.domain.subscription_obj import RegistrationStatusEnum\r
27 \r
28 from o2common.helper import o2logging\r
29 logger = o2logging.get_logger(__name__)\r
30 \r
31 \r
32 def registry_to_smo(\r
33     cmd: commands.Register2SMO,\r
34     uow: AbstractUnitOfWork,\r
35 ):\r
36     logger.info('In registry_to_smo')\r
37     data = cmd.data\r
38     logger.info('The Register2SMO all is {}'.format(data.all))\r
39     if data.all:\r
40         regs = uow.registrations.list()\r
41         for reg in regs:\r
42             reg_data = reg.serialize()\r
43             logger.debug('Registration: {}'.format(reg_data['registrationId']))\r
44 \r
45             register_smo(uow, reg_data)\r
46     else:\r
47         with uow:\r
48             reg = uow.registrations.get(data.id)\r
49             if reg is None:\r
50                 return\r
51             logger.debug('Registration: {}'.format(reg.registrationId))\r
52             reg_data = reg.serialize()\r
53             register_smo(uow, reg_data)\r
54 \r
55 \r
56 def register_smo(uow, reg_data):\r
57     call_res = call_smo(reg_data)\r
58     logger.debug('Call SMO response is {}'.format(call_res))\r
59     if call_res:\r
60         reg = uow.registrations.get(reg_data['registrationId'])\r
61         if reg is None:\r
62             return\r
63         reg.status = RegistrationStatusEnum.NOTIFIED\r
64         logger.debug('Updating Registration: {}'.format(\r
65             reg.registrationId))\r
66         uow.registrations.update(reg)\r
67         uow.commit()\r
68 \r
69 \r
70 # def retry(fun, max_tries=2):\r
71 #     for i in range(max_tries):\r
72 #         try:\r
73 #             time.sleep(5*i)\r
74 #             # await asyncio.sleep(5*i)\r
75 #             res = fun()\r
76 #             logger.debug('retry function result: {}'.format(res))\r
77 #             return res\r
78 #         except Exception:\r
79 #             continue\r
80 \r
81 \r
82 @retry((ConnectionRefusedError), tries=2, delay=2)\r
83 def call_smo(reg_data: dict):\r
84     callback_data = json.dumps({\r
85         'consumerSubscriptionId': reg_data['registrationId'],\r
86         'imsUrl': config.get_api_url()\r
87     })\r
88     logger.info('URL: {}, data: {}'.format(\r
89         reg_data['callback'], callback_data))\r
90 \r
91     o = urlparse(reg_data['callback'])\r
92     conn = http.client.HTTPConnection(o.netloc)\r
93     headers = {'Content-type': 'application/json'}\r
94     conn.request('POST', o.path, callback_data, headers)\r
95     resp = conn.getresponse()\r
96     data = resp.read().decode('utf-8')\r
97     # json_data = json.loads(data)\r
98     if resp.status == 202 or resp.status == 200:\r
99         logger.info('Registrer to SMO successed, response code {} {}, data {}'.\r
100                     format(resp.status, resp.reason, data))\r
101         return True\r
102     logger.error('Response code is: {}'.format(resp.status))\r
103     return False\r