Add support for simple yaml profile 1.2 48/102148/6
authorvasraz <vasyl.razinkov@est.tech>
Fri, 21 Feb 2020 13:50:25 +0000 (13:50 +0000)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Mon, 23 Mar 2020 10:32:59 +0000 (10:32 +0000)
Fix NPE in GroupEntityQuery, NodeTemplateEntityQuery, PolicyEntityQuery, QueryProcessor
Add new CSAR to test tosca_definitions_version: tosca_simple_yaml_1_2

Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Change-Id: Ib52616b3d86008d5fece2f146309b35d79fafa18
Issue-ID: SDC-2738

sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/elements/queries/GroupEntityQuery.java
sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/elements/queries/NodeTemplateEntityQuery.java
sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/elements/queries/PolicyEntityQuery.java
sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/impl/QueryProcessor.java
sdc-tosca/src/test/java/org/onap/sdc/impl/ToscaParserSimpleYaml12Test.java [new file with mode: 0644]
sdc-tosca/src/test/resources/csars/resource-Amf-tosca_simple_yaml_1_2.csar [new file with mode: 0644]
sdc-tosca/src/test/resources/csars/resource-Networkfunction-tosca_simple_yaml_1_2.csar [new file with mode: 0644]
sdc-tosca/src/test/resources/csars/resource-Networkservice-tosca_simple_yaml_1_2.csar [new file with mode: 0644]
sdc-tosca/src/test/resources/csars/resource-Resource-tosca_simple_yaml_1_2.csar [new file with mode: 0644]

index dba16d3..d4945dd 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 package org.onap.sdc.tosca.parser.elements.queries;
 
 import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.onap.sdc.tosca.parser.api.IEntityDetails;
 import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory;
 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
 import org.onap.sdc.toscaparser.api.Group;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
+import org.onap.sdc.toscaparser.api.SubstitutionMappings;
 import org.onap.sdc.toscaparser.api.ToscaTemplate;
 
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
 /**
  * Implements EntityQuery object for Groups
  */
@@ -44,32 +45,41 @@ public class GroupEntityQuery extends EntityQuery {
         super(EntityTemplateType.GROUP, null, null);
     }
 
