X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fservice%2Fcommand%2Fregistration_handler.py;h=c144405bedd91117e063362dee06a797cb6f9bf5;hb=d71385743dbf345b507a0ad53357910cc81c19cf;hp=0c6bbefd3923a17d39df72fc13684147a037fdff;hpb=c34ab47848138150a1456be2fa9f82d7260e37b8;p=pti%2Fo2.git diff --git a/o2ims/service/command/registration_handler.py b/o2ims/service/command/registration_handler.py index 0c6bbef..c144405 100644 --- a/o2ims/service/command/registration_handler.py +++ b/o2ims/service/command/registration_handler.py @@ -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. @@ -16,13 +16,17 @@ import json # import asyncio # import requests -import http.client + from urllib.parse import urlparse from retry import retry from o2common.service.unit_of_work import AbstractUnitOfWork from o2common.config import config, conf - +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 o2ims.domain import commands from o2ims.domain.subscription_obj import NotificationEventEnum @@ -48,9 +52,18 @@ def registry_to_smo( register_smo(uow, ocloud_dict) +class RegIMSToSMOExp(Exception): + def __init__(self, value): + self.value = value + + def register_smo(uow, ocloud_data): - call_res = call_smo(ocloud_data) + call_res, status = call_smo(ocloud_data) logger.debug('Call SMO response is {}'.format(call_res)) + if call_res is True: + logger.info('Register to smo success response is {}'.format(status)) + else: + raise RegIMSToSMOExp('Register o2ims to SMO failed') # TODO: record the result for the smo register @@ -87,15 +100,26 @@ def call_smo(reg_data: dict): logger.info('URL: {}, data: {}'.format( conf.DEFAULT.smo_register_url, callback_data)) o = urlparse(conf.DEFAULT.smo_register_url) - 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('Registrer to SMO successed, response code {} {}, data {}'. - format(resp.status, resp.reason, data)) - return True - logger.error('Response code is: {}'.format(resp.status)) - return False + if o.scheme == 'https': + conn = get_https_conn_default(o.netloc) + else: + conn = get_http_conn(o.netloc) + + try: + return post_data(conn, o.path, callback_data) + except ssl.SSLCertVerificationError as e: + logger.debug('Try to register to smo 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( + 'Register to smo with self-signed ca failed: {}'.format(e)) + # TODO: write the status to extension db table. + return False, None + return False, None + except Exception as e: + logger.critical('Register to smo except: {}'.format(e)) + return False, None