2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 2017-2019 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.xml.generator.api;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.Optional;
32 import java.util.stream.Collectors;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.aai.babel.logging.ApplicationMsgs;
36 import org.onap.aai.babel.logging.LogHelper;
37 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
38 import org.onap.aai.babel.parser.ToscaParser;
39 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
40 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
41 import org.onap.aai.babel.xml.generator.data.Artifact;
42 import org.onap.aai.babel.xml.generator.data.ArtifactType;
43 import org.onap.aai.babel.xml.generator.data.GenerationData;
44 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
45 import org.onap.aai.babel.xml.generator.data.GroupType;
46 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
47 import org.onap.aai.babel.xml.generator.model.Model;
48 import org.onap.aai.babel.xml.generator.model.Resource;
49 import org.onap.aai.babel.xml.generator.model.Service;
50 import org.onap.aai.babel.xml.generator.model.Widget;
51 import org.onap.aai.babel.xml.generator.model.WidgetType;
52 import org.onap.aai.babel.xml.generator.types.ModelType;
53 import org.onap.aai.cl.api.Logger;
54 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
55 import org.onap.sdc.tosca.parser.enums.SdcTypes;
56 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
57 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
58 import org.onap.sdc.toscaparser.api.Group;
59 import org.onap.sdc.toscaparser.api.NodeTemplate;
60 import org.onap.sdc.toscaparser.api.elements.Metadata;
63 public class AaiArtifactGenerator implements ArtifactGenerator {
65 private static Logger log = LogHelper.INSTANCE;
67 private static final String MDC_PARAM_MODEL_INFO = "ARTIFACT_MODEL_INFO";
68 private static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml";
69 private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA =
70 "Service tosca missing from list of input artifacts";
71 private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION =
72 "Cannot generate artifacts. Service version is not specified";
73 private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION =
74 "Cannot generate artifacts. Service version is incorrect";
76 private AaiModelGenerator modelGenerator = new AaiModelGenerator();
79 public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input,
80 Map<String, String> additionalParams) {
81 String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
82 if (configLocation == null) {
83 throw new IllegalArgumentException(
84 String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND,
85 ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
89 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
90 } catch (IOException e) {
91 log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation);
92 return createErrorData(e);
98 csarPath = createTempFile(csarArchive);
99 } catch (IOException e) {
100 log.error(ApplicationMsgs.TEMP_FILE_ERROR, e);
101 return createErrorData(e);
105 ISdcCsarHelper csarHelper =
106 SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString());
107 return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper);
108 } catch (SdcToscaParserException | XmlArtifactGenerationException e) {
109 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
110 return createErrorData(e);
112 FileUtils.deleteQuietly(csarPath.toFile());
116 private GenerationData createErrorData(Exception e) {
117 GenerationData generationData = new GenerationData();
118 generationData.add(ArtifactType.AAI.name(), e.getMessage());
119 return generationData;
123 * Generate model artifacts for the Service and its associated Resources.
125 * @param serviceVersion
127 * interface to the TOSCA parser
128 * @return the generated Artifacts (containing XML models)
129 * @throws XmlArtifactGenerationException
130 * if the configured widget mappings do not support processed widget type(s)
132 public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper)
133 throws XmlArtifactGenerationException {
134 List<NodeTemplate> serviceNodeTemplates =
135 ToscaParser.getServiceNodeTemplates(csarHelper).collect(Collectors.toList());
136 if (serviceNodeTemplates == null) {
137 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
140 Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
142 MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
144 List<Resource> resources = generateResourceModels(csarHelper, serviceNodeTemplates, serviceModel);
146 // Generate the A&AI XML model for the Service.
147 final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
149 // Build a Babel Artifact to be returned to the caller.
150 GenerationData generationData = new GenerationData();
151 generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
153 // For each Resource, generate the A&AI XML model and then create an additional Artifact for that model.
154 for (Resource resource : resources) {
155 generateResourceArtifact(generationData, resource);
156 for (Resource childResource : resource.getResources()) {
157 boolean isProvidingService =
158 (boolean) Optional.ofNullable(childResource.getProperties().get("providingService")) //
160 if (!isProvidingService) {
161 generateResourceArtifact(generationData, childResource);
166 return generationData;
170 * Create a Service from the provided metadata
172 * @param serviceVersion
176 private Service createServiceModel(final String serviceVersion, Map<String, String> properties) {
177 log.debug("Processing (TOSCA) Service object");
178 Service serviceModel = new Service();
179 serviceModel.setModelVersion(serviceVersion);
180 serviceModel.populateModelIdentificationInformation(properties);
186 * @param serviceNodeTemplates
187 * @param serviceModel
188 * @return the generated Models
189 * @throws XmlArtifactGenerationException
190 * if the configured widget mappings do not support processed widget type(s)
192 private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
193 Service serviceModel) throws XmlArtifactGenerationException {
194 final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
196 List<Resource> resources = new ArrayList<>();
198 final List<Group> serviceGroups = ToscaParser.getServiceLevelGroups(csarHelper);
199 for (NodeTemplate nodeTemplate : serviceNodeTemplates) {
200 if (nodeTemplate.getMetaData() != null) {
201 generateModelFromNodeTemplate(csarHelper, serviceModel, resources, serviceGroups, parser, nodeTemplate);
203 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
212 * @param serviceModel
214 * @param serviceGroups
216 * @param nodeTemplate
217 * @throws XmlArtifactGenerationException
218 * if the configured widget mappings do not support processed widget type(s)
220 private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
221 List<Resource> resources, final List<Group> serviceGroups, ArtifactGeneratorToscaParser parser,
222 NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
223 Resource model = getModelFor(parser, nodeTemplate);
226 if (nodeTemplate.getMetaData() != null) {
227 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
230 parser.addRelatedModel(serviceModel, model);
231 if (model.getModelType() == ModelType.RESOURCE) {
232 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
235 for (Group group : serviceGroups) {
236 ArrayList<String> members = group.getMembers();
237 if (members != null && members.contains(nodeTemplate.getName())
238 && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
239 log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(),
240 group.getType(), members));
242 Resource groupModel = parser.createInstanceGroupModel(
243 parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
244 serviceModel.addResource(groupModel);
245 resources.add(groupModel);
251 private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
252 String nodeTypeName = nodeTemplate.getType();
254 log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
256 Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
259 Metadata metadata = nodeTemplate.getMetaData();
260 if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties())
261 && model.hasWidgetType("VF")) {
262 model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
273 * @param serviceVfNode
274 * a VF resource Node Template
275 * @throws XmlArtifactGenerationException
276 * if the configured widget mappings do not support processed widget type(s)
278 private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
279 ArtifactGeneratorToscaParser parser, NodeTemplate serviceVfNode) throws XmlArtifactGenerationException {
280 Resource resourceModel = getModelFor(parser, serviceVfNode);
281 if (resourceModel == null) {
282 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model");
286 Map<String, String> serviceMetadata = serviceVfNode.getMetaData().getAllProperties();
287 resourceModel.populateModelIdentificationInformation(serviceMetadata);
289 parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode));
291 List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
292 .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
294 if (serviceVfList != null) {
295 parser.processVfModules(resources, resourceModel, serviceVfNode);
298 if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
299 resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT"));
302 resources.addAll(parser.processInstanceGroups(resourceModel, serviceVfNode));
303 resources.add(resourceModel);
307 * Return all child Node Templates (via Substitution Mappings) that do not have a type ending VnfConfiguration.
309 * @param nodeTemplate
310 * the parent Node Template
311 * @return the child Node Templates which are not a VNF Configuration type
313 private List<NodeTemplate> getNonVnfChildren(NodeTemplate nodeTemplate) {
314 return Optional.ofNullable(nodeTemplate.getSubMappingToscaTemplate()) //
315 .map(sm -> Optional.ofNullable(sm.getNodeTemplates())
316 .map(nts -> nts.stream().filter(nt -> !isVNFType(nt)) //
317 .collect(Collectors.toList()))
318 .orElse(Collections.emptyList()))
319 .orElse(Collections.emptyList());
322 private boolean isVNFType(NodeTemplate nt) {
323 return nt.getType().endsWith("VnfConfiguration");
327 * @param generationData
329 * @throws XmlArtifactGenerationException
331 private void generateResourceArtifact(GenerationData generationData, Resource resource)
332 throws XmlArtifactGenerationException {
333 if (!isContained(generationData, getArtifactName(resource))) {
334 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
335 generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
339 private Path createTempFile(byte[] bytes) throws IOException {
340 log.debug("Creating temp file on file system for the csar");
341 Path path = Files.createTempFile("temp", ".csar");
342 Files.write(path, bytes);
347 * Create the artifact label for an AAI model.
350 * @return the artifact label as String
352 private String getArtifactLabel(Model model) {
353 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
354 artifactName.append("-");
355 artifactName.append(model.getModelTypeName());
356 artifactName.append("-");
357 artifactName.append(hashCodeUuId(model.getModelNameVersionId()));
358 return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-");
362 * Method to generate the artifact name for an AAI model.
366 * @return Model artifact name
368 private String getArtifactName(Model model) {
369 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
370 artifactName.append("-");
372 String truncatedArtifactName = truncateName(model.getModelName());
373 artifactName.append(truncatedArtifactName);
375 artifactName.append("-");
376 artifactName.append(model.getModelTypeName());
377 artifactName.append("-");
378 artifactName.append(model.getModelVersion());
380 artifactName.append(".");
381 artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
382 return artifactName.toString();
386 * Create Resource artifact model from the AAI xml model string.
388 * @param resourceModel
389 * Model of the resource artifact
390 * @param aaiResourceModel
391 * AAI model as string
392 * @return Generated {@link Artifact} model for the resource
394 private Artifact getResourceArtifact(Resource resourceModel, String aaiResourceModel) {
395 final String resourceArtifactLabel = getArtifactLabel(resourceModel);
396 MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel);
397 final byte[] bytes = aaiResourceModel.getBytes();
399 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
400 GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes));
401 artifact.setName(getArtifactName(resourceModel));
402 artifact.setLabel(resourceArtifactLabel);
403 artifact.setDescription("AAI Resource Model");
408 * @param generationData
409 * @param artifactName
412 private boolean isContained(GenerationData generationData, final String artifactName) {
413 return generationData.getResultData().stream()
414 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
418 * Create Service artifact model from the AAI XML model.
420 * @param serviceModel
421 * Model of the service artifact
422 * @param aaiServiceModel
423 * AAI model as string
424 * @return Generated {@link Artifact} model for the service
426 private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) {
427 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
428 GeneratorUtil.checkSum(aaiServiceModel.getBytes()), GeneratorUtil.encode(aaiServiceModel.getBytes()));
429 String serviceArtifactName = getArtifactName(serviceModel);
430 String serviceArtifactLabel = getArtifactLabel(serviceModel);
431 artifact.setName(serviceArtifactName);
432 artifact.setLabel(serviceArtifactLabel);
433 artifact.setDescription("AAI Service Model");
437 private int hashCodeUuId(String uuId) {
439 for (int i = 0; i < uuId.length(); i++) {
440 hashcode = 31 * hashcode + uuId.charAt(i);
445 private String truncateName(String name) {
446 String truncatedName = name;
447 if (name.length() >= 200) {
448 truncatedName = name.substring(0, 199);
450 return truncatedName;
453 private String validateServiceVersion(Map<String, String> additionalParams) {
454 String serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName());
455 if (serviceVersion == null) {
456 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
458 String versionRegex = "^[1-9]\\d*(\\.0)$";
459 if (!(serviceVersion.matches(versionRegex))) {
460 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
463 return serviceVersion;