From a8b7b97e8f4aa088a7e39e06946c425ddd1ba2b4 Mon Sep 17 00:00:00 2001 From: "Manzon, Inna (im453s)" Date: Wed, 12 Sep 2018 18:58:05 +0300 Subject: Tosca Parser - getPropertyValue and tests Change-Id: I1baa920033f410f90b2b742c007d917eb6e151ff Issue-ID: SDC-1757 Signed-off-by: Manzon, Inna (im453s) --- .../onap/sdc/tosca/parser/api/ISdcCsarHelper.java | 51 +++--- .../onap/sdc/tosca/parser/enums/FilterType.java | 26 +++ .../parser/enums/JToscaValidationIssueType.java | 6 + .../sdc/tosca/parser/enums/PropertySchemaType.java | 69 +++++++ .../org/onap/sdc/tosca/parser/enums/SdcTypes.java | 46 +++++ .../org/onap/sdc/tosca/parser/impl/FilterType.java | 26 --- .../parser/impl/JToscaValidationIssueType.java | 6 - .../sdc/tosca/parser/impl/SdcCsarHelperImpl.java | 199 ++++++++++++++------- .../sdc/tosca/parser/impl/SdcPropertyNames.java | 1 + .../tosca/parser/impl/SdcToscaParserFactory.java | 1 + .../org/onap/sdc/tosca/parser/impl/SdcTypes.java | 46 ----- .../onap/sdc/tosca/parser/utils/PropertyUtils.java | 172 ++++++++++++++++++ 12 files changed, 488 insertions(+), 161 deletions(-) create mode 100644 src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java create mode 100644 src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java create mode 100644 src/main/java/org/onap/sdc/tosca/parser/enums/PropertySchemaType.java create mode 100644 src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java delete mode 100644 src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java delete mode 100644 src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java delete mode 100644 src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java create mode 100644 src/main/java/org/onap/sdc/tosca/parser/utils/PropertyUtils.java (limited to 'src/main/java') 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 getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); + List 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 getPolicyTargetsFromTopologyTemplate(String policyName); + List 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 getPolicyTargetsFromOrigin(NodeTemplate nodeTemplate, String policyName); + List 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 getPoliciesOfTarget(NodeTemplate targetNode); + List 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 getPoliciesOfTargetByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); + List 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 getGroupsOfOriginOfNodeTemplate(NodeTemplate nodeTemplate); + ArrayList 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 getGroupsOfTopologyTemplateByToscaGroupType(String groupType); + ArrayList 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 getGroupsOfTopologyTemplate(); + ArrayList 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 getGroupsOfOriginOfNodeTemplateByToscaGroupType(NodeTemplate nodeTemplate, String groupType); + ArrayList 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 getGroupMembersFromTopologyTemplate(String groupName); + List 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 getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName); + List getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName); /** * Get inputs of the topology template including existing annotations * @return the list of the inputs */ - public List getInputsWithAnnotations(); + List getInputsWithAnnotations(); /** * Get all interface details for given node template.
* @return Map that contains the list of all interfaces and their definitions. * If none found, an empty map will be returned. */ - public Map> getInterfacesOf(NodeTemplate nt); + Map> getInterfacesOf(NodeTemplate nt); /** * Get all interface names for given node template.
@@ -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 getInterfaceDetails(NodeTemplate nt, String interfaceName); + List getInterfaceDetails(NodeTemplate nt, String interfaceName); /** * Get all operation names for given node template and interface name.
* @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 getAllInterfaceOperations(NodeTemplate nt, String interfaceName); + List getAllInterfaceOperations(NodeTemplate nt, String interfaceName); /** * Get interface details for a given node template, interface name and operation name.
* @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.
+ * @param propertyNamePath valid name of property for search.
+ * 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.
+ * 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 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/enums/FilterType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java new file mode 100644 index 0000000..c4ce838 --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/FilterType.java @@ -0,0 +1,26 @@ +package org.onap.sdc.tosca.parser.enums; + +public enum FilterType { + + CONTAINS("contains"){ + @Override + public boolean isMatch(String value, String pattern) { + return value.contains(pattern); + } + }, + EQUALS("equals"){ + @Override + public boolean isMatch(String value, String pattern) { + return value.equals(pattern); + } + }; + + String filterName; + + FilterType(String name) { + this.filterName = name; + } + + public abstract boolean isMatch(String value, String pattern); + +} diff --git a/src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java new file mode 100644 index 0000000..539ba00 --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/JToscaValidationIssueType.java @@ -0,0 +1,6 @@ +package org.onap.sdc.tosca.parser.enums; + +public enum JToscaValidationIssueType { + CRITICAL, + WARNING +} 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/enums/SdcTypes.java b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java new file mode 100644 index 0000000..99bdd81 --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.tosca.parser.enums; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public enum SdcTypes { + + CP("CP"), VL("VL"), VF("VF"), CR("CR"), VFC("VFC"), PNF("PNF"), SERVICE("Service"), CVFC("CVFC"), SERVICE_PROXY("Service Proxy"), CONFIGURATION("Configuration"); + + private String value; + + private static List complexTypes = Arrays.asList(VF, PNF, CR, SERVICE, CVFC).stream().map(SdcTypes::getValue).collect(Collectors.toList()); + + SdcTypes(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public static boolean isComplex(String sdcType) { + return complexTypes.contains(sdcType); + } +} diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java b/src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java deleted file mode 100644 index 09c3c5c..0000000 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/FilterType.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.onap.sdc.tosca.parser.impl; - -public enum FilterType { - - CONTAINS("contains"){ - @Override - public boolean isMatch(String value, String pattern) { - return value.contains(pattern); - } - }, - EQUALS("equals"){ - @Override - public boolean isMatch(String value, String pattern) { - return value.equals(pattern); - } - }; - - String filterName; - - FilterType(String name) { - this.filterName = name; - } - - public abstract boolean isMatch(String value, String pattern); - -} diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java b/src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java deleted file mode 100644 index fb13867..0000000 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/JToscaValidationIssueType.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.onap.sdc.tosca.parser.impl; - -public enum JToscaValidationIssueType { - CRITICAL, - WARNING -} 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 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 properties = nodeTemplate.getProperties(); - return processProperties(split, properties); + return PropertyUtils.processProperties(split, properties); } public Map> 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) 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 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 properties = group.getProperties(); - return processProperties(split, properties); + return PropertyUtils.processProperties(split, properties); } @Override @@ -757,6 +727,13 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { public List 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 getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType, boolean isVNF) { @@ -941,7 +918,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { String[] split = getSplittedPath(pathToPropertyLeafValue); LinkedHashMap 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 properties) { - Optional> 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> getInterfacesOf(NodeTemplate nt){ if (nt == null) { @@ -1122,7 +1087,115 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return null; } - @Override + @Override + public List getPropertyLeafValueByPropertyNamePathAndNodeTemplatePath(String propertyNamePath, String nodeTemplatePath) { + log.info("A new request is received: property path is [{}], node template path is [{}]", + propertyNamePath, nodeTemplatePath); + + List 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 getPropertyFromInternalNodeTemplate(NodeTemplate parent, int index, + String[] nodeTemplatePath, String propertyPath) { + List 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 getSimpleOrListPropertyValue(NodeTemplate nodeTemplate, String propertyPath) { + List 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 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)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 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/impl/SdcTypes.java b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java deleted file mode 100644 index 9daf0ef..0000000 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcTypes.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * sdc-distribution-client - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.sdc.tosca.parser.impl; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public enum SdcTypes { - - CP("CP"), VL("VL"), VF("VF"), CR("CR"), VFC("VFC"), PNF("PNF"), SERVICE("Service"), CVFC("CVFC"), SERVICE_PROXY("Service Proxy"), CONFIGURATION("Configuration"); - - private String value; - - private static List complexTypes = Arrays.asList(VF, PNF, CR, SERVICE, CVFC).stream().map(SdcTypes::getValue).collect(Collectors.toList()); - - private SdcTypes(String value) { - this.value = value; - } - - public String getValue() { - return value; - } - - public static boolean isComplex(String sdcType) { - return complexTypes.contains(sdcType); - } -} 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 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 property) { + LinkedHashMap entrySchema = (LinkedHashMap)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 properties) { + Optional> 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 findSimplePropertyValueInListOfDataTypes(List 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) 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 buildSimplePropertValueOrList(Object value) { + if (value instanceof List) { + return ((ArrayList) 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 complexProperty = (LinkedHashMap) nodeTemplate.getCustomDef().get(dataTypeName); + if (complexProperty != null) { + LinkedHashMap properties = (LinkedHashMap) 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 properties) { + final String methodName = "getPropertyTypeFromCustomDef"; + if (properties != null) { + LinkedHashMap foundProperty = (LinkedHashMap) (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; + } + + +} -- cgit 1.2.3-korg