diff options
Diffstat (limited to 'src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java')
-rw-r--r-- | src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java | 354 |
1 files changed, 176 insertions, 178 deletions
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java b/src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java index 62f51d2..efc6ac9 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/elements/EntityType.java @@ -7,9 +7,9 @@ * 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. @@ -36,189 +36,189 @@ import org.yaml.snakeyaml.Yaml; public class EntityType { - private static Logger log = LoggerFactory.getLogger(EntityType.class.getName()); - - private static final String TOSCA_DEFINITION_1_0_YAML = "TOSCA_definition_1_0.yaml"; - protected static final String DERIVED_FROM = "derived_from"; - protected static final String PROPERTIES = "properties"; - protected static final String ATTRIBUTES = "attributes"; - protected static final String REQUIREMENTS = "requirements"; - protected static final String INTERFACES = "interfaces"; - protected static final String CAPABILITIES = "capabilities"; - protected static final String TYPE = "type"; - protected static final String ARTIFACTS = "artifacts"; - - @SuppressWarnings("unused") - private static final String SECTIONS[] = { - DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS, + private static Logger log = LoggerFactory.getLogger(EntityType.class.getName()); + + private static final String TOSCA_DEFINITION_1_0_YAML = "TOSCA_definition_1_0.yaml"; + protected static final String DERIVED_FROM = "derived_from"; + protected static final String PROPERTIES = "properties"; + protected static final String ATTRIBUTES = "attributes"; + protected static final String REQUIREMENTS = "requirements"; + protected static final String INTERFACES = "interfaces"; + protected static final String CAPABILITIES = "capabilities"; + protected static final String TYPE = "type"; + protected static final String ARTIFACTS = "artifacts"; + + @SuppressWarnings("unused") + private static final String SECTIONS[] = { + DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS, INTERFACES, CAPABILITIES, TYPE, ARTIFACTS - }; + }; - public static final String TOSCA_DEF_SECTIONS[] = { - "node_types", "data_types", "artifact_types", - "group_types", "relationship_types", - "capability_types", "interface_types", - "policy_types"}; + public static final String TOSCA_DEF_SECTIONS[] = { + "node_types", "data_types", "artifact_types", + "group_types", "relationship_types", + "capability_types", "interface_types", + "policy_types"}; // TOSCA definition file - //private final static String path = EntityType.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - - //private final static String path = EntityType.class.getClassLoader().getResource("TOSCA_definition_1_0.yaml").getFile(); + //private final static String path = EntityType.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + + //private final static String path = EntityType.class.getClassLoader().getResource("TOSCA_definition_1_0.yaml").getFile(); //private final static String TOSCA_DEF_FILE = EntityType.class.getClassLoader().getResourceAsStream("TOSCA_definition_1_0.yaml"); - - private static LinkedHashMap<String,Object> TOSCA_DEF_LOAD_AS_IS = loadTdf(); - - //EntityType.class.getClassLoader().getResourceAsStream("TOSCA_definition_1_0.yaml"); - - @SuppressWarnings("unchecked") - private static LinkedHashMap<String,Object> loadTdf() { - String toscaDefLocation = EntityType.class.getClassLoader().getResource(TOSCA_DEFINITION_1_0_YAML).getFile(); - InputStream input = EntityType.class.getClassLoader().getResourceAsStream(TOSCA_DEFINITION_1_0_YAML); - if (input == null){ - log.error("EntityType - loadTdf - Couldn't load TOSCA_DEF_FILE {}", toscaDefLocation); - } - Yaml yaml = new Yaml(); - Object loaded = yaml.load(input); - //@SuppressWarnings("unchecked") - return (LinkedHashMap<String,Object>) loaded; - } + + private static LinkedHashMap<String, Object> TOSCA_DEF_LOAD_AS_IS = loadTdf(); + + //EntityType.class.getClassLoader().getResourceAsStream("TOSCA_definition_1_0.yaml"); + + @SuppressWarnings("unchecked") + private static LinkedHashMap<String, Object> loadTdf() { + String toscaDefLocation = EntityType.class.getClassLoader().getResource(TOSCA_DEFINITION_1_0_YAML).getFile(); + InputStream input = EntityType.class.getClassLoader().getResourceAsStream(TOSCA_DEFINITION_1_0_YAML); + if (input == null) { + log.error("EntityType - loadTdf - Couldn't load TOSCA_DEF_FILE {}", toscaDefLocation); + } + Yaml yaml = new Yaml(); + Object loaded = yaml.load(input); + //@SuppressWarnings("unchecked") + return (LinkedHashMap<String, Object>) loaded; + } // Map of definition with pre-loaded values of TOSCA_DEF_FILE_SECTIONS - public static LinkedHashMap<String,Object> TOSCA_DEF; - static { - TOSCA_DEF = new LinkedHashMap<String,Object>(); - for(String section: TOSCA_DEF_SECTIONS) { - @SuppressWarnings("unchecked") - LinkedHashMap<String,Object> value = (LinkedHashMap<String,Object>)TOSCA_DEF_LOAD_AS_IS.get(section); - if(value != null) { - for(String key: value.keySet()) { - TOSCA_DEF.put(key, value.get(key)); - } - } + public static LinkedHashMap<String, Object> TOSCA_DEF; + + static { + TOSCA_DEF = new LinkedHashMap<String, Object>(); + for (String section : TOSCA_DEF_SECTIONS) { + @SuppressWarnings("unchecked") + LinkedHashMap<String, Object> value = (LinkedHashMap<String, Object>) TOSCA_DEF_LOAD_AS_IS.get(section); + if (value != null) { + for (String key : value.keySet()) { + TOSCA_DEF.put(key, value.get(key)); + } + } } - } - - public static final String DEPENDSON = "tosca.relationships.DependsOn"; - public static final String HOSTEDON = "tosca.relationships.HostedOn"; - public static final String CONNECTSTO = "tosca.relationships.ConnectsTo"; - public static final String ATTACHESTO = "tosca.relationships.AttachesTo"; - public static final String LINKSTO = "tosca.relationships.network.LinksTo"; - public static final String BINDSTO = "tosca.relationships.network.BindsTo"; - - public static final String RELATIONSHIP_TYPE[] = { - "tosca.relationships.DependsOn", - "tosca.relationships.HostedOn", - "tosca.relationships.ConnectsTo", - "tosca.relationships.AttachesTo", - "tosca.relationships.network.LinksTo", - "tosca.relationships.network.BindsTo"}; - - public static final String NODE_PREFIX = "tosca.nodes."; - public static final String RELATIONSHIP_PREFIX = "tosca.relationships."; - public static final String CAPABILITY_PREFIX = "tosca.capabilities."; - public static final String INTERFACE_PREFIX = "tosca.interfaces."; - public static final String ARTIFACT_PREFIX = "tosca.artifacts."; - public static final String POLICY_PREFIX = "tosca.policies."; - public static final String GROUP_PREFIX = "tosca.groups."; - //currently the data types are defined only for network - // but may have changes in the future. - public static final String DATATYPE_PREFIX = "tosca.datatypes."; - public static final String DATATYPE_NETWORK_PREFIX = DATATYPE_PREFIX + "network."; - public static final String TOSCA = "tosca"; - - protected String type; - protected LinkedHashMap<String,Object> defs = null; - public Object getParentType() { return null; } - - public String derivedFrom(LinkedHashMap<String,Object> defs) { + } + + public static final String DEPENDSON = "tosca.relationships.DependsOn"; + public static final String HOSTEDON = "tosca.relationships.HostedOn"; + public static final String CONNECTSTO = "tosca.relationships.ConnectsTo"; + public static final String ATTACHESTO = "tosca.relationships.AttachesTo"; + public static final String LINKSTO = "tosca.relationships.network.LinksTo"; + public static final String BINDSTO = "tosca.relationships.network.BindsTo"; + + public static final String RELATIONSHIP_TYPE[] = { + "tosca.relationships.DependsOn", + "tosca.relationships.HostedOn", + "tosca.relationships.ConnectsTo", + "tosca.relationships.AttachesTo", + "tosca.relationships.network.LinksTo", + "tosca.relationships.network.BindsTo"}; + + public static final String NODE_PREFIX = "tosca.nodes."; + public static final String RELATIONSHIP_PREFIX = "tosca.relationships."; + public static final String CAPABILITY_PREFIX = "tosca.capabilities."; + public static final String INTERFACE_PREFIX = "tosca.interfaces."; + public static final String ARTIFACT_PREFIX = "tosca.artifacts."; + public static final String POLICY_PREFIX = "tosca.policies."; + public static final String GROUP_PREFIX = "tosca.groups."; + //currently the data types are defined only for network + // but may have changes in the future. + public static final String DATATYPE_PREFIX = "tosca.datatypes."; + public static final String DATATYPE_NETWORK_PREFIX = DATATYPE_PREFIX + "network."; + public static final String TOSCA = "tosca"; + + protected String type; + protected LinkedHashMap<String, Object> defs = null; + + public Object getParentType() { + return null; + } + + public String derivedFrom(LinkedHashMap<String, Object> defs) { // Return a type this type is derived from - return (String)entityValue(defs, "derived_from"); + return (String) entityValue(defs, "derived_from"); } public boolean isDerivedFrom(String type_str) { // Check if object inherits from the given type // Returns true if this object is derived from 'type_str' // False otherwise. - if(type == null || this.type.isEmpty()) { + if (type == null || this.type.isEmpty()) { return false; - } - else if(type == type_str) { + } else if (type == type_str) { return true; - } - else if(getParentType() != null) { - return ((EntityType)getParentType()).isDerivedFrom(type_str); - } - else { + } else if (getParentType() != null) { + return ((EntityType) getParentType()).isDerivedFrom(type_str); + } else { return false; } } - public Object entityValue(LinkedHashMap<String,Object> defs, String key) { - if(defs != null) { - return defs.get(key); - } - return null; + public Object entityValue(LinkedHashMap<String, Object> defs, String key) { + if (defs != null) { + return defs.get(key); + } + return null; } @SuppressWarnings("unchecked") - public Object getValue(String ndtype, LinkedHashMap<String,Object> _defs, boolean parent) { + public Object getValue(String ndtype, LinkedHashMap<String, Object> _defs, boolean parent) { Object value = null; - if(_defs == null) { - if(defs == null) { + if (_defs == null) { + if (defs == null) { return null; } _defs = this.defs; } - Object defndt = _defs.get(ndtype); - if(defndt != null) { + Object defndt = _defs.get(ndtype); + if (defndt != null) { // copy the value to avoid that next operations add items in the // item definitions //value = copy.copy(defs[ndtype]) - value = CopyUtils.copyLhmOrAl(defndt); + value = CopyUtils.copyLhmOrAl(defndt); } - - if(parent) { + + if (parent) { EntityType p = this; - if(p != null) { - while(p != null) { - if(p.defs != null && p.defs.get(ndtype) != null) { + if (p != null) { + while (p != null) { + if (p.defs != null && p.defs.get(ndtype) != null) { // get the parent value Object parentValue = p.defs.get(ndtype); - if(value != null) { - if(value instanceof LinkedHashMap) { - for(Map.Entry<String,Object> me: ((LinkedHashMap<String,Object>)parentValue).entrySet()) { - String k = me.getKey(); - if(((LinkedHashMap<String,Object>)value).get(k) == null) { - ((LinkedHashMap<String,Object>)value).put(k,me.getValue()); - } - } + if (value != null) { + if (value instanceof LinkedHashMap) { + for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) parentValue).entrySet()) { + String k = me.getKey(); + if (((LinkedHashMap<String, Object>) value).get(k) == null) { + ((LinkedHashMap<String, Object>) value).put(k, me.getValue()); + } + } } - if(value instanceof ArrayList) { - for(Object pValue: (ArrayList<Object>)parentValue) { - if(!((ArrayList<Object>)value).contains(pValue)) { - ((ArrayList<Object>)value).add(pValue); - } - } + if (value instanceof ArrayList) { + for (Object pValue : (ArrayList<Object>) parentValue) { + if (!((ArrayList<Object>) value).contains(pValue)) { + ((ArrayList<Object>) value).add(pValue); + } + } } - } - else { - // value = copy.copy(parent_value) + } else { + // value = copy.copy(parent_value) value = CopyUtils.copyLhmOrAl(parentValue); } - } - p = (EntityType)p.getParentType(); + } + p = (EntityType) p.getParentType(); } } } - - return value; - } + + return value; + } @SuppressWarnings("unchecked") - public Object getDefinition(String ndtype) { + public Object getDefinition(String ndtype) { Object value = null; - LinkedHashMap<String,Object> _defs; + LinkedHashMap<String, Object> _defs; // no point in hasattr, because we have it, and it // doesn't do anything except emit an exception anyway //if not hasattr(self, 'defs'): @@ -228,59 +228,57 @@ public class EntityType { //else: // defs = self.defs _defs = this.defs; - - if(_defs != null && _defs.get(ndtype) != null) { - value = _defs.get(ndtype); + + if (_defs != null && _defs.get(ndtype) != null) { + value = _defs.get(ndtype); } Object p = getParentType(); - if(p != null) { - Object inherited = ((EntityType)p).getDefinition(ndtype); - if(inherited != null) { + if (p != null) { + Object inherited = ((EntityType) p).getDefinition(ndtype); + if (inherited != null) { // inherited = dict(inherited) WTF?!? - if(value == null) { - value = inherited; - } - else { - //????? + if (value == null) { + value = inherited; + } else { + //????? //inherited.update(value) //value.update(inherited) - for(Map.Entry<String,Object> me: ((LinkedHashMap<String,Object>)inherited).entrySet()) { - ((LinkedHashMap<String,Object>)value).put(me.getKey(),me.getValue()); - } - } - } + for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) inherited).entrySet()) { + ((LinkedHashMap<String, Object>) value).put(me.getKey(), me.getValue()); + } + } + } } return value; } - - public static void updateDefinitions(String version) { + + public static void updateDefinitions(String version) { ExtTools exttools = new ExtTools(); String extensionDefsFile = exttools.getDefsFile(version); - try (InputStream input = EntityType.class.getClassLoader().getResourceAsStream(extensionDefsFile);){ - Yaml yaml = new Yaml(); - LinkedHashMap<String,Object> nfvDefFile = (LinkedHashMap<String,Object>)yaml.load(input); - LinkedHashMap<String,Object> nfvDef = new LinkedHashMap<>(); - for(String section: TOSCA_DEF_SECTIONS) { - if(nfvDefFile.get(section) != null) { - LinkedHashMap<String,Object> value = - (LinkedHashMap<String,Object>)nfvDefFile.get(section); - for(String key: value.keySet()) { - nfvDef.put(key, value.get(key)); - } - } - } - TOSCA_DEF.putAll(nfvDef); - } - catch (IOException e) { - log.error("EntityType - updateDefinitions - Failed to update definitions from defs file {}",extensionDefsFile); - log.error("Exception:", e); - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE280", - String.format("Failed to update definitions from defs file \"%s\" ",extensionDefsFile))); - return; - } + try (InputStream input = EntityType.class.getClassLoader().getResourceAsStream(extensionDefsFile);) { + Yaml yaml = new Yaml(); + LinkedHashMap<String, Object> nfvDefFile = (LinkedHashMap<String, Object>) yaml.load(input); + LinkedHashMap<String, Object> nfvDef = new LinkedHashMap<>(); + for (String section : TOSCA_DEF_SECTIONS) { + if (nfvDefFile.get(section) != null) { + LinkedHashMap<String, Object> value = + (LinkedHashMap<String, Object>) nfvDefFile.get(section); + for (String key : value.keySet()) { + nfvDef.put(key, value.get(key)); + } + } + } + TOSCA_DEF.putAll(nfvDef); + } catch (IOException e) { + log.error("EntityType - updateDefinitions - Failed to update definitions from defs file {}", extensionDefsFile); + log.error("Exception:", e); + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE280", + String.format("Failed to update definitions from defs file \"%s\" ", extensionDefsFile))); + return; + } } } |