2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api.elements;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
29 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
30 import org.onap.sdc.toscaparser.api.utils.CopyUtils;
31 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
32 import org.onap.sdc.toscaparser.api.extensions.ExtTools;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.yaml.snakeyaml.Yaml;
37 public class EntityType {
39 private static Logger log = LoggerFactory.getLogger(EntityType.class.getName());
41 private static final String TOSCA_DEFINITION_1_0_YAML = "TOSCA_definition_1_0.yaml";
42 protected static final String DERIVED_FROM = "derived_from";
43 protected static final String PROPERTIES = "properties";
44 protected static final String ATTRIBUTES = "attributes";
45 protected static final String REQUIREMENTS = "requirements";
46 protected static final String INTERFACES = "interfaces";
47 protected static final String CAPABILITIES = "capabilities";
48 protected static final String TYPE = "type";
49 protected static final String ARTIFACTS = "artifacts";
51 @SuppressWarnings("unused")
52 private static final String SECTIONS[] = {
53 DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS,
54 INTERFACES, CAPABILITIES, TYPE, ARTIFACTS
57 public static final String TOSCA_DEF_SECTIONS[] = {
58 "node_types", "data_types", "artifact_types",
59 "group_types", "relationship_types",
60 "capability_types", "interface_types",
64 // TOSCA definition file
65 //private final static String path = EntityType.class.getProtectionDomain().getCodeSource().getLocation().getPath();
67 //private final static String path = EntityType.class.getClassLoader().getResource("TOSCA_definition_1_0.yaml").getFile();
68 //private final static String TOSCA_DEF_FILE = EntityType.class.getClassLoader().getResourceAsStream("TOSCA_definition_1_0.yaml");
70 private static LinkedHashMap<String, Object> TOSCA_DEF_LOAD_AS_IS = loadTdf();
72 //EntityType.class.getClassLoader().getResourceAsStream("TOSCA_definition_1_0.yaml");
74 @SuppressWarnings("unchecked")
75 private static LinkedHashMap<String, Object> loadTdf() {
76 String toscaDefLocation = EntityType.class.getClassLoader().getResource(TOSCA_DEFINITION_1_0_YAML).getFile();
77 InputStream input = EntityType.class.getClassLoader().getResourceAsStream(TOSCA_DEFINITION_1_0_YAML);
79 log.error("EntityType - loadTdf - Couldn't load TOSCA_DEF_FILE {}", toscaDefLocation);
81 Yaml yaml = new Yaml();
82 Object loaded = yaml.load(input);
83 //@SuppressWarnings("unchecked")
84 return (LinkedHashMap<String, Object>) loaded;
87 // Map of definition with pre-loaded values of TOSCA_DEF_FILE_SECTIONS
88 public static LinkedHashMap<String, Object> TOSCA_DEF;
91 TOSCA_DEF = new LinkedHashMap<String, Object>();
92 for (String section : TOSCA_DEF_SECTIONS) {
93 @SuppressWarnings("unchecked")
94 LinkedHashMap<String, Object> value = (LinkedHashMap<String, Object>) TOSCA_DEF_LOAD_AS_IS.get(section);
96 for (String key : value.keySet()) {
97 TOSCA_DEF.put(key, value.get(key));
103 public static final String DEPENDSON = "tosca.relationships.DependsOn";
104 public static final String HOSTEDON = "tosca.relationships.HostedOn";
105 public static final String CONNECTSTO = "tosca.relationships.ConnectsTo";
106 public static final String ATTACHESTO = "tosca.relationships.AttachesTo";
107 public static final String LINKSTO = "tosca.relationships.network.LinksTo";
108 public static final String BINDSTO = "tosca.relationships.network.BindsTo";
110 public static final String RELATIONSHIP_TYPE[] = {
111 "tosca.relationships.DependsOn",
112 "tosca.relationships.HostedOn",
113 "tosca.relationships.ConnectsTo",
114 "tosca.relationships.AttachesTo",
115 "tosca.relationships.network.LinksTo",
116 "tosca.relationships.network.BindsTo"};
118 public static final String NODE_PREFIX = "tosca.nodes.";
119 public static final String RELATIONSHIP_PREFIX = "tosca.relationships.";
120 public static final String CAPABILITY_PREFIX = "tosca.capabilities.";
121 public static final String INTERFACE_PREFIX = "tosca.interfaces.";
122 public static final String ARTIFACT_PREFIX = "tosca.artifacts.";
123 public static final String POLICY_PREFIX = "tosca.policies.";
124 public static final String GROUP_PREFIX = "tosca.groups.";
125 //currently the data types are defined only for network
126 // but may have changes in the future.
127 public static final String DATATYPE_PREFIX = "tosca.datatypes.";
128 public static final String DATATYPE_NETWORK_PREFIX = DATATYPE_PREFIX + "network.";
129 public static final String TOSCA = "tosca";
131 protected String type;
132 protected LinkedHashMap<String, Object> defs = null;
134 public Object getParentType() {
138 public String derivedFrom(LinkedHashMap<String, Object> defs) {
139 // Return a type this type is derived from
140 return (String) entityValue(defs, "derived_from");
143 public boolean isDerivedFrom(String type_str) {
144 // Check if object inherits from the given type
145 // Returns true if this object is derived from 'type_str'
147 if (type == null || this.type.isEmpty()) {
149 } else if (type == type_str) {
151 } else if (getParentType() != null) {
152 return ((EntityType) getParentType()).isDerivedFrom(type_str);
158 public Object entityValue(LinkedHashMap<String, Object> defs, String key) {
160 return defs.get(key);
165 @SuppressWarnings("unchecked")
166 public Object getValue(String ndtype, LinkedHashMap<String, Object> _defs, boolean parent) {
174 Object defndt = _defs.get(ndtype);
175 if (defndt != null) {
176 // copy the value to avoid that next operations add items in the
178 //value = copy.copy(defs[ndtype])
179 value = CopyUtils.copyLhmOrAl(defndt);
186 if (p.defs != null && p.defs.get(ndtype) != null) {
187 // get the parent value
188 Object parentValue = p.defs.get(ndtype);
190 if (value instanceof LinkedHashMap) {
191 for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) parentValue).entrySet()) {
192 String k = me.getKey();
193 if (((LinkedHashMap<String, Object>) value).get(k) == null) {
194 ((LinkedHashMap<String, Object>) value).put(k, me.getValue());
198 if (value instanceof ArrayList) {
199 for (Object pValue : (ArrayList<Object>) parentValue) {
200 if (!((ArrayList<Object>) value).contains(pValue)) {
201 ((ArrayList<Object>) value).add(pValue);
206 // value = copy.copy(parent_value)
207 value = CopyUtils.copyLhmOrAl(parentValue);
210 p = (EntityType) p.getParentType();
218 @SuppressWarnings("unchecked")
219 public Object getDefinition(String ndtype) {
221 LinkedHashMap<String, Object> _defs;
222 // no point in hasattr, because we have it, and it
223 // doesn't do anything except emit an exception anyway
224 //if not hasattr(self, 'defs'):
226 // ValidationIssueCollector.appendException(
227 // ValidationError(message="defs is " + str(defs)))
233 if (_defs != null && _defs.get(ndtype) != null) {
234 value = _defs.get(ndtype);
237 Object p = getParentType();
239 Object inherited = ((EntityType) p).getDefinition(ndtype);
240 if (inherited != null) {
241 // inherited = dict(inherited) WTF?!?
246 //inherited.update(value)
247 //value.update(inherited)
248 for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) inherited).entrySet()) {
249 ((LinkedHashMap<String, Object>) value).put(me.getKey(), me.getValue());
257 public static void updateDefinitions(String version) {
258 ExtTools exttools = new ExtTools();
259 String extensionDefsFile = exttools.getDefsFile(version);
261 try (InputStream input = EntityType.class.getClassLoader().getResourceAsStream(extensionDefsFile);) {
262 Yaml yaml = new Yaml();
263 LinkedHashMap<String, Object> nfvDefFile = (LinkedHashMap<String, Object>) yaml.load(input);
264 LinkedHashMap<String, Object> nfvDef = new LinkedHashMap<>();
265 for (String section : TOSCA_DEF_SECTIONS) {
266 if (nfvDefFile.get(section) != null) {
267 LinkedHashMap<String, Object> value =
268 (LinkedHashMap<String, Object>) nfvDefFile.get(section);
269 for (String key : value.keySet()) {
270 nfvDef.put(key, value.get(key));
274 TOSCA_DEF.putAll(nfvDef);
275 } catch (IOException e) {
276 log.error("EntityType - updateDefinitions - Failed to update definitions from defs file {}", extensionDefsFile);
277 log.error("Exception:", e);
278 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE280",
279 String.format("Failed to update definitions from defs file \"%s\" ", extensionDefsFile)));
287 from toscaparser.common.exception import ValidationIssueCollector
288 from toscaparser.common.exception import ValidationError
289 from toscaparser.extensions.exttools import ExtTools
290 import org.onap.sdc.toscaparser.api.utils.yamlparser
292 log = logging.getLogger('tosca')
295 class EntityType(object):
296 '''Base class for TOSCA elements.'''
298 SECTIONS = (DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS,
299 INTERFACES, CAPABILITIES, TYPE, ARTIFACTS) = \
300 ('derived_from', 'properties', 'attributes', 'requirements',
301 'interfaces', 'capabilities', 'type', 'artifacts')
303 TOSCA_DEF_SECTIONS = ['node_types', 'data_types', 'artifact_types',
304 'group_types', 'relationship_types',
305 'capability_types', 'interface_types',
308 '''TOSCA definition file.'''
309 TOSCA_DEF_FILE = os.path.join(
310 os.path.dirname(os.path.abspath(__file__)),
311 "TOSCA_definition_1_0.yaml")
313 loader = toscaparser.utils.yamlparser.load_yaml
315 TOSCA_DEF_LOAD_AS_IS = loader(TOSCA_DEF_FILE)
317 # Map of definition with pre-loaded values of TOSCA_DEF_FILE_SECTIONS
319 for section in TOSCA_DEF_SECTIONS:
320 if section in TOSCA_DEF_LOAD_AS_IS.keys():
321 value = TOSCA_DEF_LOAD_AS_IS[section]
322 for key in value.keys():
323 TOSCA_DEF[key] = value[key]
325 RELATIONSHIP_TYPE = (DEPENDSON, HOSTEDON, CONNECTSTO, ATTACHESTO,
326 LINKSTO, BINDSTO) = \
327 ('tosca.relationships.DependsOn',
328 'tosca.relationships.HostedOn',
329 'tosca.relationships.ConnectsTo',
330 'tosca.relationships.AttachesTo',
331 'tosca.relationships.network.LinksTo',
332 'tosca.relationships.network.BindsTo')
334 NODE_PREFIX = 'tosca.nodes.'
335 RELATIONSHIP_PREFIX = 'tosca.relationships.'
336 CAPABILITY_PREFIX = 'tosca.capabilities.'
337 INTERFACE_PREFIX = 'tosca.interfaces.'
338 ARTIFACT_PREFIX = 'tosca.artifacts.'
339 POLICY_PREFIX = 'tosca.policies.'
340 GROUP_PREFIX = 'tosca.groups.'
341 # currently the data types are defined only for network
342 # but may have changes in the future.
343 DATATYPE_PREFIX = 'tosca.datatypes.'
344 DATATYPE_NETWORK_PREFIX = DATATYPE_PREFIX + 'network.'
347 def derived_from(self, defs):
348 '''Return a type this type is derived from.'''
349 return self.entity_value(defs, 'derived_from')
351 def is_derived_from(self, type_str):
352 '''Check if object inherits from the given type.
354 Returns true if this object is derived from 'type_str'.
359 elif self.type == type_str:
361 elif self.parent_type:
362 return self.parent_type.is_derived_from(type_str)
366 def entity_value(self, defs, key):
370 def get_value(self, ndtype, defs=None, parent=None):
373 if not hasattr(self, 'defs'):
377 # copy the value to avoid that next operations add items in the
379 value = copy.copy(defs[ndtype])
385 # get the parent value
386 parent_value = p.defs[ndtype]
388 if isinstance(value, dict):
389 for k, v in parent_value.items():
390 if k not in value.keys():
392 if isinstance(value, list):
393 for p_value in parent_value:
394 if p_value not in value:
395 value.append(p_value)
397 value = copy.copy(parent_value)
401 def get_definition(self, ndtype):
403 if not hasattr(self, 'defs'):
405 ValidationIssueCollector.appendException(
406 ValidationError(message="defs is " + str(defs)))
409 if defs is not None and ndtype in defs:
413 inherited = p.get_definition(ndtype)
415 inherited = dict(inherited)
419 inherited.update(value)
420 value.update(inherited)
424 def update_definitions(version):
425 exttools = ExtTools()
426 extension_defs_file = exttools.get_defs_file(version)
427 loader = toscaparser.utils.yamlparser.load_yaml
428 nfv_def_file = loader(extension_defs_file)
430 for section in EntityType.TOSCA_DEF_SECTIONS:
431 if section in nfv_def_file.keys():
432 value = nfv_def_file[section]
433 for key in value.keys():
434 nfv_def[key] = value[key]
435 EntityType.TOSCA_DEF.update(nfv_def)