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=52d342f880f3b938f7137e5c6915563cc8e59077;hb=fbd05f86edf3e4710ab454780581f0ee40e152f6;hp=57e8a673f9258562a3ad52c11df22bd8dbaf0dac;hpb=be30876e3a3872a7274c944995544836ff31913c;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 57e8a67..52d342f 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-2019 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2019 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. @@ -33,6 +33,7 @@ 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.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; @@ -43,10 +44,12 @@ 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.Resource; import org.onap.aai.babel.xml.generator.model.Service; -import org.onap.aai.babel.xml.generator.model.TunnelXconnectWidget; +import org.onap.aai.babel.xml.generator.model.Widget; import org.onap.aai.babel.xml.generator.model.Widget.Type; +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.exceptions.SdcToscaParserException; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.Group; import org.onap.sdc.toscaparser.api.NodeTemplate; @@ -66,20 +69,11 @@ 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) { - Path csarPath; - - try { - csarPath = createTempFile(csarArchive); - } catch (IOException e) { - log.error(ApplicationMsgs.TEMP_FILE_ERROR, e); - return createErrorData(e); - } - String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE); if (configLocation == null) { throw new IllegalArgumentException( @@ -90,10 +84,25 @@ public class AaiArtifactGenerator implements ArtifactGenerator { try { ArtifactGeneratorToscaParser.initWidgetConfiguration(); ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation); + } catch (IOException e) { + log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation); + return createErrorData(e); + } + + Path csarPath; + + try { + csarPath = createTempFile(csarArchive); + } catch (IOException e) { + log.error(ApplicationMsgs.TEMP_FILE_ERROR, e); + return createErrorData(e); + } + + try { ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString()); return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper); - } catch (Exception e) { + } catch (SdcToscaParserException | XmlArtifactGenerationException e) { log.error(ApplicationMsgs.INVALID_CSAR_FILE, e); return createErrorData(e); } finally { @@ -112,10 +121,12 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * * @param serviceVersion * @param csarHelper - * interface to the TOSCA parser + * interface to the TOSCA parser * @return the generated Artifacts (containing XML models) + * @throws XmlArtifactGenerationException */ - private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) { + private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) + throws XmlArtifactGenerationException { List serviceNodeTemplates = csarHelper.getServiceNodeTemplates(); if (serviceNodeTemplates == null) { throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); @@ -170,9 +181,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * @param serviceNodeTemplates * @param serviceModel * @return the generated Models + * @throws XmlArtifactGenerationException */ private List generateResourceModels(ISdcCsarHelper csarHelper, List serviceNodeTemplates, - Service serviceModel) { + Service serviceModel) throws XmlArtifactGenerationException { final List serviceGroups = csarHelper.getGroupsOfTopologyTemplate(); final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper); @@ -191,7 +203,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel, List resources, final List serviceGroups, ArtifactGeneratorToscaParser parser, - NodeTemplate nodeTemplate) { + NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { Resource model = getModelFor(parser, nodeTemplate); if (model != null) { @@ -200,7 +212,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } parser.addRelatedModel(serviceModel, model); - if (model.isResource()) { + if (model.getModelType() == ModelType.RESOURCE) { generateResourceModel(csarHelper, resources, parser, nodeTemplate); } } else { @@ -239,7 +251,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } private void generateResourceModel(ISdcCsarHelper csarHelper, List resources, - ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) { + ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { Resource resourceModel = getModelFor(parser, nodeTemplate); if (resourceModel == null) { log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model"); @@ -256,7 +268,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) { - resourceModel.addWidget(new TunnelXconnectWidget()); + resourceModel.addWidget(Widget.getWidget(Type.TUNNEL_XCONNECT)); } resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate)); @@ -266,8 +278,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator { /** * @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"); generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource))); @@ -290,7 +304,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 +]+", "-"); @@ -300,7 +314,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * Method to generate the artifact name for an AAI model. * * @param model - * AAI artifact model + * AAI artifact model * @return Model artifact name */ private String getArtifactName(Model model) { @@ -311,7 +325,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()); @@ -324,12 +338,12 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * Create Resource artifact model from the AAI xml model string. * * @param resourceModel - * Model of the resource artifact + * Model of the resource artifact * @param aaiResourceModel - * AAI model as string + * 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(); @@ -338,7 +352,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; } @@ -356,9 +370,9 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * Create Service artifact model from the AAI XML model. * * @param serviceModel - * Model of the service artifact + * Model of the service artifact * @param aaiServiceModel - * AAI model as string + * AAI model as string * @return Generated {@link Artifact} model for the service */ private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) { @@ -368,8 +382,7 @@ 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; }