diff options
author | Manzon, Inna (im453s) <im453s@intl.att.com> | 2018-09-12 18:58:05 +0300 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2018-09-27 12:14:56 +0000 |
commit | a8b7b97e8f4aa088a7e39e06946c425ddd1ba2b4 (patch) | |
tree | 8c9ba44de4e337f67602c3b54776494e2eb047ee /src/main/java/org/onap | |
parent | c244e1a7ede9c936b81b48b9e6e00dff64461e1d (diff) |
Tosca Parser - getPropertyValue and tests
Change-Id: I1baa920033f410f90b2b742c007d917eb6e151ff
Issue-ID: SDC-1757
Signed-off-by: Manzon, Inna (im453s) <im453s@intl.att.com>
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java | 51 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java (renamed from src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java (renamed from src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/enums/PropertySchemaType.java | 69 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java (renamed from src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java) | 4 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java | 199 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/impl/SdcPropertyNames.java | 1 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/impl/SdcToscaParserFactory.java | 1 | ||||
-rw-r--r-- | src/main/java/org/onap/sdc/tosca/parser/utils/PropertyUtils.java | 172 |
9 files changed, 414 insertions, 87 deletions
diff --git a/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java b/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java index cc47db9..8ce4b1b 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java +++ b/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java @@ -24,8 +24,8 @@ import java.util.List; import java.util.Map; import org.apache.commons.lang3.tuple.Pair; -import org.onap.sdc.tosca.parser.impl.SdcTypes; -import org.onap.sdc.tosca.parser.impl.FilterType; +import org.onap.sdc.tosca.parser.enums.SdcTypes; +import org.onap.sdc.tosca.parser.enums.FilterType; import org.onap.sdc.toscaparser.api.*; import org.onap.sdc.toscaparser.api.elements.InterfacesDef; import org.onap.sdc.toscaparser.api.elements.Metadata; @@ -108,7 +108,7 @@ public interface ISdcCsarHelper { * @param pathToPropertyLeafValue - the full path of the required property. * @return the leaf value as Object, or null if there's no such property. It's up to the caller to cast it to a proper type. */ - public Object getNodeTemplatePropertyAsObject(NodeTemplate nodeTemplate, String pathToPropertyLeafValue); + public Object getNodeTemplatePropertyValueAsObject(NodeTemplate nodeTemplate, String pathToPropertyLeafValue); /** * Get any property leaf value for a group definition by full path separated by #. @@ -479,14 +479,14 @@ public interface ISdcCsarHelper { * @param policyTypeName the name of the policy type * @return the list of the policies */ - public List<Policy> getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); + List<Policy> getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); /** * Get all the node templates of the topology template, which are the targets of the policy specified by name * @param policyName the name of the policy * @return the list of the node templates */ - public List<NodeTemplate> getPolicyTargetsFromTopologyTemplate(String policyName); + List<NodeTemplate> getPolicyTargetsFromTopologyTemplate(String policyName); /** * Get all the node templates of the origin component (nested topology template) of node template, which are the targets of the policy specified by name @@ -494,21 +494,21 @@ public interface ISdcCsarHelper { * @param policyName the name of the policy * @return the list of the node templates */ - public List<NodeTemplate> getPolicyTargetsFromOrigin(NodeTemplate nodeTemplate, String policyName); + List<NodeTemplate> getPolicyTargetsFromOrigin(NodeTemplate nodeTemplate, String policyName); /** * Get the node template of the topology template specified by name * @param nodeTemplateName the name of the node template * @return the node template */ - public NodeTemplate getNodeTemplateByName(String nodeTemplateName); + NodeTemplate getNodeTemplateByName(String nodeTemplateName); /** * Get all the policies, which contain the specified node template as a target * @param targetNode the node template * @return the list of the policies */ - public List<Policy> getPoliciesOfTarget(NodeTemplate targetNode); + List<Policy> getPoliciesOfTarget(NodeTemplate targetNode); /** * Get all the policies of the specified type, which contain the specified node template as a target @@ -516,27 +516,27 @@ public interface ISdcCsarHelper { * @param policyTypeName the name of the policy type * @return the list of the policies */ - public List<Policy> getPoliciesOfTargetByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); + List<Policy> getPoliciesOfTargetByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); /** * Get all the groups of the origin component (nested topology template) of the node template * @param nodeTemplate the node template * @return the list of the groups */ - public ArrayList<Group> getGroupsOfOriginOfNodeTemplate(NodeTemplate nodeTemplate); + ArrayList<Group> getGroupsOfOriginOfNodeTemplate(NodeTemplate nodeTemplate); /** * Get all groups of this of the main topology template (either VF or service) by specified tosca group type * @param groupType the group type * @return the list of the groups */ - public ArrayList<Group> getGroupsOfTopologyTemplateByToscaGroupType(String groupType); + ArrayList<Group> getGroupsOfTopologyTemplateByToscaGroupType(String groupType); /** * Get all groups of this of the main topology template (either VF or service) * @return the list of the groups */ - public ArrayList<Group> getGroupsOfTopologyTemplate(); + ArrayList<Group> getGroupsOfTopologyTemplate(); /** * Get all groups of this of the origin component (nested topology template) of the node template by specified tosca group type @@ -544,14 +544,14 @@ public interface ISdcCsarHelper { * @param groupType the group type * @return the list of the groups */ - public ArrayList<Group> getGroupsOfOriginOfNodeTemplateByToscaGroupType(NodeTemplate nodeTemplate, String groupType); + ArrayList<Group> getGroupsOfOriginOfNodeTemplateByToscaGroupType(NodeTemplate nodeTemplate, String groupType); /** * Get members of the group belongs to the main topology template (either VF or service) by group name * @param groupName the name of the group * @return the list of the node templates */ - public List<NodeTemplate> getGroupMembersFromTopologyTemplate(String groupName); + List<NodeTemplate> getGroupMembersFromTopologyTemplate(String groupName); /** * Get members of the group belongs to the origin component (nested topology template) of the node template by group name @@ -559,20 +559,20 @@ public interface ISdcCsarHelper { * @param groupName the name of the group * @return the list of the node templates */ - public List<NodeTemplate> getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName); + List<NodeTemplate> getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName); /** * Get inputs of the topology template including existing annotations * @return the list of the inputs */ - public List<Input> getInputsWithAnnotations(); + List<Input> getInputsWithAnnotations(); /** * Get all interface details for given node template.<br> * @return Map that contains the list of all interfaces and their definitions. * If none found, an empty map will be returned. */ - public Map<String, List<InterfacesDef>> getInterfacesOf(NodeTemplate nt); + Map<String, List<InterfacesDef>> getInterfacesOf(NodeTemplate nt); /** * Get all interface names for given node template.<br> @@ -586,20 +586,31 @@ public interface ISdcCsarHelper { * @return List that contains the definitions of given interface name. * If none found, an empty list will be returned. */ - public List<InterfacesDef> getInterfaceDetails(NodeTemplate nt, String interfaceName); + List<InterfacesDef> getInterfaceDetails(NodeTemplate nt, String interfaceName); /** * Get all operation names for given node template and interface name.<br> * @return List that contains the name of all operations for a given node template and interface name. * If none found, an empty list will be returned. */ - public List<String> getAllInterfaceOperations(NodeTemplate nt, String interfaceName); + List<String> getAllInterfaceOperations(NodeTemplate nt, String interfaceName); /** * Get interface details for a given node template, interface name and operation name.<br> * @return InterfaceDef representing the operation details. * If none found, null will be returned. */ - public InterfacesDef getInterfaceOperationDetails(NodeTemplate nt, String interfaceName, String operationName); + InterfacesDef getInterfaceOperationDetails(NodeTemplate nt, String interfaceName, String operationName); + /** + * Get property value for a property of given node template.<br> + * @param propertyNamePath valid name of property for search.<br> + * To find value in the datatype with datatype entry schema, the property name should be defined with # delimiter + * @param nodeTemplatePath path to the model node template that property value will be searched.<br> + * Path is based on the collection of the node templates names delimited by #. + * @return List of property values. If none found, empty list will be returned. + */ + List<String> getPropertyLeafValueByPropertyNamePathAndNodeTemplatePath(String propertyNamePath, String nodeTemplatePath); + + boolean isNodeTypeSupported(NodeTemplate nodeTemplate); }
\ No newline at end of file diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java index 09c3c5c..c4ce838 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java @@ -1,4 +1,4 @@ -package org.onap.sdc.tosca.parser.impl; +package org.onap.sdc.tosca.parser.enums; public enum FilterType { diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java index fb13867..539ba00 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java @@ -1,4 +1,4 @@ -package org.onap.sdc.tosca.parser.impl; +package org.onap.sdc.tosca.parser.enums; public enum JToscaValidationIssueType { CRITICAL, diff --git a/src/main/java/org/onap/sdc/tosca/parser/enums/PropertySchemaType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/PropertySchemaType.java new file mode 100644 index 0000000..48fc28f --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/PropertySchemaType.java @@ -0,0 +1,69 @@ +package org.onap.sdc.tosca.parser.enums; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import static org.onap.sdc.tosca.parser.enums.PropertySchemaType.PropertySchemaComplexity.Complex; +import static org.onap.sdc.tosca.parser.enums.PropertySchemaType.PropertySchemaComplexity.DataType; +import static org.onap.sdc.tosca.parser.enums.PropertySchemaType.PropertySchemaComplexity.Simple; + +public enum PropertySchemaType { + + STRING(Simple, "string"), + INTEGER(Simple, "integer"), + BOOLEAN(Simple, "boolean"), + FLOAT(Simple, "float"), + NUMBER(Simple, "number"), + TIMESTAMP(Simple, "timestamp"), + RANGE(Simple, "range"), + VERSION(Simple, "version"), + SCALAR_UNIT_SIZE(Simple, "scalar-unit.size"), + SCALAR_UNIT_TIME(Simple, "scalar-unit.time"), + SCALAR_UNIT_FREQUENCY(Simple, "scalar-unit.frequency"), + LIST(Complex, "list"), + MAP(Complex, "map"), + DATATYPE(DataType, "datatypes"); + + private PropertySchemaComplexity complexity; + private String schemaType; + + PropertySchemaType(PropertySchemaComplexity complexity, String schemaType) { + this.complexity = complexity; + this.schemaType = schemaType; + } + + public PropertySchemaComplexity getSchemaTypeComplexity() { + return complexity; + } + + public String getSchemaTypeName() { + return schemaType; + } + + public enum PropertySchemaComplexity { + Simple, Complex, DataType + } + + public static PropertySchemaType getEnumByValue(String type){ + if (type == null) { + throwNoSuchElementException(null); + } + + if (type.contains(DATATYPE.getSchemaTypeName())) { + return DATATYPE; + } + PropertySchemaType propertySchemaType = Arrays.stream(PropertySchemaType.values()) + .filter(v->v.getSchemaTypeName().equals(type)) + .findFirst().orElse(null); + if (propertySchemaType == null) { + throwNoSuchElementException(type); + } + return propertySchemaType; + } + + private static void throwNoSuchElementException(String type) { + throw new NoSuchElementException(String.format("Value %s is not defined in %s", type, PropertySchemaType.class.getName())); + } + +} diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java index 9daf0ef..99bdd81 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.sdc.tosca.parser.impl; +package org.onap.sdc.tosca.parser.enums; import java.util.Arrays; import java.util.List; @@ -32,7 +32,7 @@ public enum SdcTypes { private static List<String> complexTypes = Arrays.asList(VF, PNF, CR, SERVICE, CVFC).stream().map(SdcTypes::getValue).collect(Collectors.toList()); - private SdcTypes(String value) { + SdcTypes(String value) { this.value = value; } diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java index f82272b..ee9ac06 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java +++ b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java @@ -21,7 +21,10 @@ package org.onap.sdc.tosca.parser.impl; import static java.util.stream.Collectors.toList; + import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -35,7 +38,11 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.config.ConfigurationManager; +import org.onap.sdc.tosca.parser.enums.FilterType; +import org.onap.sdc.tosca.parser.enums.PropertySchemaType; +import org.onap.sdc.tosca.parser.enums.SdcTypes; import org.onap.sdc.tosca.parser.utils.GeneralUtility; +import org.onap.sdc.tosca.parser.utils.PropertyUtils; import org.onap.sdc.tosca.parser.utils.SdcToscaUtility; import org.onap.sdc.toscaparser.api.CapabilityAssignment; import org.onap.sdc.toscaparser.api.CapabilityAssignments; @@ -199,33 +206,23 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { @Override //Sunny flow - covered with UT, flat and nested public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) { - if (nodeTemplate == null) { - log.error("getNodeTemplatePropertyLeafValue - nodeTemplate is null"); - return null; - } - if (GeneralUtility.isEmptyString(leafValuePath)) { - log.error("getNodeTemplatePropertyLeafValue - leafValuePath is null or empty"); - return null; - } - String[] split = getSplittedPath(leafValuePath); - LinkedHashMap<String, Property> properties = nodeTemplate.getProperties(); - Object property = processProperties(split, properties); - return property == null || property instanceof Function ? null : String.valueOf(property); + Object value = getNodeTemplatePropertyValueAsObject(nodeTemplate, leafValuePath); + return value == null || value instanceof Function ? null : String.valueOf(value); } @Override - public Object getNodeTemplatePropertyAsObject(NodeTemplate nodeTemplate, String leafValuePath) { + public Object getNodeTemplatePropertyValueAsObject(NodeTemplate nodeTemplate, String leafValuePath) { if (nodeTemplate == null) { - log.error("getNodeTemplatePropertyAsObject - nodeTemplate is null"); + log.error("getNodeTemplatePropertyValueAsObject - nodeTemplate is null"); return null; } if (GeneralUtility.isEmptyString(leafValuePath)) { - log.error("getNodeTemplatePropertyAsObject - leafValuePath is null or empty"); + log.error("getNodeTemplatePropertyValueAsObject - leafValuePath is null or empty"); return null; } String[] split = getSplittedPath(leafValuePath); LinkedHashMap<String, Property> properties = nodeTemplate.getProperties(); - return processProperties(split, properties); + return PropertyUtils.processProperties(split, properties); } public Map<String, Map<String, Object>> getCpPropertiesFromVfcAsObject(NodeTemplate vfc) { @@ -436,7 +433,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { if (findFirst.isPresent()) { Input input = findFirst.get(); Object current = input.getDefault(); - Object property = iterateProcessPath(2, current, split); + Object property = PropertyUtils.iterateProcessPath(2, current, split); return property == null || property instanceof Function? null : String.valueOf(property); } } @@ -463,42 +460,15 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { if (findFirst.isPresent()) { Input input = findFirst.get(); Object current = input.getDefault(); - return iterateProcessPath(2, current, split); + return PropertyUtils.iterateProcessPath(2, current, split); } } log.error("getServiceInputLeafValueOfDefaultAsObject - value not found"); return null; } - @SuppressWarnings({ "unchecked", "rawtypes" }) - private Object iterateProcessPath(Integer index, Object current, String[] split) { - if (current == null) { - log.error("iterateProcessPath - this input has no default"); - return null; - } - if (split.length > index) { - for (int i = index; i < split.length; i++) { - if (current instanceof Map) { - current = ((Map<String, Object>) current).get(split[i]); - } else if (current instanceof List) { - current = ((List) current).get(0); - i--; - } - else { - log.error("iterateProcessPath - found an unexpected leaf where expected to find a complex type"); - return null; - } - } - } - if (current != null) { - return current; - } - log.error("iterateProcessPath - Path not Found"); - return null; - } - - private String[] getSplittedPath(String inputLeafValuePath) { - return inputLeafValuePath.split(PATH_DELIMITER); + private String[] getSplittedPath(String leafValuePath) { + return leafValuePath.split(PATH_DELIMITER); } @@ -564,7 +534,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { String[] split = getSplittedPath(leafValuePath); LinkedHashMap<String, Property> properties = group.getProperties(); - Object property = processProperties(split, properties); + Object property = PropertyUtils.processProperties(split, properties); return property == null || property instanceof Function? null : String.valueOf(property); } @@ -582,7 +552,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { String[] split = getSplittedPath(leafValuePath); LinkedHashMap<String, Property> properties = group.getProperties(); - return processProperties(split, properties); + return PropertyUtils.processProperties(split, properties); } @Override @@ -757,6 +727,13 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType) { return getNodeTemplateBySdcType(parentNodeTemplate, sdcType, false); } + + public boolean isNodeTypeSupported(NodeTemplate nodeTemplate) { + SdcTypes[] supportedTypes = SdcTypes.values(); + return Arrays.stream(supportedTypes) + .anyMatch(v->nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE) + .equals(v.getValue())); + } private List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType, boolean isVNF) { @@ -941,7 +918,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { String[] split = getSplittedPath(pathToPropertyLeafValue); LinkedHashMap<String, Property> properties = capability.getProperties(); - Object property = processProperties(split, properties); + Object property = PropertyUtils.processProperties(split, properties); return property == null || property instanceof Function ? null : String.valueOf(property); } @@ -1073,18 +1050,6 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { } } - private Object processProperties(String[] split, LinkedHashMap<String, Property> properties) { - Optional<Entry<String, Property>> findFirst = properties.entrySet().stream().filter(x -> x.getKey().equals(split[0])).findFirst(); - if (findFirst.isPresent()) { - Property property = findFirst.get().getValue(); - Object current = property.getValue(); - return iterateProcessPath(1, current, split); - } - String propName = (split != null && split.length > 0 ? split[0] : null); - log.error("processProperties - property {} not found", propName); - return null; - } - @Override public Map<String, List<InterfacesDef>> getInterfacesOf(NodeTemplate nt){ if (nt == null) { @@ -1122,7 +1087,115 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return null; } - @Override + @Override + public List<String> getPropertyLeafValueByPropertyNamePathAndNodeTemplatePath(String propertyNamePath, String nodeTemplatePath) { + log.info("A new request is received: property path is [{}], node template path is [{}]", + propertyNamePath, nodeTemplatePath); + + List<String> propertyValuesList; + + if (StringUtils.isEmpty(nodeTemplatePath) || StringUtils.isEmpty(propertyNamePath)) { + log.error("One of parameters is empty or null: property path is [{}], node template path is [{}]", + propertyNamePath, nodeTemplatePath); + propertyValuesList = Collections.emptyList(); + } + else { + String[] nodeTemplates = getSplittedPath(nodeTemplatePath); + propertyValuesList = getPropertyFromInternalNodeTemplate(getNodeTemplateByName(nodeTemplates[0]), 1, nodeTemplates, propertyNamePath); + log.info("Found property value {} by path [{}] for node template [{}]", + propertyValuesList, propertyNamePath, nodeTemplatePath); + } + return propertyValuesList; + } + + private List<String> getPropertyFromInternalNodeTemplate(NodeTemplate parent, int index, + String[] nodeTemplatePath, String propertyPath) { + List<String> propertyValuesList; + if (parent == null) { + log.error("Node template {} is not found, the request will be rejected", nodeTemplatePath[index]); + propertyValuesList = Collections.emptyList(); + } + else if (nodeTemplatePath.length <= index) { + log.debug("Stop NODE TEMPLATE searching"); + propertyValuesList = getSimpleOrListPropertyValue(parent, propertyPath); + } + else { + log.debug("Node template {} is found with name {}", nodeTemplatePath[index], parent.getName()); + NodeTemplate childNT = getChildNodeTemplateByName(parent, nodeTemplatePath[index]); + + if (childNT == null || !isNodeTypeSupported(childNT)) { + log.error("Unsupported or not found node template named {}, the request will be rejected", + nodeTemplatePath[index]); + propertyValuesList = Collections.emptyList(); + } + else { + propertyValuesList = getPropertyFromInternalNodeTemplate(childNT, index + 1, nodeTemplatePath, + propertyPath); + } + } + return propertyValuesList; + } + + + + private List<String> getSimpleOrListPropertyValue(NodeTemplate nodeTemplate, String propertyPath) { + List<String> propertyValueList; + String[] path = getSplittedPath(propertyPath); + Property property = getNodeTemplatePropertyObjectByName(nodeTemplate, path[0]); + + if (PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property)) { + //the requested property type is either simple or list of simple types + PropertySchemaType propertyType = PropertySchemaType.getEnumByValue(property.getType()); + if (propertyType == PropertySchemaType.LIST && + PropertyUtils.isDataPropertyType((String)property.getEntrySchema() + .get(SdcPropertyNames.PROPERTY_NAME_TYPE))) { + //cover the case when a type of property "path[0]' is list of data types + // and the requested property is an internal simple property of this data type + propertyValueList = calculatePropertyValue(getNodeTemplatePropertyValueAsObject(nodeTemplate, path[0]), path, nodeTemplate.getName()); + } + else { + //the requested property is simple type or list of simple types + propertyValueList = calculatePropertyValue(getNodeTemplatePropertyValueAsObject(nodeTemplate, propertyPath), null, nodeTemplate.getName()); + } + } + else { + log.error("The type of property {} on node {} is neither simple nor list of simple objects, the request will be rejected", + propertyPath, nodeTemplate.getName()); + propertyValueList = Collections.emptyList(); + } + return propertyValueList; + } + + private List<String> calculatePropertyValue(Object valueAsObject, String path[], String nodeName) { + if (valueAsObject == null || valueAsObject instanceof Map) { + log.error("The property {} either is not found on node template [{}], or it is data type, or it is not resolved get_input", path, nodeName); + return Collections.emptyList(); + } + if (path != null) { + return PropertyUtils.findSimplePropertyValueInListOfDataTypes((List<Object>)valueAsObject, path); + } + return PropertyUtils.buildSimplePropertValueOrList(valueAsObject); + } + + + + + private Property getNodeTemplatePropertyObjectByName(NodeTemplate nodeTemplate, String propertyName) { + return nodeTemplate.getPropertiesObjects() + .stream() + .filter(p->p.getName().equals(propertyName)) + .findFirst() + .orElse(null); + } + + private NodeTemplate getChildNodeTemplateByName(NodeTemplate parent, String nodeTemplateName) { + return getNodeTemplateChildren(parent) + .stream() + .filter(nt->nt.getName().equals(nodeTemplateName)) + .findFirst().orElse(null); + } + + @Override public List<Input> getInputsWithAnnotations() { return toscaTemplate.getInputs(true); } diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcPropertyNames.java b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcPropertyNames.java index 6f7e568..f791520 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcPropertyNames.java +++ b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcPropertyNames.java @@ -117,4 +117,5 @@ public class SdcPropertyNames { public static String PROPERTY_NAME_TARGETS = "targets"; public static String PROPERTY_NAME_MEMBERS = "members"; public static String PROPERTY_NAME_CAPABILITIES = "capabilities"; + public static String PROPERTY_NAME_ENTRY_SCHEMA = "entry_schema"; } diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcToscaParserFactory.java b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcToscaParserFactory.java index 3293a58..12fa0e8 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcToscaParserFactory.java +++ b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcToscaParserFactory.java @@ -8,6 +8,7 @@ import org.onap.sdc.tosca.parser.config.ConfigurationManager; import org.onap.sdc.tosca.parser.config.ErrorInfo; import org.onap.sdc.tosca.parser.config.JToscaValidationIssueInfo; import org.onap.sdc.tosca.parser.config.SdcToscaParserErrors; +import org.onap.sdc.tosca.parser.enums.JToscaValidationIssueType; import org.onap.sdc.tosca.parser.utils.GeneralUtility; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; diff --git a/src/main/java/org/onap/sdc/tosca/parser/utils/PropertyUtils.java b/src/main/java/org/onap/sdc/tosca/parser/utils/PropertyUtils.java new file mode 100644 index 0000000..cefb426 --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/utils/PropertyUtils.java @@ -0,0 +1,172 @@ +package org.onap.sdc.tosca.parser.utils; + +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.enums.PropertySchemaType; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.Property; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.onap.sdc.tosca.parser.enums.PropertySchemaType.PropertySchemaComplexity.Simple; + +public class PropertyUtils { + + private static Logger log = LoggerFactory.getLogger(PropertyUtils.class.getName()); + + private PropertyUtils() {} + + private static String calculatePropertyType(LinkedHashMap<String, Object> property) { + String type = (String) property.get(SdcPropertyNames.PROPERTY_NAME_TYPE); + if (PropertySchemaType.LIST.getSchemaTypeName().equals(type)) { + //it might be a data type + return getEntrySchemaType(property); + } + return type; + } + + private static String getEntrySchemaType(LinkedHashMap<String, Object> property) { + LinkedHashMap<String, Object> entrySchema = (LinkedHashMap<String, Object>)property.get(SdcPropertyNames.PROPERTY_NAME_ENTRY_SCHEMA); + if (entrySchema != null) { + return (String) entrySchema.get(SdcPropertyNames.PROPERTY_NAME_TYPE); + } + return null; + } + + private static String calculatePropertyType(Property property) { + if (PropertySchemaType.LIST.getSchemaTypeName().equals(property.getType())) { + //if it is list, return entry schema type + return (String)property.getEntrySchema().get(SdcPropertyNames.PROPERTY_NAME_TYPE); + } + return property.getType(); + } + + public static boolean isListOfSimpleTypes(String type) { + PropertySchemaType entrySchemaType = PropertySchemaType.getEnumByValue(type); + return entrySchemaType.getSchemaTypeComplexity() == PropertySchemaType.PropertySchemaComplexity.Simple; + } + + public static boolean isDataPropertyType(String type) { + PropertySchemaType entrySchemaType = PropertySchemaType.getEnumByValue(type); + return entrySchemaType == PropertySchemaType.DATATYPE; + } + + public static Object processProperties(String[] split, LinkedHashMap<String, Property> properties) { + Optional<Map.Entry<String, Property>> findFirst = properties.entrySet().stream().filter(x -> x.getKey().equals(split[0])).findFirst(); + if (findFirst.isPresent()) { + Property property = findFirst.get().getValue(); + Object current = property.getValue(); + return iterateProcessPath(1, current, split); + } + String propName = (split != null && split.length > 0 ? split[0] : null); + log.error("processProperties - property {} is not found", propName); + return null; + } + + public static List<String> findSimplePropertyValueInListOfDataTypes(List<Object> valueAsObjectList, String[] path) { + return valueAsObjectList.stream() + .map(v->iterateProcessPath(1, v, path)) + .filter(Objects::nonNull) + .map(String::valueOf) + .collect(Collectors.toList()); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static Object iterateProcessPath(Integer index, Object current, String[] split) { + if (current == null) { + log.error("iterateProcessPath - this input has no default"); + return null; + } + if (split.length > index) { + for (int i = index; i < split.length; i++) { + if (current instanceof Map) { + current = ((Map<String, Object>) current).get(split[i]); + } else if (current instanceof List) { + current = ((List) current).get(0); + i--; + } + else { + log.error("iterateProcessPath - found an unexpected leaf where expected to find a complex type"); + return null; + } + } + } + if (current != null) { + return current; + } + log.error("iterateProcessPath - Path not Found"); + return null; + } + + public static boolean isPropertyTypeSimpleOrListOfSimpleTypes(NodeTemplate nodeTemplate, String[] path, Property property) { + PropertySchemaType internalPropertyType = PropertyUtils.getPropertyTypeByPath(nodeTemplate, path, property); + return internalPropertyType.getSchemaTypeComplexity() == Simple; + } + + private static PropertySchemaType getPropertyTypeByPath(NodeTemplate nodeTemplate, String[] path, Property property) { + String propertyType = calculatePropertyType(property); + String propertyTypeByPath = propertyType; + + if (path.length > 1) { + propertyTypeByPath = getInternalPropertyType(nodeTemplate, propertyType, path, 1); + } + return PropertySchemaType.getEnumByValue(propertyTypeByPath); + } + + public static List<String> buildSimplePropertValueOrList(Object value) { + if (value instanceof List) { + return ((ArrayList<Object>) value) + .stream() + //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()); + } + return Lists.newArrayList(String.valueOf(value)); + } + + private static String getInternalPropertyType(NodeTemplate nodeTemplate, String dataTypeName, String[] path, int index) { + if (path.length > index) { + LinkedHashMap<String, Object> complexProperty = (LinkedHashMap<String, Object>) nodeTemplate.getCustomDef().get(dataTypeName); + if (complexProperty != null) { + LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) complexProperty.get(SdcPropertyNames.PROPERTY_NAME_PROPERTIES); + return getPropertyTypeFromCustomDef(nodeTemplate, path, index, properties); + } + } + //stop searching - seems as wrong flow: the path is finished but the value is not found yet + log.error("The property path {} is incorrect, the request will be rejected", path); + return null; + } + + private static String getPropertyTypeFromCustomDef(NodeTemplate nodeTemplate, String[] path, int index, LinkedHashMap<String, Object> properties) { + final String methodName = "getPropertyTypeFromCustomDef"; + if (properties != null) { + LinkedHashMap<String, Object> foundProperty = (LinkedHashMap<String, Object>) (properties).get(path[index]); + if (foundProperty != null) { + String propertyType = calculatePropertyType(foundProperty); + log.info("{} - type {} is data type", methodName, propertyType); + if ((index == path.length - 1)){ + log.info("{} - the last element {} in the property path is found", methodName, path[index]); + return propertyType; + } + return getInternalPropertyType(nodeTemplate, propertyType, path, index + 1); + } + log.error("{} - the property [{}] is not found", methodName, path[index]); + } + return null; + } + + +} |