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.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;
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.TunnelXconnectWidget;
import org.onap.aai.cl.api.Logger;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
+import org.onap.sdc.toscaparser.api.Group;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.slf4j.MDC;
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();
- String serviceVersion = validateServiceVersion(additionalParams);
- GenerationData generationData = new GenerationData();
+ ArtifactGeneratorToscaParser.initGroupFilterConfiguration();
+ ISdcCsarHelper csarHelper =
+ SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString());
+ return generateAllArtifacts(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
+ * interface to the TOSCA parser
+ * @return the generated Artifacts (containing XML models)
+ */
+ private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) {
+ List<NodeTemplate> serviceNodeTemplates = csarHelper.getServiceNodeTemplates();
+ if (serviceNodeTemplates == null) {
+ throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
+ }
- if (serviceNodes == null) {
- throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
- }
+ Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
- // Populate basic service model metadata
- Service serviceModel = new Service();
- serviceModel.populateModelIdentificationInformation(serviceMetaData);
- serviceModel.setModelVersion(serviceVersion);
+ MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
- Map<String, String> idTypeStore = new HashMap<>();
+ List<Resource> resources = generateResourceModels(csarHelper, serviceNodeTemplates, serviceModel);
- ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
- if (!serviceNodes.isEmpty()) {
- parser.processServiceTosca(serviceModel, idTypeStore, serviceNodes);
+ // 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, serviceArtifact));
+
+ // 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)) {
+ generateResourceArtifact(generationData, childResource);
}
+ }
+ }
- // Process the resource TOSCA files
- List<Resource> resources = parser.processResourceToscas(serviceNodes, idTypeStore);
+ return generationData;
+ }
+
+ /**
+ * Create a Service from the provided metadata
+ *
+ * @param serviceVersion
+ * @param properties
+ * @return
+ */
+ private Service createServiceModel(final String serviceVersion, Map<String, String> properties) {
+ log.debug("Processing (TOSCA) Service object");
+ Service serviceModel = new Service();
+ serviceModel.setModelVersion(serviceVersion);
+ serviceModel.populateModelIdentificationInformation(properties);
+ return serviceModel;
+ }
- MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
- String aaiServiceModel = modelGenerator.generateModelFor(serviceModel);
- generationData.add(getServiceArtifact(serviceModel, aaiServiceModel));
+ /**
+ * @param csarHelper
+ * @param serviceNodeTemplates
+ * @param serviceModel
+ * @return the generated Models
+ */
+ private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
+ Service serviceModel) {
+ final List<Group> serviceGroups = csarHelper.getGroupsOfTopologyTemplate();
+ final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
- // Generate AAI XML resource model
- for (Resource res : resources) {
- MDC.put(MDC_PARAM_MODEL_INFO, res.getModelName() + "," + getArtifactLabel(res));
- String aaiResourceModel = modelGenerator.generateModelFor(res);
- generationData.add(getResourceArtifact(res, aaiResourceModel));
+ List<Resource> resources = new ArrayList<>();
- }
+ 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 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 resources;
+ }
+
+ private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
+ List<Resource> resources, final List<Group> serviceGroups, ArtifactGeneratorToscaParser parser,
+ NodeTemplate nodeTemplate) {
+ String nodeTypeName = parser.normaliseNodeTypeName(nodeTemplate);
+ Model model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
+ if (model != null) {
+ if (nodeTemplate.getMetaData() != null) {
+ model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
+ }
+
+ parser.addRelatedModel(serviceModel, model);
+ if (model instanceof Resource) {
+ generateResourceModel(csarHelper, resources, parser, nodeTemplate, nodeTypeName);
+ }
+ } else {
+ for (Group group : serviceGroups) {
+ if (group.getMembers().contains(nodeTemplate.getName())
+ && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
+ log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(),
+ group.getType(), group.getMembers()));
+
+ Resource groupModel = parser.createInstanceGroupModel(
+ parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
+ serviceModel.addResource(groupModel);
+ resources.add(groupModel);
+ }
}
}
}
- 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);
+ private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
+ ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate, String nodeTypeName) {
+ log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
+ Model resourceModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
+
+ Map<String, String> serviceMetadata = nodeTemplate.getMetaData().getAllProperties();
+ resourceModel.populateModelIdentificationInformation(serviceMetadata);
+
+ parser.processResourceModels(resourceModel, csarHelper.getNodeTemplateChildren(nodeTemplate));
+
+ if (csarHelper.getServiceVfList() != null) {
+ parser.processVfModules(resources, resourceModel, nodeTemplate);
}
+
+ if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
+ resourceModel.addWidget(new TunnelXconnectWidget());
+ }
+
+ resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate));
+ resources.add((Resource) resourceModel);
+ }
+
+ /**
+ * @param generationData
+ * @param resource
+ */
+ private void generateResourceArtifact(GenerationData generationData, Resource resource) {
+ if (!isContained(generationData, getArtifactName(resource))) {
+ log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
+ generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
+ }
+ }
+
+ 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());
* @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;
}
/**
- * Create Service artifact model from the AAI xml model string.
+ * @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.
*
* @param serviceModel
* Model of the service artifact
}
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 {
String versionRegex = "^[1-9]\\d*(\\.0)$";
if (!(serviceVersion.matches(versionRegex))) {
- throw new IllegalArgumentException(
- String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
+ throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
}
}
return serviceVersion;