2 # ============LICENSE_START=======================================================
3 # Copyright (C) 2021 Nordix Foundation.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
20 # http://www.apache.org/licenses/LICENSE-2.0
21 # Unless required by applicable law or agreed to in writing, software
22 # distributed under the License is distributed on an "AS IS" BASIS,
23 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 # See the License for the specific language governing permissions and
25 # limitations under the License.
27 # SPDX-License-Identifier: Apache-2.0
28 # ============LICENSE_END========================================================
30 from io import BytesIO
31 from pathlib import Path
35 from sdcBePy.common import logger
40 def __init__(self, sdc_be_proxy):
41 self.__base_path = Path('/sdc2/rest/v1/catalog/upload')
42 self.__import_all_path = self.__base_path / 'resource/import'
43 self.__sdc_be_proxy = sdc_be_proxy
45 def import_all(self, node_type_yaml_path, node_type_metadata_json_str, is_update=False):
46 logger.debug("Starting to import node types '{}'".format(node_type_yaml_path))
48 multi_part_form_data = []
50 node_type_yaml_param = ('nodeTypesYaml', (pycurl.FORM_FILE, str(node_type_yaml_path)))
51 multi_part_form_data.append(node_type_yaml_param)
53 node_type_metadata_json_param = ('nodeTypeMetadataJson', (
54 pycurl.FORM_CONTENTS, node_type_metadata_json_str,
55 pycurl.FORM_CONTENTTYPE, 'application/json'
57 multi_part_form_data.append(node_type_metadata_json_param)
59 if is_update is not None:
60 create_new_version_param = ('createNewVersion', (
61 pycurl.FORM_CONTENTS, str(is_update).lower(),
62 pycurl.FORM_CONTENTTYPE, 'text/plain'
64 multi_part_form_data.append(create_new_version_param)
66 response_buffer = BytesIO()
67 response_code = self.__sdc_be_proxy.post_file(str(self.__import_all_path), multi_part_form_data,
69 logger.debug("Import all node types response code '{}'".format(response_code))
70 if response_code != 201:
71 error_msg = "Failed to import node types '{}'".format(node_type_yaml_path)
72 logger.log(error_msg, response_buffer.getvalue())
73 raise Exception(error_msg)