3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
17 from toscaparser.common.exception import ExceptionCollector
18 from toscaparser.common.exception import ValidationError
19 from toscaparser.prereq.csar import CSAR
20 from toscaparser.utils.gettextutils import _
21 from toscaparser.utils.urlutils import UrlUtils
22 from toscaparser.utils import yamlparser
27 from BytesIO import BytesIO
28 except ImportError: # Python 3.x
29 from io import BytesIO
31 TOSCA_META = 'TOSCA-Metadata/TOSCA.meta'
32 YAML_LOADER = yamlparser.load_yaml
36 def __init__(self, csar_file, model_name, a_file=True):
37 super(SDCCSAR, self).__init__(csar_file, a_file=True)
38 self.model_name = model_name
41 """Validate the provided CSAR file."""
42 self.is_validated = True
43 # validate that the file or URL exists
44 missing_err_msg = (_('"%s" does not exist.') % self.path)
46 if not os.path.isfile(self.path):
47 ExceptionCollector.appendException(
48 ValidationError(message=missing_err_msg))
53 if not UrlUtils.validate_url(self.path):
54 ExceptionCollector.appendException(
55 ValidationError(message=missing_err_msg))
58 response = requests.get(self.path)
59 self.csar = BytesIO(response.content)
61 # validate that it is a valid zip file
62 if not zipfile.is_zipfile(self.csar):
63 err_msg = (_('"%s" is not a valid zip file.') % self.path)
64 ExceptionCollector.appendException(
65 ValidationError(message=err_msg))
68 # validate that it contains the metadata file in the correct location
69 self.zfile = zipfile.ZipFile(self.csar, 'r')
70 filelist = self.zfile.namelist()
71 if TOSCA_META in filelist:
72 self.is_tosca_metadata = True
73 # validate that 'Entry-Definitions' property exists in TOSCA.meta
74 is_validated = self._validate_tosca_meta(filelist)
76 self.is_tosca_metadata = False
77 is_validated = self._validate_root_level_yaml(filelist)
80 main_tpl = self._read_template_yaml(self.main_template_file_name)
81 nst_properies_res = self.get_nst_properties(main_tpl)
82 print("nst properties", nst_properies_res)
83 return nst_properies_res
85 def get_nst_properties(self, main_tpl):
86 importsarr = main_tpl.get('imports')
87 for imports in importsarr:
89 if "service-{}-interface".format(self.model_name) in key:
91 filename = val.get("file")
92 datanew = self._read_template_yaml("Definitions/" + filename)
93 node_types = datanew.get("node_types")
94 for key in list(node_types):
95 if "org.openecomp" in key:
96 nodedata = node_types[key]
97 nst_properties = nodedata.get("properties")