From 9220c7ac558a42f65c2df48337dfc8f03d6b129c Mon Sep 17 00:00:00 2001 From: "Manzon, Inna (im453s)" Date: Thu, 29 Nov 2018 17:26:34 +0200 Subject: Retrieve leaf property value by path Change-Id: I556f6bfaa3d7c96f9d1f26ae0fcba199d23800c7 Issue-ID: SDC-1982 Signed-off-by: Manzon, Inna (im453s) --- pom.xml | 2 +- .../onap/sdc/toscaparser/api/EntityTemplate.java | 19 +- .../java/org/onap/sdc/toscaparser/api/Group.java | 12 +- .../org/onap/sdc/toscaparser/api/NodeTemplate.java | 16 +- .../java/org/onap/sdc/toscaparser/api/Policy.java | 14 +- .../org/onap/sdc/toscaparser/api/Property.java | 212 ++++++++++++++++++++- .../sdc/toscaparser/api/RelationshipTemplate.java | 12 +- .../onap/sdc/toscaparser/api/TopologyTemplate.java | 41 ++-- .../api/elements/constraints/Schema.java | 10 + .../onap/sdc/toscaparser/api/JToscaImportTest.java | 33 ++++ .../api/elements/CalculatePropertyByPathTest.java | 147 ++++++++++++++ .../service-NetworkCloudVnfServiceMock-csar.csar | Bin 0 -> 60223 bytes version.properties | 2 +- 13 files changed, 474 insertions(+), 46 deletions(-) create mode 100644 src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java create mode 100644 src/test/resources/csars/service-NetworkCloudVnfServiceMock-csar.csar diff --git a/pom.xml b/pom.xml index 8e6c0f8..c75ce9b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.onap.sdc.jtosca jtosca - 1.4.9-SNAPSHOT + 1.4.10-SNAPSHOT sdc-jtosca diff --git a/src/main/java/org/onap/sdc/toscaparser/api/EntityTemplate.java b/src/main/java/org/onap/sdc/toscaparser/api/EntityTemplate.java index 637329e..2178be3 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/EntityTemplate.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/EntityTemplate.java @@ -5,6 +5,7 @@ import org.onap.sdc.toscaparser.api.elements.*; import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; @@ -51,16 +52,27 @@ public abstract class EntityTemplate { private ArrayList _requirements; private ArrayList _capabilities; + @Nullable + private NodeTemplate _parentNodeTemplate; + // dummy constructor for subclasses that don't want super public EntityTemplate() { return; } + public EntityTemplate(String _name, + LinkedHashMap _template, + String _entityName, + LinkedHashMap _customDef) { + this(_name, _template, _entityName, _customDef, null); + } + @SuppressWarnings("unchecked") public EntityTemplate(String _name, LinkedHashMap _template, String _entityName, - LinkedHashMap _customDef) { + LinkedHashMap _customDef, + NodeTemplate parentNodeTemplate) { name = _name; entityTpl = _template; customDef = _customDef; @@ -111,8 +123,13 @@ public abstract class EntityTemplate { _interfaces = null; _requirements = null; _capabilities = null; + _parentNodeTemplate = parentNodeTemplate; } + public NodeTemplate getParentNodeTemplate() { + return _parentNodeTemplate; + } + public String getType() { if(typeDefinition != null) { String clType = typeDefinition.getClass().getSimpleName(); diff --git a/src/main/java/org/onap/sdc/toscaparser/api/Group.java b/src/main/java/org/onap/sdc/toscaparser/api/Group.java index de031e6..15ddfb1 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/Group.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/Group.java @@ -25,12 +25,18 @@ public class Group extends EntityTemplate { ArrayList memberNodes; LinkedHashMap customDef; Metadata metaData; - + + + public Group(String _name, LinkedHashMap _templates, + ArrayList _memberNodes, + LinkedHashMap _customDef){ + this(_name, _templates, _memberNodes, _customDef, null); + } public Group(String _name, LinkedHashMap _templates, ArrayList _memberNodes, - LinkedHashMap _customDef) { - super(_name, _templates, "group_type", _customDef); + LinkedHashMap _customDef, NodeTemplate parentNodeTemplate) { + super(_name, _templates, "group_type", _customDef, parentNodeTemplate); name = _name; tpl = _templates; diff --git a/src/main/java/org/onap/sdc/toscaparser/api/NodeTemplate.java b/src/main/java/org/onap/sdc/toscaparser/api/NodeTemplate.java index 270e908..eaa650b 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/NodeTemplate.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/NodeTemplate.java @@ -29,15 +29,25 @@ public class NodeTemplate extends EntityTemplate { private static final String METADATA = "metadata"; + public NodeTemplate(String name, + LinkedHashMap ntnodeTemplates, + LinkedHashMap ntcustomDef, + ArrayList ntavailableRelTpls, + LinkedHashMap ntavailableRelTypes) { + this( name, ntnodeTemplates, ntcustomDef, ntavailableRelTpls, + ntavailableRelTypes, null); + } @SuppressWarnings("unchecked") public NodeTemplate(String name, LinkedHashMap ntnodeTemplates, LinkedHashMap ntcustomDef, ArrayList ntavailableRelTpls, - LinkedHashMap ntavailableRelTypes) { + LinkedHashMap ntavailableRelTypes, + NodeTemplate parentNodeTemplate) { - super(name, (LinkedHashMap)ntnodeTemplates.get(name), "node_type", ntcustomDef); + super(name, (LinkedHashMap)ntnodeTemplates.get(name), + "node_type", ntcustomDef, parentNodeTemplate); templates = ntnodeTemplates; _validateFields((LinkedHashMap)templates.get(name)); @@ -209,7 +219,7 @@ public class NodeTemplate extends EntityTemplate { LinkedHashMap req = new LinkedHashMap<>(); req.put("relationship", CopyUtils.copyLhmOrAl(requirement.getRelationship())); req.put("type",rtype); - RelationshipTemplate tpl = new RelationshipTemplate(req, rtype, customDef, this, source); + RelationshipTemplate tpl = new RelationshipTemplate(req, rtype, customDef, this, source, getParentNodeTemplate()); relationshipTpl.add(tpl); } diff --git a/src/main/java/org/onap/sdc/toscaparser/api/Policy.java b/src/main/java/org/onap/sdc/toscaparser/api/Policy.java index 9eaacfc..5945532 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/Policy.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/Policy.java @@ -28,14 +28,22 @@ public class Policy extends EntityTemplate { String targetsType; ArrayList triggers; LinkedHashMap properties; - + public Policy(String _name, LinkedHashMap _policy, -// ArrayList targetObjects, ArrayList targetObjects, String _targetsType, LinkedHashMap _customDef) { - super(_name,_policy,"policy_type",_customDef); + this(_name, _policy, targetObjects, _targetsType, _customDef, null); + } + + public Policy(String _name, + LinkedHashMap _policy, +// ArrayList targetObjects, + ArrayList targetObjects, + String _targetsType, + LinkedHashMap _customDef, NodeTemplate parentNodeTemplate) { + super(_name,_policy,"policy_type",_customDef, parentNodeTemplate); if(_policy.get(METADATA) != null) { LinkedHashMap metadataMap = (LinkedHashMap)_policy.get(METADATA); diff --git a/src/main/java/org/onap/sdc/toscaparser/api/Property.java b/src/main/java/org/onap/sdc/toscaparser/api/Property.java index 6d05af0..0ef9dd1 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/Property.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/Property.java @@ -1,29 +1,38 @@ package org.onap.sdc.toscaparser.api; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.Map; - -import org.onap.sdc.toscaparser.api.elements.PropertyDef; -import org.onap.sdc.toscaparser.api.elements.StatefulEntityType; +import com.google.common.collect.Lists; import org.onap.sdc.toscaparser.api.elements.constraints.Constraint; import org.onap.sdc.toscaparser.api.elements.constraints.Schema; import org.onap.sdc.toscaparser.api.functions.Function; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; public class Property { // TOSCA built-in Property type + private static final Logger logger = LoggerFactory.getLogger(Property.class.getName()); private static final String TYPE = "type"; private static final String REQUIRED = "required"; private static final String DESCRIPTION = "description"; private static final String DEFAULT = "default"; private static final String CONSTRAINTS = "constraints"; - + private static String ENTRY_SCHEMA = "entry_schema"; + private static String DATA_TYPE = "datatypes"; + private static final String[] PROPERTY_KEYS = { TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS}; private static final String ENTRYTYPE = "type"; private static final String ENTRYPROPERTIES = "properties"; + private static final String PATH_DELIMITER = "#"; private static final String[] ENTRY_SCHEMA_KEYS = { ENTRYTYPE, ENTRYPROPERTIES}; @@ -117,6 +126,195 @@ public class Property { ", customDef=" + customDef + '}'; } + + /** + * Retrieves property value as list of strings if
+ * - the value is simple
+ * - the value is list of simple values
+ * - the provided path refers to a simple property inside a data type
+ * @param propertyPath valid name of property for search.
+ * If a name refers to a simple field inside a datatype, the property name should be defined with # delimiter.
+ * + * @return List of property values. If not found, empty list will be returned.
+ * If property value is a list either of simple fields or of simple fields inside a datatype, all values from the list should be returned + */ + public List getLeafPropertyValue(String propertyPath) { + List propertyValueList = Collections.emptyList(); + + if (logger.isDebugEnabled()) { + logger.debug("getLeafPropertyValue=> A new request: propertyPath: {}, value: {}", propertyPath, getValue()); + } + if (propertyPath == null || getValue() == null || + //if entry_schema disappears, it is datatype, + // otherwise it is map of simple types - should be ignored + isValueMapOfSimpleTypes()) { + logger.error("It is a wrong request - ignoring! propertyPath: {}, value: {}", propertyPath, getValue()); + return propertyValueList; + } + String[] path = propertyPath.split(PATH_DELIMITER); + + if (Schema.isRequestedTypeSimple(getPropertyTypeByPath(path))) { + //the internal property type in the path is either simple or list of simple types + if (isValueInsideDataType()) { + if (logger.isDebugEnabled()) { + logger.debug("The requested is an internal simple property inside of a data type"); + } + //requested value is an internal simple property inside of a data type + propertyValueList = getSimplePropertyValueForComplexType(path); + } + else { + if (logger.isDebugEnabled()) { + logger.debug("The requested property has simple type or list of simple types"); + } + //the requested property is simple type or list of simple types + propertyValueList = getSimplePropertyValueForSimpleType(); + } + } + return propertyValueList; + } + + private boolean isValueMapOfSimpleTypes() { + if (getValue() instanceof Map && getEntrySchema() != null) { + logger.warn("This property value is a map of simple types"); + return true; + } + return false; + } + + private boolean isValueInsideDataType() { + //value is either a list of values for data type + //or data type + return (Schema.LIST.equals(getType()) && isDataTypeInEntrySchema()) + || (getEntrySchema() == null && getType().contains(DATA_TYPE)); + } + + private Object getSimpleValueFromComplexObject(Object current, String[] path) { + if (current == null) { + return null; + } + int index = 0; + + if (path.length > index) { + for (int i = index; i < path.length; i++) { + if (current instanceof Map) { + current = ((Map) current).get(path[i]); + } else if (current instanceof List) { + current = ((List) current).get(0); + i--; + } + else { + return null; + } + } + } + if (current != null) { + return current; + } + return null; + } + + private List getSimplePropertyValueForSimpleType() { + if (getValue() instanceof List || getValue() instanceof Map) { + return getSimplePropertyValueForComplexType(null); + } + return Lists.newArrayList(String.valueOf(value)); + } + + private List getSimplePropertyValueForComplexType(String[] path) { + if (getValue() instanceof List ) { + return ((List) getValue()).stream() + .map(v -> { + if (path != null) { + return getSimpleValueFromComplexObject(v, path); + } else { + return v; + } + }) + //it might be null when get_input can't be resolved + // e.g.: + // - get_input has two parameters: 1. list and 2. index in this list + //and list has no value + // - neither value no default is defined for get_input + .filter(Objects::nonNull) + .map(String::valueOf) + .collect(Collectors.toList()); + } + //it is data type + List valueList = Lists.newArrayList(); + String valueString = String.valueOf(getSimpleValueFromComplexObject(getValue(), path)); + if (Objects.nonNull(valueString)) { + valueList.add(valueString); + } + return valueList; + } + + private String getPropertyTypeByPath(String[] path) { + String propertyType = calculatePropertyType(); + + if (path.length > 0 && !path[0].isEmpty()) { + return getInternalPropertyType(propertyType, path, 0); + } + return propertyType; + } + + private String calculatePropertyType() { + String propertyType = getType(); + if (Schema.LIST.equals(propertyType)) { + //if it is list, return entry schema type + return (String)getEntrySchema().get(ENTRYTYPE); + } + return propertyType; + } + + private String calculatePropertyType(LinkedHashMap property) { + String type = (String) property.get(TYPE); + if (Schema.LIST.equals(type)) { + //it might be a data type + return getEntrySchemaType(property); + } + return type; + } + + private String getInternalPropertyType(String dataTypeName, String[] path, int index) { + if (path.length > index) { + LinkedHashMap complexProperty = (LinkedHashMap)customDef.get(dataTypeName); + if (complexProperty != null) { + LinkedHashMap dataTypeProperties = (LinkedHashMap) complexProperty.get(ENTRYPROPERTIES); + return getPropertyTypeFromCustomDefDeeply(path, index, dataTypeProperties); + } + } + //stop searching - seems as wrong flow: the path is finished but the value is not found yet + return null; + } + + private String getEntrySchemaType(LinkedHashMap property) { + LinkedHashMap entrySchema = (LinkedHashMap)property.get(ENTRY_SCHEMA); + if (entrySchema != null) { + return (String) entrySchema.get(TYPE); + } + return null; + } + + private String getPropertyTypeFromCustomDefDeeply(String[] path, int index, LinkedHashMap properties) { + if (properties != null) { + LinkedHashMap foundProperty = (LinkedHashMap) (properties).get(path[index]); + if (foundProperty != null) { + String propertyType = calculatePropertyType(foundProperty); + if (propertyType == null || index == path.length - 1){ + return propertyType; + } + return getInternalPropertyType(propertyType, path, index + 1); + } + } + return null; + } + + private boolean isDataTypeInEntrySchema() { + String entrySchemaType = (String)getEntrySchema().get(ENTRYTYPE); + return entrySchemaType != null && entrySchemaType.contains(DATA_TYPE); + } + + } /*python diff --git a/src/main/java/org/onap/sdc/toscaparser/api/RelationshipTemplate.java b/src/main/java/org/onap/sdc/toscaparser/api/RelationshipTemplate.java index a94caed..79bf83b 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/RelationshipTemplate.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/RelationshipTemplate.java @@ -24,13 +24,21 @@ public class RelationshipTemplate extends EntityTemplate { private NodeTemplate target; private NodeTemplate source; private ArrayList _properties; - + public RelationshipTemplate(LinkedHashMap rtrelationshipTemplate, String rtname, LinkedHashMap rtcustomDef, NodeTemplate rttarget, NodeTemplate rtsource) { - super(rtname,rtrelationshipTemplate,"relationship_type",rtcustomDef); + this(rtrelationshipTemplate, rtname, rtcustomDef, rttarget, rtsource, null); + } + + public RelationshipTemplate(LinkedHashMap rtrelationshipTemplate, + String rtname, + LinkedHashMap rtcustomDef, + NodeTemplate rttarget, + NodeTemplate rtsource, NodeTemplate parentNodeTemplate) { + super(rtname,rtrelationshipTemplate,"relationship_type",rtcustomDef, parentNodeTemplate); name = rtname; target = rttarget; diff --git a/src/main/java/org/onap/sdc/toscaparser/api/TopologyTemplate.java b/src/main/java/org/onap/sdc/toscaparser/api/TopologyTemplate.java index 4c9a53d..0b1dfcd 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/TopologyTemplate.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/TopologyTemplate.java @@ -71,6 +71,7 @@ public class TopologyTemplate { description = _tplDescription(); inputs = _inputs(); relationshipTemplates =_relationshipTemplates(); + //todo: pass subMappedNodeTemplate to ET constractor nodeTemplates = _nodeTemplates(); outputs = _outputs(); if(nodeTemplates != null) { @@ -128,7 +129,8 @@ public class TopologyTemplate { tpls, customDefs, relationshipTemplates, - relTypes); + relTypes, + subMappedNodeTemplate); if(tpl.getTypeDefinition() != null) { boolean b = NodeType.TOSCA_DEF.get(tpl.getType()) != null; if(b || (tpl.getCustomDef() != null && !tpl.getCustomDef().isEmpty())) { @@ -148,7 +150,7 @@ public class TopologyTemplate { if(tpls != null) { for(String name: tpls.keySet()) { RelationshipTemplate tpl = new RelationshipTemplate( - (LinkedHashMap)tpls.get(name),name,customDefs,null,null); + (LinkedHashMap)tpls.get(name),name,customDefs,null,null, subMappedNodeTemplate); alRelationshipTemplates.add(tpl); } @@ -216,7 +218,8 @@ public class TopologyTemplate { policyTpl, targetObjects, targetsType, - customDefs); + customDefs, + subMappedNodeTemplate); alPolicies.add(policyObj); } return alPolicies; @@ -244,7 +247,7 @@ public class TopologyTemplate { Group group = new Group(groupName, groupTpl, memberNodes, - customDefs); + customDefs, subMappedNodeTemplate); groups.add(group); } return groups; @@ -314,9 +317,7 @@ public class TopologyTemplate { if(tpl.get(INPUTS) != null) { return (LinkedHashMap)tpl.get(INPUTS); } - else { - return new LinkedHashMap(); - } + return new LinkedHashMap(); } @SuppressWarnings("unchecked") @@ -329,19 +330,15 @@ public class TopologyTemplate { if(tpl.get(RELATIONSHIP_TEMPLATES) != null) { return (LinkedHashMap)tpl.get(RELATIONSHIP_TEMPLATES); } - else { - return new LinkedHashMap(); - } + return new LinkedHashMap(); } @SuppressWarnings("unchecked") private LinkedHashMap _tplOutputs() { - if(tpl.get(OUTPUTS) != null) { - return (LinkedHashMap)tpl.get(OUTPUTS); - } - else { - return new LinkedHashMap(); - } + if(tpl.get(OUTPUTS) != null) { + return (LinkedHashMap)tpl.get(OUTPUTS); + } + return new LinkedHashMap(); } @SuppressWarnings("unchecked") @@ -349,9 +346,7 @@ public class TopologyTemplate { if(tpl.get(SUBSTITUTION_MAPPINGS) != null) { return (LinkedHashMap)tpl.get(SUBSTITUTION_MAPPINGS); } - else { - return new LinkedHashMap(); - } + return new LinkedHashMap(); } @SuppressWarnings("unchecked") @@ -359,9 +354,7 @@ public class TopologyTemplate { if(tpl.get(GROUPS) != null) { return (LinkedHashMap)tpl.get(GROUPS); } - else { - return new LinkedHashMap(); - } + return new LinkedHashMap(); } @SuppressWarnings("unchecked") @@ -369,9 +362,7 @@ public class TopologyTemplate { if(tpl.get(POLICIES) != null) { return (LinkedHashMap)tpl.get(POLICIES); } - else { - return new LinkedHashMap<>(); - } + return new LinkedHashMap<>(); } private void _validateField() { diff --git a/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Schema.java b/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Schema.java index 73a63ef..c0ed6bc 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Schema.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Schema.java @@ -4,6 +4,7 @@ import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue; import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -42,6 +43,11 @@ public class Schema { INTEGER, STRING, BOOLEAN, FLOAT, RANGE,NUMBER, TIMESTAMP, LIST, MAP, SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME, VERSION, PORTDEF, PORTSPEC, JSON}; + + public static final String SIMPLE_PROPERTY_TYPES[] = { + INTEGER, STRING, BOOLEAN, FLOAT, RANGE,NUMBER, TIMESTAMP, + SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME, + VERSION}; @SuppressWarnings("unused") private static final String SCALAR_UNIT_SIZE_DEFAULT = "B"; @@ -107,6 +113,10 @@ public class Schema { return (String)schema.getOrDefault(STATUS,""); } + public static boolean isRequestedTypeSimple(String type) { + return Arrays.stream(SIMPLE_PROPERTY_TYPES).anyMatch(t->t.equals(type)); + } + @SuppressWarnings("unchecked") public ArrayList getConstraints() { if(constraintsList.size() == 0) { diff --git a/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java b/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java index 8e587a9..c660153 100644 --- a/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java +++ b/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java @@ -13,6 +13,7 @@ import java.util.Optional; import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -111,6 +112,38 @@ public class JToscaImportTest { assertNull(nodeTemplate.getPropertyValueFromTemplatesByName("test")); } + @Test + public void testGetParentNodeTemplateTest() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0); + //parent of this VF is service (null) + assertNull(nodeTemplate.getParentNodeTemplate()); + List children = nodeTemplate.getSubMappingToscaTemplate().getNodeTemplates(); + assertFalse(children.isEmpty()); + NodeTemplate cVFC = children.get(4); + //parent is the VF above + assertEquals("2017-488_ADIOD-vPE 0", cVFC.getParentNodeTemplate().getName()); + List children1 = cVFC.getSubMappingToscaTemplate().getNodeTemplates(); + assertFalse(children1.isEmpty()); + //parent is the CVFC above + assertEquals(cVFC, children1.get(0).getParentNodeTemplate()); + +/* + + TopologyTemplate tt = nodeTemplate.getOriginComponentTemplate(); + List groups = tt.getGroups(); + List policies = tt.getPolicies(); + + TopologyTemplate tt1 = cVFC.getOriginComponentTemplate(); + groups = tt.getGroups(); + policies = tt.getPolicies(); +*/ + + } + @Test public void testNullValueHasNoNullPointerException() throws JToscaException { diff --git a/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java b/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java new file mode 100644 index 0000000..59c8445 --- /dev/null +++ b/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java @@ -0,0 +1,147 @@ +package org.onap.sdc.toscaparser.api.elements; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.sdc.toscaparser.api.JToscaImportTest; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.ToscaTemplate; +import org.onap.sdc.toscaparser.api.common.JToscaException; + +import java.io.File; +import java.net.URL; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CalculatePropertyByPathTest { + private static ToscaTemplate toscaTemplate; + + @BeforeClass + public static void setUpClass() throws JToscaException { + URL scarUrl = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar"); + if (scarUrl != null) { + File file = new File(scarUrl.getFile()); + toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + } + + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNotEmpty() throws JToscaException { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("related_networks"); + List propertyValueList = property.getLeafPropertyValue("related_network_role"); + assertEquals(3, propertyValueList.size()); + assertTrue(propertyValueList.contains("cor_direct_2")); + assertTrue(propertyValueList.contains("sgi_direct_2")); + assertTrue(propertyValueList.contains("int_imbl_2")); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("exCP_naming"); + List propertyValueList = property.getLeafPropertyValue(""); + assertTrue(propertyValueList.isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasSimpleTypeAndValueAsGetInputIsNotResolvedCorrectlyAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("network"); + List propertyValueList = property.getLeafPropertyValue(""); + assertTrue(propertyValueList.isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasSimpleTypeAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("subinterface_indicator"); + List propertyValueList = property.getLeafPropertyValue(""); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + + @Test + public void testGetPropertyWhenPropertyHasDataTypeAndPathIsNotEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(2); //testVM_testVM_OVS_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_version"); + assertEquals(1, propertyValueList.size()); + assertEquals("4", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNull() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(2); //testVM_testVM_OVS_port + + Property property = cp.getProperties().get("ip_requirements"); + assertTrue(property.getLeafPropertyValue(null).isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsComplex() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required"); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsWrong() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required_1"); + assertEquals(0, propertyValueList.size()); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndComplexPath() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("mac_requirements"); + List propertyValueList = property.getLeafPropertyValue("mac_count_required#is_required"); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndSimplePath() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("mac_requirements"); + List propertyValueList = property.getLeafPropertyValue("mac_count_required"); + assertEquals(0, propertyValueList.size()); + } +} diff --git a/src/test/resources/csars/service-NetworkCloudVnfServiceMock-csar.csar b/src/test/resources/csars/service-NetworkCloudVnfServiceMock-csar.csar new file mode 100644 index 0000000..aabf83c Binary files /dev/null and b/src/test/resources/csars/service-NetworkCloudVnfServiceMock-csar.csar differ diff --git a/version.properties b/version.properties index 61bae76..3a44f58 100644 --- a/version.properties +++ b/version.properties @@ -5,7 +5,7 @@ major=1 minor=4 -patch=9 +patch=10 base_version=${major}.${minor}.${patch} -- cgit 1.2.3-korg