diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-05-28 19:10:30 +0100 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-06-14 08:19:21 +0000 |
commit | ddf9aaefc753b492fb72144d597a27df8080a4ab (patch) | |
tree | 8d998a1287b903f6669df8568bdb6d2edda60eac /catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py | |
parent | c82aebcde26e34c4151531b4d7a8f6e7689734ba (diff) |
Init ONAP model imports using the model API
Creates a client for the model endpoint in the catalog init scripts.
Introduces the directory structure to provide the models along its
imports, separated by init/upgrade phase.
Each model structure will be zipped and uploaded to the endpoint,
based on the model directory name.
Change-Id: I0392c1e6d3a29b30567b11016041a8e9cccbc745
Issue-ID: SDC-3615
Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py')
-rw-r--r-- | catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py new file mode 100644 index 0000000000..29b01bbdee --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/model_client.py @@ -0,0 +1,81 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2021 Nordix Foundation +# =============================================================================== +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END======================================================== + +import json +from io import BytesIO +from pathlib import Path + +import pycurl + +from sdcBePy.common import logger + + +class ModelClient: + + def __init__(self, sdc_be_proxy): + self.__base_path = Path('/sdc2/rest/v1/catalog/model') + self.__imports_path = self.__base_path / 'imports' + self.__sdc_be_proxy = sdc_be_proxy + + def create_model(self, model_payload_dict, model_imports_zip_path): + model_name = model_payload_dict['name'] + + logger.debug("Starting to create model '{}', zip path '{}'".format(model_name, model_imports_zip_path)) + + multi_part_form_data = [] + + model_zip_param = ('modelImportsZip', (pycurl.FORM_FILE, str(model_imports_zip_path))) + multi_part_form_data.append(model_zip_param) + + json_payload = self.__parse_to_json_str(model_payload_dict) + model_param = ('model', ( + pycurl.FORM_CONTENTS, json_payload, + pycurl.FORM_CONTENTTYPE, 'application/json' + )) + multi_part_form_data.append(model_param) + + response_buffer = BytesIO() + response_code = self.__sdc_be_proxy.post_file(str(self.__base_path), multi_part_form_data, response_buffer) + logger.debug("Create model response code '{}'".format(response_code)) + if response_code != 201: + error_msg = "Failed to create model '{}'".format(model_name) + logger.log(error_msg, response_buffer.getvalue()) + raise Exception(error_msg) + logger.log("Created model", model_name) + + def update_model_imports(self, model_payload_dict, model_imports_zip_path): + model_name = model_payload_dict['name'] + logger.debug("Starting to update model '{}', zip path '{}'".format(model_name, model_imports_zip_path)) + + multi_part_form_data = [] + + model_zip_post = ('modelImportsZip', (pycurl.FORM_FILE, str(model_imports_zip_path))) + multi_part_form_data.append(('modelName', model_name)) + multi_part_form_data.append(model_zip_post) + + response_buffer = BytesIO() + response_code = self.__sdc_be_proxy.put_file(str(self.__imports_path), multi_part_form_data, response_buffer) + logger.debug("Update model response code '{}'".format(response_code)) + if response_code != 204: + error_msg = "Failed to update model '{}'".format(model_name) + logger.log(error_msg, response_buffer.getvalue()) + raise Exception(error_msg) + logger.log("Updated model", model_name) + + @staticmethod + def __parse_to_json_str(model_payload_dict): + return json.dumps(model_payload_dict) |