diff options
author | 2019-03-27 17:13:21 +0000 | |
---|---|---|
committer | 2019-03-28 17:07:39 +0000 | |
commit | c1824169b0cfa25101c1efa8118d6b0b085edd15 (patch) | |
tree | 949b130c6f48ace5c10e2c584693fec9963d4b28 | |
parent | 834b05d3bf1e0d842a21a3cb31e8150ce0a8927b (diff) |
Depend on sdc-tosca version 1.5.0
Replace the following deprecated API methods with equivalent functions:
getGroupsOfOriginOfNodeTemplate()
getNodeTemplateCustomizationUuid()
getNodeTemplateChildren()
getServiceVfList()
getMembersOfVfModule()
getVfModulesByVf()
Create a helper class and move the remaining deprecated API calls into a
single Java file for ease of maintenance.
Change-Id: Icb08b2d66a34b0a3e717adb49f2d62e8f2e2ddb5
Issue-ID: AAI-2294
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
6 files changed, 174 insertions, 39 deletions
@@ -53,7 +53,7 @@ <!-- Dependency Versions --> <common.logging.version>1.2.2</common.logging.version> - <sdc.tosca.version>1.4.7</sdc.tosca.version> + <sdc.tosca.version>1.5.0</sdc.tosca.version> <commons.compress.version>1.18</commons.compress.version> <javax.ws.rs.version>2.1</javax.ws.rs.version> <mvn.jaxb2.version>0.13.2</mvn.jaxb2.version> diff --git a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java index 5d33bc2..5f3d15b 100644 --- a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java @@ -30,18 +30,17 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; -import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.time.StopWatch; import org.onap.aai.babel.logging.ApplicationMsgs; import org.onap.aai.babel.logging.LogHelper; +import org.onap.aai.babel.parser.ToscaParser; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.enums.SdcTypes; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; -import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.NodeTemplate; @@ -195,12 +194,12 @@ public class VnfVendorImageExtractor { throws SdcToscaParserException, InvalidNumberOfNodesException { ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath); - List<NodeTemplate> serviceVfList = csarHelper.getServiceNodeTemplates().stream() - .filter(filterOnType(SdcTypes.VF)).collect(Collectors.toList()); + List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper) + .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList()); List<NodeTemplate> vnfConfigs = serviceVfList.stream() .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream() - .filter(filterOnType(SdcTypes.VFC)) // + .filter(ToscaParser.filterOnType(SdcTypes.VFC)) // .filter(vfc -> vfc.getType().endsWith("VnfConfiguration"))) .filter(Objects::nonNull) // .collect(Collectors.toList()); @@ -255,11 +254,6 @@ public class VnfVendorImageExtractor { return vendorImageConfigurations; } - private Predicate<? super NodeTemplate> filterOnType(SdcTypes sdcType) { - return node -> (node.getMetaData() != null - && sdcType.getValue().equals(node.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))); - } - /** * Builds the Vendor Image configurations. * diff --git a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java index c2d34c9..281ac63 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -28,10 +28,12 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import org.onap.aai.babel.logging.LogHelper; @@ -45,9 +47,12 @@ import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.aai.babel.xml.generator.types.ModelType; import org.onap.aai.cl.api.Logger; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.tosca.parser.utils.SdcToscaUtility; import org.onap.sdc.toscaparser.api.Group; import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.SubstitutionMappings; import org.onap.sdc.toscaparser.api.elements.Metadata; /** @@ -135,7 +140,7 @@ public class ArtifactGeneratorToscaParser { throws XmlArtifactGenerationException { List<Resource> resources = new ArrayList<>(); if (serviceNodeTemplate.getSubMappingToscaTemplate() != null) { - List<Group> serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate); + List<Group> serviceGroups = serviceNodeTemplate.getSubMappingToscaTemplate().getGroups(); for (Group group : serviceGroups) { if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) { resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(), @@ -199,26 +204,39 @@ public class ArtifactGeneratorToscaParser { * * @param resources * @param model - * @param serviceNode + * @param serviceVfNode + * a VF resource Node Template * @throws XmlArtifactGenerationException * if the configured widget mappings do not support the widget type of a VF Module */ - public void processVfModules(List<Resource> resources, Model resourceModel, NodeTemplate serviceNode) + public void processVfModules(List<Resource> resources, Model resourceModel, NodeTemplate serviceVfNode) throws XmlArtifactGenerationException { - // Get the customization UUID for each VF node and use it to get its Groups - String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode); - List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid); - // Process each VF Group - for (Group serviceGroup : serviceGroups) { + for (Group serviceGroup : getVfModuleGroups(serviceVfNode)) { Model groupModel = Model.getModelFor(serviceGroup.getType()); if (groupModel.hasWidgetType("VFMODULE")) { - processVfModule(resources, resourceModel, serviceGroup, serviceNode, (Resource) groupModel); + processVfModule(resources, resourceModel, serviceGroup, serviceVfNode, (Resource) groupModel); } } } /** + * Implementation taken from the sdc-tosca parser (deprecated method). + * + * @param serviceVfNode + * a VF resource Node Template + * @return all service level VfModule groups with a name matching that of the supplied VF node template + */ + private List<Group> getVfModuleGroups(NodeTemplate serviceVfNode) { + String instanceName = SdcToscaUtility.normaliseComponentInstanceName(serviceVfNode.getName()); + + return ToscaParser.getServiceLevelGroups(csarHelper).stream() + .filter(group -> "org.openecomp.groups.VfModule".equals(group.getTypeDefinition().getType()) + && group.getName().startsWith(instanceName)) + .collect(Collectors.toList()); + } + + /** * @param resourceModel * @param resourceNodeTemplates */ @@ -329,7 +347,12 @@ public class ArtifactGeneratorToscaParser { groupModel.populateModelIdentificationInformation( mergeProperties(groupDefinition.getMetadata().getAllProperties(), groupDefinition.getProperties())); - processVfModuleGroup(groupModel, csarHelper.getMembersOfVfModule(serviceNode, groupDefinition)); + + SubstitutionMappings substitutionMappings = serviceNode.getSubMappingToscaTemplate(); + if (substitutionMappings != null) { + processVfModuleGroup(groupModel, getVfModuleMembers(substitutionMappings, + groupDefinition.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID))); + } vfModel.addResource(groupModel); // Add group (VfModule) to the (VF) model // Check if we have already encountered the same VfModule across all the artifacts @@ -339,6 +362,29 @@ public class ArtifactGeneratorToscaParser { } /** + * @param substitutionMappings + * @param vfModuleInvariantUuid + * @return all serviceNode child Node Templates which are members of the first VF Module Group + */ + private List<NodeTemplate> getVfModuleMembers(SubstitutionMappings substitutionMappings, + String vfModuleInvariantUuid) { + return Optional.ofNullable(substitutionMappings.getGroups()) // + .map(groups -> groups.stream() // + .filter(filterByVfModuleInvariantUuid(vfModuleInvariantUuid)) // + .findFirst().map(module -> Optional.ofNullable(module.getMembers()).orElse(new ArrayList<>())) + .orElse(new ArrayList<>())) + .map(members -> substitutionMappings.getNodeTemplates().stream() + .filter(nt -> members.contains(nt.getName())) // + .collect(Collectors.toList())) + .orElse(Collections.emptyList()); + } + + private Predicate<? super Group> filterByVfModuleInvariantUuid(String vfModuleInvariantUuid) { + return nt -> (nt.getMetadata() != null && vfModuleInvariantUuid + .equals(nt.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID))); + } + + /** * @param groupModel * @param members * @throws XmlArtifactGenerationException diff --git a/src/main/java/org/onap/aai/babel/parser/ToscaParser.java b/src/main/java/org/onap/aai/babel/parser/ToscaParser.java new file mode 100644 index 0000000..1e7d599 --- /dev/null +++ b/src/main/java/org/onap/aai/babel/parser/ToscaParser.java @@ -0,0 +1,68 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. + * ================================================================================ + * 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.aai.babel.parser; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.enums.SdcTypes; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.toscaparser.api.Group; +import org.onap.sdc.toscaparser.api.NodeTemplate; + +/** + * Helper class to isolate the deprecated tosca parser API methods. + * + */ +public class ToscaParser { + + private ToscaParser() { + // Not to be instantiated in this version + } + + @SuppressWarnings("deprecation") + public static Stream<NodeTemplate> getServiceNodeTemplates(ISdcCsarHelper csarHelper) { + return csarHelper.getServiceNodeTemplates().stream(); // NOSONAR + } + + @SuppressWarnings("deprecation") + public static List<Group> getServiceLevelGroups(ISdcCsarHelper csarHelper) { + return Optional.ofNullable(csarHelper.getGroupsOfTopologyTemplate()).orElse(new ArrayList<>()); // NOSONAR + } + + /** + * Create a Predicate that will return true if and only if the supplied NodeTemplate's metadata contains a + * <code>type</code> value matching the specified SDC Type. + * + * @param sdcType + * the type metadata value to match against + * @return a Predicate for matching the NodeTemplate to the SDC Type + */ + public static Predicate<NodeTemplate> filterOnType(SdcTypes sdcType) { + return nodeTemplate -> (nodeTemplate.getMetaData() != null + && sdcType.getValue().equals(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))); + } + +} diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java index 11c1471..32237b5 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java @@ -25,14 +25,17 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.onap.aai.babel.logging.ApplicationMsgs; import org.onap.aai.babel.logging.LogHelper; import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; +import org.onap.aai.babel.parser.ToscaParser; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.data.AdditionalParams; import org.onap.aai.babel.xml.generator.data.Artifact; @@ -49,6 +52,7 @@ import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.aai.babel.xml.generator.types.ModelType; import org.onap.aai.cl.api.Logger; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.enums.SdcTypes; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.Group; @@ -127,7 +131,8 @@ public class AaiArtifactGenerator implements ArtifactGenerator { */ public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) throws XmlArtifactGenerationException { - List<NodeTemplate> serviceNodeTemplates = csarHelper.getServiceNodeTemplates(); + List<NodeTemplate> serviceNodeTemplates = + ToscaParser.getServiceNodeTemplates(csarHelper).collect(Collectors.toList()); if (serviceNodeTemplates == null) { throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); } @@ -186,11 +191,11 @@ public class AaiArtifactGenerator implements ArtifactGenerator { */ private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates, Service serviceModel) throws XmlArtifactGenerationException { - final List<Group> serviceGroups = csarHelper.getGroupsOfTopologyTemplate(); final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); List<Resource> resources = new ArrayList<>(); + final List<Group> serviceGroups = ToscaParser.getServiceLevelGroups(csarHelper); for (NodeTemplate nodeTemplate : serviceNodeTemplates) { if (nodeTemplate.getMetaData() != null) { generateModelFromNodeTemplate(csarHelper, serviceModel, resources, serviceGroups, parser, nodeTemplate); @@ -265,36 +270,60 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * @param csarHelper * @param resources * @param parser - * @param nodeTemplate + * @param serviceVfNode + * a VF resource Node Template * @throws XmlArtifactGenerationException * if the configured widget mappings do not support processed widget type(s) */ private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources, - ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { - Resource resourceModel = getModelFor(parser, nodeTemplate); + ArtifactGeneratorToscaParser parser, NodeTemplate serviceVfNode) throws XmlArtifactGenerationException { + Resource resourceModel = getModelFor(parser, serviceVfNode); if (resourceModel == null) { log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model"); return; } - Map<String, String> serviceMetadata = nodeTemplate.getMetaData().getAllProperties(); + Map<String, String> serviceMetadata = serviceVfNode.getMetaData().getAllProperties(); resourceModel.populateModelIdentificationInformation(serviceMetadata); - parser.processResourceModels(resourceModel, csarHelper.getNodeTemplateChildren(nodeTemplate)); + parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode)); + + List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper) + .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList()); - if (csarHelper.getServiceVfList() != null) { - parser.processVfModules(resources, resourceModel, nodeTemplate); + if (serviceVfList != null) { + parser.processVfModules(resources, resourceModel, serviceVfNode); } if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) { resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT")); } - resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate)); + resources.addAll(parser.processInstanceGroups(resourceModel, serviceVfNode)); resources.add(resourceModel); } /** + * Return all child Node Templates (via Substitution Mappings) that do not have a type ending VnfConfiguration. + * + * @param nodeTemplate + * the parent Node Template + * @return the child Node Templates which are not a VNF Configuration type + */ + private List<NodeTemplate> getNonVnfChildren(NodeTemplate nodeTemplate) { + return Optional.ofNullable(nodeTemplate.getSubMappingToscaTemplate()) // + .map(sm -> Optional.ofNullable(sm.getNodeTemplates()) + .map(nts -> nts.stream().filter(nt -> !isVNFType(nt)) // + .collect(Collectors.toList())) + .orElse(Collections.emptyList())) + .orElse(Collections.emptyList()); + } + + private boolean isVNFType(NodeTemplate nt) { + return nt.getType().endsWith("VnfConfiguration"); + } + + /** * @param generationData * @param resource * @throws XmlArtifactGenerationException diff --git a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java index 50812c9..2416cc8 100644 --- a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java +++ b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java @@ -27,6 +27,7 @@ import static org.hamcrest.Matchers.is; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -189,21 +190,18 @@ public class TestArtifactGeneratorToscaParser { final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup"; WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType)); - ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class); SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class); + List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType)); + Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups)); + NodeTemplate serviceNodeTemplate = buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource"); serviceNodeTemplate.setSubMappingToscaTemplate(sm); - Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate); - ArrayList<Group> groups = new ArrayList<>(); - groups.add(buildGroup("group", instanceGroupType)); - Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups); - - ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper); Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true); - List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate); + List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class)) + .processInstanceGroups(groupResource, serviceNodeTemplate); assertThat(resources.size(), is(1)); Resource resource = resources.get(0); |