X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fxml%2Fgenerator%2Fapi%2FAaiArtifactGenerator.java;h=424b1a6ae4480393416a314ebb2478dea69dbaec;hb=60457e41054465e9fa10c8e6bfdbfa62a45d1f0a;hp=505378edd6e1dfec58fef2783e868c2233b0fdcb;hpb=2aedac7b674a854600cac9b4bfe8e864f2e88526;p=aai%2Fbabel.git 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 505378e..424b1a6 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 @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * 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. @@ -18,34 +18,46 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.xml.generator.api; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashMap; +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; import org.onap.aai.babel.xml.generator.data.ArtifactType; import org.onap.aai.babel.xml.generator.data.GenerationData; import org.onap.aai.babel.xml.generator.data.GeneratorUtil; import org.onap.aai.babel.xml.generator.data.GroupType; +import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.model.Model; -import org.onap.aai.babel.xml.generator.model.ProvidingService; import org.onap.aai.babel.xml.generator.model.Resource; import org.onap.aai.babel.xml.generator.model.Service; +import org.onap.aai.babel.xml.generator.model.Widget; +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; import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; import org.slf4j.MDC; public class AaiArtifactGenerator implements ArtifactGenerator { @@ -61,11 +73,25 @@ public class AaiArtifactGenerator implements ArtifactGenerator { private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = "Cannot generate artifacts. Service version is incorrect"; - private AaiModelGenerator modelGenerator = new AaiModelGeneratorImpl(); + private AaiModelGenerator modelGenerator = new AaiModelGenerator(); @Override public GenerationData generateArtifact(byte[] csarArchive, List input, Map additionalParams) { + String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE); + if (configLocation == null) { + throw new IllegalArgumentException( + String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, + ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE)); + } + + try { + ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation); + } catch (IOException e) { + log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation); + return createErrorData(e); + } + Path csarPath; try { @@ -76,12 +102,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } try { - ArtifactGeneratorToscaParser.initWidgetConfiguration(); - ArtifactGeneratorToscaParser.initGroupFilterConfiguration(); ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString()); - return generateService(validateServiceVersion(additionalParams), csarHelper); - } catch (Exception e) { + return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper); + } catch (SdcToscaParserException | ClassCastException | XmlArtifactGenerationException e) { log.error(ApplicationMsgs.INVALID_CSAR_FILE, e); return createErrorData(e); } finally { @@ -99,41 +123,35 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * Generate model artifacts for the Service and its associated Resources. * * @param serviceVersion - * @param csarHelper TOSCA parser - * @return the generated Artifacts + * @param csarHelper + * interface to the TOSCA parser + * @return the generated Artifacts (containing XML models) + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) */ - private GenerationData generateService(final String serviceVersion, ISdcCsarHelper csarHelper) { - List serviceNodes = csarHelper.getServiceNodeTemplates(); - if (serviceNodes == null) { - throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); - } - - // Populate basic service model metadata - Service serviceModel = new Service(); - serviceModel.setModelVersion(serviceVersion); - serviceModel.populateModelIdentificationInformation(csarHelper.getServiceMetadataAllProperties()); - - Map idTypeStore = new HashMap<>(); + public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) + throws XmlArtifactGenerationException { + Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties()); - ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); - if (!serviceNodes.isEmpty()) { - parser.processServiceTosca(serviceModel, idTypeStore, serviceNodes); - } + MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); - // Process the resource TOSCA files - List resources = parser.processResourceToscas(serviceNodes, idTypeStore); + List resources = generateResourceModels(csarHelper, serviceModel); - MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); - String aaiServiceModel = modelGenerator.generateModelFor(serviceModel); + // Generate the A&AI XML model for the Service. + final String serviceArtifact = modelGenerator.generateModelFor(serviceModel); + // Build a Babel Artifact to be returned to the caller. GenerationData generationData = new GenerationData(); - generationData.add(getServiceArtifact(serviceModel, aaiServiceModel)); + generationData.add(getServiceArtifact(serviceModel, serviceArtifact)); - // Generate AAI XML resource model + // For each Resource, generate the A&AI XML model and then create an additional Artifact for that model. for (Resource resource : resources) { generateResourceArtifact(generationData, resource); for (Resource childResource : resource.getResources()) { - if (!(childResource instanceof ProvidingService)) { + boolean isProvidingService = + (boolean) Optional.ofNullable(childResource.getProperties().get("providingService")) // + .orElse(false); + if (!isProvidingService) { generateResourceArtifact(generationData, childResource); } } @@ -142,15 +160,176 @@ public class AaiArtifactGenerator implements ArtifactGenerator { return generationData; } + /** + * Create a Service from the provided metadata + * + * @param serviceVersion + * @param properties + * @return + */ + private Service createServiceModel(final String serviceVersion, Map properties) { + log.debug("Processing (TOSCA) Service object"); + Service serviceModel = new Service(); + serviceModel.setModelVersion(serviceVersion); + serviceModel.populateModelIdentificationInformation(properties); + return serviceModel; + } + + /** + * @param csarHelper + * @param serviceModel + * @return the generated Models + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) + */ + private List generateResourceModels(ISdcCsarHelper csarHelper, Service serviceModel) + throws XmlArtifactGenerationException { + List serviceNodeTemplates = + ToscaParser.getServiceNodeTemplates(csarHelper).collect(Collectors.toList()); + if (serviceNodeTemplates == null) { + throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); + } + + final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); + List resources = new ArrayList<>(); + final List serviceGroups = ToscaParser.getServiceLevelGroups(csarHelper); + for (NodeTemplate nodeTemplate : serviceNodeTemplates) { + if (nodeTemplate.getMetaData() != null) { + generateModelFromNodeTemplate(csarHelper, serviceModel, resources, serviceGroups, parser, nodeTemplate); + } else { + log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName()); + } + } + + return resources; + } + + /** + * @param csarHelper + * @param serviceModel + * @param resources + * @param serviceGroups + * @param parser + * @param nodeTemplate + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) + */ + private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel, + List resources, final List serviceGroups, ArtifactGeneratorToscaParser parser, + NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { + Resource model = getModelFor(parser, nodeTemplate); + + if (model != null) { + if (nodeTemplate.getMetaData() != null) { + model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties()); + } + + parser.addRelatedModel(serviceModel, model); + if (model.getModelType() == ModelType.RESOURCE) { + generateResourceModel(csarHelper, resources, parser, nodeTemplate); + } + } else { + for (Group group : serviceGroups) { + ArrayList members = group.getMembers(); + if (members != null && members.contains(nodeTemplate.getName()) + && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) { + log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(), + group.getType(), members)); + + Resource groupModel = parser.createInstanceGroupModel( + parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties())); + serviceModel.addResource(groupModel); + resources.add(groupModel); + } + } + } + } + + private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) { + String nodeTypeName = nodeTemplate.getType(); + + log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID")); + + Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type")); + + if (model != null) { + Metadata metadata = nodeTemplate.getMetaData(); + if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties()) + && model.hasWidgetType("VF")) { + model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true); + } + } + + return model; + } + + /** + * @param csarHelper + * @param resources + * @param parser + * @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 resources, + 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 serviceMetadata = serviceVfNode.getMetaData().getAllProperties(); + resourceModel.populateModelIdentificationInformation(serviceMetadata); + + parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode)); + + List serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper) + .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList()); + + 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, 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 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 */ - private void generateResourceArtifact(GenerationData generationData, Resource resource) { + private void generateResourceArtifact(GenerationData generationData, Resource resource) + throws XmlArtifactGenerationException { if (!isContained(generationData, getArtifactName(resource))) { log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model"); - Artifact resourceArtifact = getResourceArtifact(resource, modelGenerator.generateModelFor(resource)); - generationData.add(resourceArtifact); + generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource))); } } @@ -170,7 +349,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { private String getArtifactLabel(Model model) { StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name()); artifactName.append("-"); - artifactName.append(model.getModelType().name().toLowerCase()); + artifactName.append(model.getModelTypeName()); artifactName.append("-"); artifactName.append(hashCodeUuId(model.getModelNameVersionId())); return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-"); @@ -179,7 +358,8 @@ public class AaiArtifactGenerator implements ArtifactGenerator { /** * Method to generate the artifact name for an AAI model. * - * @param model AAI artifact model + * @param model + * AAI artifact model * @return Model artifact name */ private String getArtifactName(Model model) { @@ -190,7 +370,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { artifactName.append(truncatedArtifactName); artifactName.append("-"); - artifactName.append(model.getModelType().name().toLowerCase()); + artifactName.append(model.getModelTypeName()); artifactName.append("-"); artifactName.append(model.getModelVersion()); @@ -202,11 +382,13 @@ public class AaiArtifactGenerator implements ArtifactGenerator { /** * Create Resource artifact model from the AAI xml model string. * - * @param resourceModel Model of the resource artifact - * @param aaiResourceModel AAI model as string + * @param resourceModel + * Model of the resource artifact + * @param aaiResourceModel + * AAI model as string * @return Generated {@link Artifact} model for the resource */ - private Artifact getResourceArtifact(Model resourceModel, String aaiResourceModel) { + private Artifact getResourceArtifact(Resource resourceModel, String aaiResourceModel) { final String resourceArtifactLabel = getArtifactLabel(resourceModel); MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel); final byte[] bytes = aaiResourceModel.getBytes(); @@ -215,7 +397,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes)); artifact.setName(getArtifactName(resourceModel)); artifact.setLabel(resourceArtifactLabel); - artifact.setDescription(ArtifactGeneratorToscaParser.getArtifactDescription(resourceModel)); + artifact.setDescription("AAI Resource Model"); return artifact; } @@ -230,10 +412,12 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } /** - * Create Service artifact model from the AAI xml model string. + * Create Service artifact model from the AAI XML model. * - * @param serviceModel Model of the service artifact - * @param aaiServiceModel AAI model as string + * @param serviceModel + * Model of the service artifact + * @param aaiServiceModel + * AAI model as string * @return Generated {@link Artifact} model for the service */ private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) { @@ -243,22 +427,23 @@ public class AaiArtifactGenerator implements ArtifactGenerator { String serviceArtifactLabel = getArtifactLabel(serviceModel); artifact.setName(serviceArtifactName); artifact.setLabel(serviceArtifactLabel); - String description = ArtifactGeneratorToscaParser.getArtifactDescription(serviceModel); - artifact.setDescription(description); + artifact.setDescription("AAI Service Model"); return artifact; } private int hashCodeUuId(String uuId) { int hashcode = 0; - for (int i = 0; i < uuId.length(); i++) { - hashcode = 31 * hashcode + uuId.charAt(i); + if (uuId != null) { + for (int i = 0; i < uuId.length(); i++) { + hashcode = 31 * hashcode + uuId.charAt(i); + } } return hashcode; } private String truncateName(String name) { String truncatedName = name; - if (name.length() >= 200) { + if (name != null && name.length() >= 200) { truncatedName = name.substring(0, 199); } return truncatedName;