X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=o2common%2Fservice%2Fcommand%2Fhandler.py;fp=o2common%2Fservice%2Fcommand%2Fhandler.py;h=83eafef30bc162181d4efcf0fa4052eb00ab092e;hb=3f76188e406303fb0892d7599c91ca811774a798;hp=0000000000000000000000000000000000000000;hpb=562c275195cc03987cbe4bc35fca98364d758c7d;p=pti%2Fo2.git diff --git a/o2common/service/command/handler.py b/o2common/service/command/handler.py new file mode 100644 index 0000000..83eafef --- /dev/null +++ b/o2common/service/command/handler.py @@ -0,0 +1,61 @@ +# Copyright (C) 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import http.client +import ssl +from o2common.helper import o2logging +from o2common.config import config + +logger = o2logging.get_logger(__name__) + + +def post_data(conn, path, data): + headers = {'Content-type': 'application/json'} + conn.request('POST', path, data, headers) + resp = conn.getresponse() + data = resp.read().decode('utf-8') + # json_data = json.loads(data) + if resp.status >= 200 and resp.status <= 299: + logger.info('Post data to SMO successed, response code {} {}, data {}'. + format(resp.status, resp.reason, data)) + return True, resp.status + logger.error('Response code is: {}'.format(resp.status)) + return False, resp.status + + +def get_http_conn(callbackurl): + conn = http.client.HTTPConnection(callbackurl) + return conn + + +# with default CA +def get_https_conn_default(callbackurl): + sslctx = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH) + sslctx.check_hostname = True + sslctx.verify_mode = ssl.CERT_REQUIRED + sslctx.load_default_certs() + conn = http.client.HTTPSConnection(callbackurl, context=sslctx) + return conn + + +# with self signed ca +def get_https_conn_selfsigned(callbackurl): + sslctx = ssl.create_default_context( + purpose=ssl.Purpose.SERVER_AUTH) + smo_ca_path = config.get_smo_ca_config_path() + sslctx.load_verify_locations(smo_ca_path) + sslctx.check_hostname = False + sslctx.verify_mode = ssl.CERT_REQUIRED + conn = http.client.HTTPSConnection(callbackurl, context=sslctx) + return conn