-    @Override
-    public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) {
-        if (nodeTemplate.getSubMappingToscaTemplate() != null) {
-            return convertGroupLisToEntityDetailsList(filter(nodeTemplate.getSubMappingToscaTemplate().getGroups()));
-        }
-        return Lists.newArrayList();
+    GroupEntityQuery(String toscaType) {
+        super(EntityTemplateType.GROUP, null, toscaType);
     }
 
-    @Override
-    public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) {
-        return convertGroupLisToEntityDetailsList(filter(toscaTemplate.getGroups()));
+    static List<IEntityDetails> convertGroupLisToEntityDetailsList(final Stream<Group> groups) {
+        return groups.map(gr -> EntityDetailsFactory.createEntityDetails(EntityTemplateType.GROUP, gr))
+            .collect(Collectors.toList());
     }
 
-    GroupEntityQuery(String toscaType) {
-        super(EntityTemplateType.GROUP, null, toscaType);
+    @Override
+    public List<IEntityDetails> getEntitiesFromTopologyTemplate(final NodeTemplate nodeTemplate) {
+        final SubstitutionMappings subMappingToscaTemplate = nodeTemplate.getSubMappingToscaTemplate();
+        if (subMappingToscaTemplate != null) {
+            final List<Group> groups = subMappingToscaTemplate.getGroups();
+            if (groups != null) {
+                return convertGroupLisToEntityDetailsList(filter(groups));
+            }
+        }
+        return Lists.newArrayList();
     }
 
-    static List<IEntityDetails> convertGroupLisToEntityDetailsList(Stream<Group> groups) {
-        return groups.map(gr->EntityDetailsFactory.createEntityDetails(EntityTemplateType.GROUP, gr))
-            .collect(Collectors.toList());
+    @Override
+    public List<IEntityDetails> getEntitiesFromService(final ToscaTemplate toscaTemplate) {
+        final List<Group> groups = toscaTemplate.getGroups();
+        if (groups != null) {
+            return convertGroupLisToEntityDetailsList(filter(groups));
+        } else {
+            return new ArrayList<>();
+        }
     }
 
-    private Stream<Group> filter(List<Group> groupList) {
+    private Stream<Group> filter(final List<Group> groupList) {
         return groupList.stream()
-            .filter(gr->isSearchCriteriaMatched(gr.getMetadata(), gr.getType()) ||
-                    isSearchCriteriaMatched(gr.getMetadata(), gr.getType(), VF_MODULE_UUID, VF_MODULE_CUSTOMIZATION_UUID));
+            .filter(gr -> isSearchCriteriaMatched(gr.getMetadata(), gr.getType()) ||
+                isSearchCriteriaMatched(gr.getMetadata(), gr.getType(), VF_MODULE_UUID, VF_MODULE_CUSTOMIZATION_UUID));
     }
 
 }
index 30784e7..352d28d 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 package org.onap.sdc.tosca.parser.elements.queries;
 
 import com.google.common.collect.Lists;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.onap.sdc.tosca.parser.api.IEntityDetails;
 import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory;
 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
 import org.onap.sdc.tosca.parser.enums.SdcTypes;
 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
+import org.onap.sdc.toscaparser.api.SubstitutionMappings;
 import org.onap.sdc.toscaparser.api.ToscaTemplate;
 
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
 /**
  * Implements EntityQuery object for NodeTemplates
  */
 public class NodeTemplateEntityQuery extends EntityQuery {
+
     NodeTemplateEntityQuery() {
         super(EntityTemplateType.NODE_TEMPLATE, null, null);
     }
@@ -49,33 +50,39 @@ public class NodeTemplateEntityQuery extends EntityQuery {
         super(EntityTemplateType.NODE_TEMPLATE, null, toscaType);
     }
 
+    static List<IEntityDetails> convertNodeTemplatesListToEntityDetailsList(final Stream<NodeTemplate> nodeTemplates) {
+        return nodeTemplates
+            .map(nt -> EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, nt))
+            .collect(Collectors.toList());
+    }
+
     @Override
-    public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) {
-        if (nodeTemplate.getSubMappingToscaTemplate() != null) {
-            return convertNodeTemplatesListToEntityDetailsList(filter(nodeTemplate.getSubMappingToscaTemplate()
-                    .getNodeTemplates()));
+    public List<IEntityDetails> getEntitiesFromTopologyTemplate(final NodeTemplate nodeTemplate) {
+        final SubstitutionMappings subMappingToscaTemplate = nodeTemplate.getSubMappingToscaTemplate();
+        if (subMappingToscaTemplate != null) {
+            final List<NodeTemplate> nodeTemplates = subMappingToscaTemplate.getNodeTemplates();
+            if (nodeTemplates != null) {
+                return convertNodeTemplatesListToEntityDetailsList(filter(nodeTemplates));
+            }
         }
         return Lists.newArrayList();
     }
 
     @Override
-    public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) {
-        return convertNodeTemplatesListToEntityDetailsList(filter(toscaTemplate.getNodeTemplates()));
-    }
-
-    static List<IEntityDetails> convertNodeTemplatesListToEntityDetailsList(Stream<NodeTemplate> nodeTemplates) {
-        return nodeTemplates
-            .map(nt->EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, nt))
-            .collect(Collectors.toList());
+    public List<IEntityDetails> getEntitiesFromService(final ToscaTemplate toscaTemplate) {
+        final List<NodeTemplate> nodeTemplates = toscaTemplate.getNodeTemplates();
+        if (nodeTemplates != null) {
+            return convertNodeTemplatesListToEntityDetailsList(filter(nodeTemplates));
+        }
+        return Lists.newArrayList();
     }
 
-    private Stream<NodeTemplate> filter(List<NodeTemplate> nodeTemplateList) {
+    private Stream<NodeTemplate> filter(final List<NodeTemplate> nodeTemplateList) {
         return nodeTemplateList.stream()
-                .filter(nt->isSearchCriteriaMatched(nt.getMetaData(), nt.getType()))
-                .filter(nt->getNodeTemplateType() == null ||
-                        isStringMatchingOrNull(nt.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE),
-                                getNodeTemplateType().getValue()));
+            .filter(nt -> isSearchCriteriaMatched(nt.getMetaData(), nt.getType()))
+            .filter(nt -> getNodeTemplateType() == null ||
+                isStringMatchingOrNull(nt.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE),
+                    getNodeTemplateType().getValue()));
     }
 
-
 }
index f93505d..32a4b10 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 package org.onap.sdc.tosca.parser.elements.queries;
 
 import com.google.common.collect.Lists;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.onap.sdc.tosca.parser.api.IEntityDetails;
 import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory;
 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
 import org.onap.sdc.toscaparser.api.Policy;
+import org.onap.sdc.toscaparser.api.TopologyTemplate;
 import org.onap.sdc.toscaparser.api.ToscaTemplate;
 
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
 /**
  * Implements EntityQuery object for Policies
  */
@@ -45,28 +45,36 @@ public class PolicyEntityQuery extends EntityQuery {
         super(EntityTemplateType.POLICY, null, toscaType);
     }
 
+    static List<IEntityDetails> convertPolicyLisToEntityDetailsList(final Stream<Policy> policies) {
+        return policies
+            .map(p -> EntityDetailsFactory.createEntityDetails(EntityTemplateType.POLICY, p))
+            .collect(Collectors.toList());
+    }
+
     @Override
-    public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) {
-        if (nodeTemplate.getOriginComponentTemplate() != null) {
-            return convertPolicyLisToEntityDetailsList(filter(nodeTemplate.getOriginComponentTemplate().getPolicies()));
+    public List<IEntityDetails> getEntitiesFromTopologyTemplate(final NodeTemplate nodeTemplate) {
+        final TopologyTemplate originComponentTemplate = nodeTemplate.getOriginComponentTemplate();
+        if (originComponentTemplate != null) {
+            final List<Policy> policies = originComponentTemplate.getPolicies();
+            if (policies != null) {
+                return convertPolicyLisToEntityDetailsList(filter(policies));
+            }
         }
         return Lists.newArrayList();
     }
 
     @Override
-    public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) {
-        return convertPolicyLisToEntityDetailsList(filter(toscaTemplate.getPolicies()));
-    }
-
-    static List<IEntityDetails> convertPolicyLisToEntityDetailsList(Stream<Policy> policies) {
-        return policies
-            .map(p->EntityDetailsFactory.createEntityDetails(EntityTemplateType.POLICY, p))
-            .collect(Collectors.toList());
+    public List<IEntityDetails> getEntitiesFromService(final ToscaTemplate toscaTemplate) {
+        final List<Policy> policies = toscaTemplate.getPolicies();
+        if (policies != null) {
+            return convertPolicyLisToEntityDetailsList(filter(policies));
+        }
+        return Lists.newArrayList();
     }
 
-    private Stream<Policy> filter(List<Policy> policyList) {
+    private Stream<Policy> filter(final List<Policy> policyList) {
         return policyList.stream()
-                .filter(p->isSearchCriteriaMatched(p.getMetaDataObj(), p.getType()));
+            .filter(p -> isSearchCriteriaMatched(p.getMetaDataObj(), p.getType()));
     }
 
 }
index 6fd5ce5..c703e2c 100644 (file)
 
 package org.onap.sdc.tosca.parser.impl;
 
-import com.google.common.collect.Lists;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 import org.onap.sdc.tosca.parser.api.IEntityDetails;
 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
@@ -31,10 +34,6 @@ import org.onap.sdc.toscaparser.api.ToscaTemplate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
 /**
  * Performs search for entity templates inside node template according to query criteria
  */
@@ -54,7 +53,7 @@ class QueryProcessor {
     }
 
     List<IEntityDetails> doQuery() {
-        List<IEntityDetails> entityDetailsList = Lists.newArrayList();
+        final List<IEntityDetails> entityDetailsList = Collections.emptyList();
         if (isServiceSearch()) {
             //search for entities inside the service
             if (logger.isDebugEnabled()) {
@@ -113,15 +112,18 @@ class QueryProcessor {
     }
 
     private List<NodeTemplate> getInternalTopologyTemplates(List<NodeTemplate> nodeTemplateList, boolean isRecursive) {
-        return nodeTemplateList
-            .stream()
-            .map(child->getTopologyTemplatesByQuery(child, isRecursive))
-            .flatMap(List::stream)
-            .collect(Collectors.toList());
+        if (nodeTemplateList != null) {
+            return nodeTemplateList
+                .stream()
+                .map(child -> getTopologyTemplatesByQuery(child, isRecursive))
+                .flatMap(List::stream)
+                .collect(Collectors.toList());
+        }
+        return Collections.emptyList();
     }
 
     private List<NodeTemplate> getTopologyTemplatesByQuery(NodeTemplate current, boolean isRecursive) {
-        List<NodeTemplate> topologyTemplateList = Lists.newArrayList();
+        final List<NodeTemplate> topologyTemplateList = Collections.emptyList();
 
         boolean isTopologyTemplateFound = isRecursive ?
                 SdcTypes.isComplex(current.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))
@@ -145,5 +147,4 @@ class QueryProcessor {
         return topologyTemplateList;
     }
 
-
 }
diff --git a/sdc-tosca/src/test/java/org/onap/sdc/impl/ToscaParserSimpleYaml12Test.java b/sdc-tosca/src/test/java/org/onap/sdc/impl/ToscaParserSimpleYaml12Test.java
new file mode 100644 (file)
index 0000000..e1b16c1
--- /dev/null
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Nordix Foundation. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.sdc.impl;
+
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import java.io.File;
+import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
+import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
+import org.testng.annotations.Test;
+
+public class ToscaParserSimpleYaml12Test {
+
+    private static final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
+
+    @Test
+    public void testVersion() throws Exception {
+
+        verify("csars/resource-Networkfunction-tosca_simple_yaml_1_2.csar");
+        verify("csars/resource-Networkservice-tosca_simple_yaml_1_2.csar");
+        verify("csars/resource-Amf-tosca_simple_yaml_1_2.csar");
+        verify("csars/resource-Resource-tosca_simple_yaml_1_2.csar");
+    }
+
+    private void verify(final String resourceFileName) throws Exception {
+        final ISdcCsarHelper fdntCsarHelper = getCsarHelper(resourceFileName);
+
+        assertNotNull(fdntCsarHelper.getServiceMetadata());
+        assertNull(fdntCsarHelper.getServiceSubstitutionMappingsTypeName());
+        assertNotNull(fdntCsarHelper.getServiceMetadataAllProperties());
+        assertNull(fdntCsarHelper.getServiceInputs());
+        assertNotNull(fdntCsarHelper.getConformanceLevel());
+        assertNotNull(fdntCsarHelper.getDataTypes());
+        assertNull(fdntCsarHelper.getInputsWithAnnotations());
+        assertNotNull(fdntCsarHelper.getVFModule());
+    }
+
+    private ISdcCsarHelper getCsarHelper(final String path) throws SdcToscaParserException {
+        System.out.println("Parsing CSAR " + path + "...");
+        final String fileName = SdcToscaParserBasicTest.class.getClassLoader().getResource(path).getFile();
+        return factory.getSdcCsarHelper((new File(fileName)).getAbsolutePath());
+    }
+
+}
diff --git a/sdc-tosca/src/test/resources/csars/resource-Amf-tosca_simple_yaml_1_2.csar b/sdc-tosca/src/test/resources/csars/resource-Amf-tosca_simple_yaml_1_2.csar
new file mode 100644 (file)
index 0000000..5c733c3
Binary files /dev/null and b/sdc-tosca/src/test/resources/csars/resource-Amf-tosca_simple_yaml_1_2.csar differ
diff --git a/sdc-tosca/src/test/resources/csars/resource-Networkfunction-tosca_simple_yaml_1_2.csar b/sdc-tosca/src/test/resources/csars/resource-Networkfunction-tosca_simple_yaml_1_2.csar
new file mode 100644 (file)
index 0000000..aeb5366
Binary files /dev/null and b/sdc-tosca/src/test/resources/csars/resource-Networkfunction-tosca_simple_yaml_1_2.csar differ
diff --git a/sdc-tosca/src/test/resources/csars/resource-Networkservice-tosca_simple_yaml_1_2.csar b/sdc-tosca/src/test/resources/csars/resource-Networkservice-tosca_simple_yaml_1_2.csar
new file mode 100644 (file)
index 0000000..ecf76b1
Binary files /dev/null and b/sdc-tosca/src/test/resources/csars/resource-Networkservice-tosca_simple_yaml_1_2.csar differ
diff --git a/sdc-tosca/src/test/resources/csars/resource-Resource-tosca_simple_yaml_1_2.csar b/sdc-tosca/src/test/resources/csars/resource-Resource-tosca_simple_yaml_1_2.csar
new file mode 100644 (file)
index 0000000..7d91ff7
Binary files /dev/null and b/sdc-tosca/src/test/resources/csars/resource-Resource-tosca_simple_yaml_1_2.csar differ