Change notify handler to support https.
[pti/o2.git] / o2ims / service / command / notify_handler.py
index 793f9d2..fe22f2a 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 Wind River Systems, Inc.
+# Copyright (C) 2021-2022 Wind River Systems, Inc.
 #
 #  Licensed under the Apache License, Version 2.0 (the "License");
 #  you may not use this file except in compliance with the License.
 import json
 # import redis
 # import requests
-import http.client
 from urllib.parse import urlparse
 
 # from o2common.config import config
 from o2common.service.unit_of_work import AbstractUnitOfWork
 from o2ims.domain import commands
 from o2ims.domain.subscription_obj import Subscription, Message2SMO
-
+from o2common.service.command.handler import get_https_conn_default
+from o2common.service.command.handler import get_http_conn
+from o2common.service.command.handler import get_https_conn_selfsigned
+from o2common.service.command.handler import post_data
+import ssl
 from o2common.helper import o2logging
 logger = o2logging.get_logger(__name__)
 
+
 # # Maybe another MQ server
 # r = redis.Redis(**config.get_redis_host_and_port())
 
@@ -86,14 +90,30 @@ def callback_smo(sub: Subscription, msg: Message2SMO):
     # except requests.exceptions.HTTPError as err:
     #     logger.error('request smo error: {}'.format(err))
     o = urlparse(sub_data['callback'])
-    conn = http.client.HTTPConnection(o.netloc)
-    headers = {'Content-type': 'application/json'}
-    conn.request('POST', o.path, callback_data, headers)
-    resp = conn.getresponse()
-    data = resp.read().decode('utf-8')
-    # json_data = json.loads(data)
-    if resp.status == 202 or resp.status == 200:
-        logger.info('Notify to SMO successed, response code {} {}, data {}'.
-                    format(resp.status, resp.reason, data))
-        return
-    logger.error('Response code is: {}'.format(resp.status))
+    if o.scheme == 'https':
+        conn = get_https_conn_default(o.netloc)
+    else:
+        conn = get_http_conn(o.netloc)
+    try:
+        rst, status = post_data(conn, o.path, callback_data)
+        if rst is True:
+            logger.info(
+                'Notify to SMO successed with status: {}'.format(status))
+            return
+        logger.error('Notify Response code is: {}'.format(status))
+    except ssl.SSLCertVerificationError as e:
+        logger.info('Notify post data with trusted ca failed: {}'.format(e))
+        if 'self signed' in str(e):
+            conn = get_https_conn_selfsigned(o.netloc)
+            try:
+                return post_data(conn, o.path, callback_data)
+            except Exception as e:
+                logger.info(
+                    'Notify post data with self-signed ca \
+                    failed: {}'.format(e))
+                # TODO: write the status to extension db table.
+                return False
+        return False
+    except Exception as e:
+        logger.critical('Notify except: {}'.format(e))
+        return False