diff options
author | mark.j.leonard <mark.j.leonard@gmail.com> | 2018-10-08 12:04:17 +0100 |
---|---|---|
committer | mark.j.leonard <mark.j.leonard@gmail.com> | 2018-10-08 12:04:17 +0100 |
commit | 2aedac7b674a854600cac9b4bfe8e864f2e88526 (patch) | |
tree | 608a8cde7a211324741a2a0a891169224f2ca100 | |
parent | 90fac993d264e255eac148487e24c9b02464a071 (diff) |
Generate models for child resources
Babel now generates models for immediate children of Resources,
non-recursively. Existing functionality is unchanged. Some large methods
are refactored to avoid a Cognitive Complexity Sonar code smell.
Change-Id: I7ab173a5693f25d0b303dfa97a25106e7e29b5f2
Issue-ID: AAI-1704
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
3 files changed, 151 insertions, 94 deletions
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 b85ffab..e5141a5 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -28,8 +28,10 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.stream.Collectors; + import org.onap.aai.babel.logging.ApplicationMsgs; import org.onap.aai.babel.logging.LogHelper; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; @@ -209,7 +211,7 @@ public class ArtifactGeneratorToscaParser { resourceModel.populateModelIdentificationInformation(serviceMetadata); idTypeStore.remove(resourceModel.getModelNameVersionId()); - processVfTosca(idTypeStore, resourceModel, resourceNodes); + processResourceModels(idTypeStore, resourceModel, resourceNodes); if (csarHelper.getServiceVfList() != null) { processVfModules(resources, resourceModel, serviceNode); @@ -407,29 +409,17 @@ public class ArtifactGeneratorToscaParser { e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString())); } - private void processVfTosca(Map<String, String> idTypeStore, Model resourceModel, + private void processResourceModels(Map<String, String> idTypeStore, Model resourceModel, List<NodeTemplate> resourceNodes) { boolean foundProvidingService = false; for (NodeTemplate resourceNodeTemplate : resourceNodes) { String nodeTypeName = normaliseNodeTypeName(resourceNodeTemplate); - Model resourceNode = Model.getModelFor(nodeTypeName); - if (resourceNode instanceof ProvidingService) { - foundProvidingService = true; - Map<String, Property> nodeProperties = resourceNodeTemplate.getProperties(); - if (nodeProperties.get("providing_service_uuid") == null - || nodeProperties.get("providing_service_invariant_uuid") == null) { - throw new IllegalArgumentException(String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, - resourceModel.getModelId())); - } - Map<String, String> properties = populateStringProperties(nodeProperties); - properties.put(VERSION, "1.0"); - resourceNode.populateModelIdentificationInformation(properties); - resourceModel.addResource((Resource) resourceNode); - } else if (resourceNode instanceof Resource && !(resourceNode.getWidgetType().equals(Widget.Type.L3_NET))) { - idTypeStore.put(resourceNode.getModelNameVersionId(), nodeTypeName); - resourceModel.addResource((Resource) resourceNode); - } + Metadata metaData = resourceNodeTemplate.getMetaData(); + String metaDataType = Optional.ofNullable(metaData).map(m -> m.getValue("type")).orElse(nodeTypeName); + Model resourceNode = Model.getModelFor(nodeTypeName, metaDataType); + foundProvidingService |= processModel(idTypeStore, resourceModel, resourceNodeTemplate, nodeTypeName, + metaData, resourceNode); } if (resourceModel instanceof AllotedResource && !foundProvidingService) { @@ -438,4 +428,33 @@ public class ArtifactGeneratorToscaParser { } } + private boolean processModel(Map<String, String> idTypeStore, Model resourceModel, + NodeTemplate resourceNodeTemplate, String nodeTypeName, Metadata metaData, Model resourceNode) { + boolean foundProvidingService = false; + if (resourceNode instanceof ProvidingService) { + foundProvidingService = true; + processProvidingService(resourceModel, resourceNodeTemplate, resourceNode); + } else if (resourceNode instanceof Resource && !(resourceNode.getWidgetType().equals(Widget.Type.L3_NET))) { + if (metaData != null) { + resourceNode.populateModelIdentificationInformation(metaData.getAllProperties()); + } + idTypeStore.put(resourceNode.getModelNameVersionId(), nodeTypeName); + resourceModel.addResource((Resource) resourceNode); + } + return foundProvidingService; + } + + private void processProvidingService(Model resourceModel, NodeTemplate resourceNodeTemplate, Model resourceNode) { + Map<String, Property> nodeProperties = resourceNodeTemplate.getProperties(); + if (nodeProperties.get("providing_service_uuid") == null + || nodeProperties.get("providing_service_invariant_uuid") == null) { + throw new IllegalArgumentException( + String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, resourceModel.getModelId())); + } + Map<String, String> properties = populateStringProperties(nodeProperties); + properties.put(VERSION, "1.0"); + resourceNode.populateModelIdentificationInformation(properties); + resourceModel.addResource((Resource) resourceNode); + } + } 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 ca4cfa2..505378e 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 @@ -26,7 +26,9 @@ import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; + 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; @@ -37,6 +39,7 @@ 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.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.cl.api.Logger; @@ -51,95 +54,120 @@ public class AaiArtifactGenerator implements ArtifactGenerator { private static final String MDC_PARAM_MODEL_INFO = "ARTIFACT_MODEL_INFO"; private static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml"; - private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA = "Service tosca missing from list of input artifacts"; - private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION = "Cannot generate artifacts. Service version is not specified"; - private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = "Cannot generate artifacts. Service version is incorrect"; + private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA = + "Service tosca missing from list of input artifacts"; + private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION = + "Cannot generate artifacts. Service version is not specified"; + private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = + "Cannot generate artifacts. Service version is incorrect"; private AaiModelGenerator modelGenerator = new AaiModelGeneratorImpl(); @Override public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input, Map<String, String> additionalParams) { - Path path = null; + Path csarPath; + + try { + csarPath = createTempFile(csarArchive); + } catch (IOException e) { + log.error(ApplicationMsgs.TEMP_FILE_ERROR, e); + return createErrorData(e); + } try { ArtifactGeneratorToscaParser.initWidgetConfiguration(); ArtifactGeneratorToscaParser.initGroupFilterConfiguration(); - String serviceVersion = validateServiceVersion(additionalParams); - GenerationData generationData = new GenerationData(); + ISdcCsarHelper csarHelper = + SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString()); + return generateService(validateServiceVersion(additionalParams), csarHelper); + } catch (Exception e) { + log.error(ApplicationMsgs.INVALID_CSAR_FILE, e); + return createErrorData(e); + } finally { + FileUtils.deleteQuietly(csarPath.toFile()); + } + } - path = createTempFile(csarArchive); - if (path != null) { - ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance() - .getSdcCsarHelper(path.toAbsolutePath().toString()); + private GenerationData createErrorData(Exception e) { + GenerationData generationData = new GenerationData(); + generationData.add(ArtifactType.AAI.name(), e.getMessage()); + return generationData; + } - List<NodeTemplate> serviceNodes = csarHelper.getServiceNodeTemplates(); - Map<String, String> serviceMetaData = csarHelper.getServiceMetadataAllProperties(); + /** + * Generate model artifacts for the Service and its associated Resources. + * + * @param serviceVersion + * @param csarHelper TOSCA parser + * @return the generated Artifacts + */ + private GenerationData generateService(final String serviceVersion, ISdcCsarHelper csarHelper) { + List<NodeTemplate> serviceNodes = csarHelper.getServiceNodeTemplates(); + if (serviceNodes == null) { + throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); + } - 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()); - // Populate basic service model metadata - Service serviceModel = new Service(); - serviceModel.populateModelIdentificationInformation(serviceMetaData); - serviceModel.setModelVersion(serviceVersion); + Map<String, String> idTypeStore = new HashMap<>(); - Map<String, String> idTypeStore = new HashMap<>(); + ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); + if (!serviceNodes.isEmpty()) { + parser.processServiceTosca(serviceModel, idTypeStore, serviceNodes); + } - ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); - if (!serviceNodes.isEmpty()) { - parser.processServiceTosca(serviceModel, idTypeStore, serviceNodes); - } + // Process the resource TOSCA files + List<Resource> resources = parser.processResourceToscas(serviceNodes, idTypeStore); - // Process the resource TOSCA files - List<Resource> resources = parser.processResourceToscas(serviceNodes, idTypeStore); + MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); + String aaiServiceModel = modelGenerator.generateModelFor(serviceModel); - MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); - String aaiServiceModel = modelGenerator.generateModelFor(serviceModel); - generationData.add(getServiceArtifact(serviceModel, aaiServiceModel)); + GenerationData generationData = new GenerationData(); + generationData.add(getServiceArtifact(serviceModel, aaiServiceModel)); - // Generate AAI XML resource model - for (Resource res : resources) { - log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model"); - MDC.put(MDC_PARAM_MODEL_INFO, res.getModelName() + "," + getArtifactLabel(res)); - String aaiResourceModel = modelGenerator.generateModelFor(res); - generationData.add(getResourceArtifact(res, aaiResourceModel)); + // Generate AAI XML resource model + for (Resource resource : resources) { + generateResourceArtifact(generationData, resource); + for (Resource childResource : resource.getResources()) { + if (!(childResource instanceof ProvidingService)) { + generateResourceArtifact(generationData, childResource); } } - return generationData; - } catch (Exception e) { - log.error(ApplicationMsgs.INVALID_CSAR_FILE, e); - GenerationData generationData = new GenerationData(); - generationData.add(ArtifactType.AAI.name(), e.getMessage()); - return generationData; - - } finally { - if (path != null) { - FileUtils.deleteQuietly(path.toFile()); - } } + + return generationData; } - private Path createTempFile(byte[] bytes) { - Path path = null; - try { - log.debug("Creating temp file on file system for the csar"); - path = Files.createTempFile("temp", ".csar"); - Files.write(path, bytes); - } catch (IOException e) { - log.error(ApplicationMsgs.TEMP_FILE_ERROR, e); + /** + * @param generationData + * @param resource + */ + private void generateResourceArtifact(GenerationData generationData, Resource resource) { + if (!isContained(generationData, getArtifactName(resource))) { + log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model"); + Artifact resourceArtifact = getResourceArtifact(resource, modelGenerator.generateModelFor(resource)); + generationData.add(resourceArtifact); } + } + + private Path createTempFile(byte[] bytes) throws IOException { + log.debug("Creating temp file on file system for the csar"); + Path path = Files.createTempFile("temp", ".csar"); + Files.write(path, bytes); return path; } /** - * Method to generate the artifact label for AAI model + * Create the artifact label for an AAI model. * * @param model * @return the artifact label as String */ - public String getArtifactLabel(Model model) { + private String getArtifactLabel(Model model) { StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name()); artifactName.append("-"); artifactName.append(model.getModelType().name().toLowerCase()); @@ -151,8 +179,7 @@ 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) { @@ -175,31 +202,38 @@ 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) { + final String resourceArtifactLabel = getArtifactLabel(resourceModel); + MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel); + final byte[] bytes = aaiResourceModel.getBytes(); + Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(), - GeneratorUtil.checkSum(aaiResourceModel.getBytes()), GeneratorUtil.encode(aaiResourceModel.getBytes())); - String resourceArtifactName = getArtifactName(resourceModel); - String resourceArtifactLabel = getArtifactLabel(resourceModel); - artifact.setName(resourceArtifactName); + GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes)); + artifact.setName(getArtifactName(resourceModel)); artifact.setLabel(resourceArtifactLabel); - String description = ArtifactGeneratorToscaParser.getArtifactDescription(resourceModel); - artifact.setDescription(description); + artifact.setDescription(ArtifactGeneratorToscaParser.getArtifactDescription(resourceModel)); return artifact; } /** + * @param generationData + * @param artifactName + * @return + */ + private boolean isContained(GenerationData generationData, final String artifactName) { + return generationData.getResultData().stream() + .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName)); + } + + /** * Create Service artifact model from the AAI xml model string. * - * @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) { @@ -231,8 +265,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } private String validateServiceVersion(Map<String, String> additionalParams) { - String serviceVersion; - serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName()); + String serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName()); if (serviceVersion == null) { throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION); } else { diff --git a/src/main/resources/babel-logging-resources.properties b/src/main/resources/babel-logging-resources.properties index 5213103..167c369 100644 --- a/src/main/resources/babel-logging-resources.properties +++ b/src/main/resources/babel-logging-resources.properties @@ -84,4 +84,9 @@ MISSING_REQUEST_ID=\ MISSING_SERVICE_METADATA=\ BABEL0011W|\ - The service node template: {0} does not provide a metadata section. Skipping...|\
\ No newline at end of file + The service node template: {0} does not provide a metadata section. Skipping...|\ + +TEMP_FILE_ERROR=\ + BABEL0012E|\ + Error creating temporary CSAR file.|\ +
\ No newline at end of file |