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 | ClassCastException | 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 Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
136 MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
138 List<Resource> resources = generateResourceModels(csarHelper, serviceModel);
140 // Generate the A&AI XML model for the Service.
141 final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
143 // Build a Babel Artifact to be returned to the caller.
144 GenerationData generationData = new GenerationData();
145 generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
147 // For each Resource, generate the A&AI XML model and then create an additional Artifact for that model.
148 for (Resource resource : resources) {
149 generateResourceArtifact(generationData, resource);
150 for (Resource childResource : resource.getResources()) {
151 boolean isProvidingService =
152 (boolean) Optional.ofNullable(childResource.getProperties().get("providingService")) //
154 if (!isProvidingService) {
155 generateResourceArtifact(generationData, childResource);
160 return generationData;
164 * Create a Service from the provided metadata
166 * @param serviceVersion
170 private Service createServiceModel(final String serviceVersion, Map<String, String> properties) {
171 log.debug("Processing (TOSCA) Service object");
172 Service serviceModel = new Service();
173 serviceModel.setModelVersion(serviceVersion);
174 serviceModel.populateModelIdentificationInformation(properties);
180 * @param serviceModel
181 * @return the generated Models
182 * @throws XmlArtifactGenerationException
183 * if the configured widget mappings do not support processed widget type(s)
185 private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, Service serviceModel)
186 throws XmlArtifactGenerationException {
187 List<NodeTemplate> serviceNodeTemplates =
188 ToscaParser.getServiceNodeTemplates(csarHelper).collect(Collectors.toList());
189 if (serviceNodeTemplates == null) {
190 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
193 final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
194 List<Resource> resources = new ArrayList<>();
195 final List<Group> serviceGroups = ToscaParser.getServiceLevelGroups(csarHelper);
196 for (NodeTemplate nodeTemplate : serviceNodeTemplates) {
197 if (nodeTemplate.getMetaData() != null) {
198 generateModelFromNodeTemplate(csarHelper, serviceModel, resources, serviceGroups, parser, nodeTemplate);
200 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
209 * @param serviceModel
211 * @param serviceGroups
213 * @param nodeTemplate
214 * @throws XmlArtifactGenerationException
215 * if the configured widget mappings do not support processed widget type(s)
217 private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
218 List<Resource> resources, final List<Group> serviceGroups, ArtifactGeneratorToscaParser parser,
219 NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
220 Resource model = getModelFor(parser, nodeTemplate);
223 if (nodeTemplate.getMetaData() != null) {
224 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
227 parser.addRelatedModel(serviceModel, model);
228 if (model.getModelType() == ModelType.RESOURCE) {
229 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
232 for (Group group : serviceGroups) {
233 ArrayList<String> members = group.getMembers();
234 if (members != null && members.contains(nodeTemplate.getName())
235 && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
236 log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(),
237 group.getType(), members));
239 Resource groupModel = parser.createInstanceGroupModel(
240 parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
241 serviceModel.addResource(groupModel);
242 resources.add(groupModel);
248 private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
249 String nodeTypeName = nodeTemplate.getType();
251 log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
253 Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
256 Metadata metadata = nodeTemplate.getMetaData();
257 if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties())
258 && model.hasWidgetType("VF")) {
259 model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
270 * @param serviceVfNode
271 * a VF resource Node Template
272 * @throws XmlArtifactGenerationException
273 * if the configured widget mappings do not support processed widget type(s)
275 private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
276 ArtifactGeneratorToscaParser parser, NodeTemplate serviceVfNode) throws XmlArtifactGenerationException {
277 Resource resourceModel = getModelFor(parser, serviceVfNode);
278 if (resourceModel == null) {
279 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model");
283 Map<String, String> serviceMetadata = serviceVfNode.getMetaData().getAllProperties();
284 resourceModel.populateModelIdentificationInformation(serviceMetadata);
286 parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode));
288 List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
289 .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
291 if (serviceVfList != null) {
292 parser.processVfModules(resources, resourceModel, serviceVfNode);
295 if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
296 resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT"));
299 resources.addAll(parser.processInstanceGroups(resourceModel, serviceVfNode));
300 resources.add(resourceModel);
304 * Return all child Node Templates (via Substitution Mappings) that do not have a type ending VnfConfiguration.
306 * @param nodeTemplate
307 * the parent Node Template
308 * @return the child Node Templates which are not a VNF Configuration type
310 private List<NodeTemplate> getNonVnfChildren(NodeTemplate nodeTemplate) {
311 return Optional.ofNullable(nodeTemplate.getSubMappingToscaTemplate()) //
312 .map(sm -> Optional.ofNullable(sm.getNodeTemplates())
313 .map(nts -> nts.stream().filter(nt -> !isVNFType(nt)) //
314 .collect(Collectors.toList()))
315 .orElse(Collections.emptyList()))
316 .orElse(Collections.emptyList());
319 private boolean isVNFType(NodeTemplate nt) {
320 return nt.getType().endsWith("VnfConfiguration");
324 * @param generationData
326 * @throws XmlArtifactGenerationException
328 private void generateResourceArtifact(GenerationData generationData, Resource resource)
329 throws XmlArtifactGenerationException {
330 if (!isContained(generationData, getArtifactName(resource))) {
331 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
332 generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
336 private Path createTempFile(byte[] bytes) throws IOException {
337 log.debug("Creating temp file on file system for the csar");
338 Path path = Files.createTempFile("temp", ".csar");
339 Files.write(path, bytes);
344 * Create the artifact label for an AAI model.
347 * @return the artifact label as String
349 private String getArtifactLabel(Model model) {
350 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
351 artifactName.append("-");
352 artifactName.append(model.getModelTypeName());
353 artifactName.append("-");
354 artifactName.append(hashCodeUuId(model.getModelNameVersionId()));
355 return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-");
359 * Method to generate the artifact name for an AAI model.
363 * @return Model artifact name
365 private String getArtifactName(Model model) {
366 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
367 artifactName.append("-");
369 String truncatedArtifactName = truncateName(model.getModelName());
370 artifactName.append(truncatedArtifactName);
372 artifactName.append("-");
373 artifactName.append(model.getModelTypeName());
374 artifactName.append("-");
375 artifactName.append(model.getModelVersion());
377 artifactName.append(".");
378 artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
379 return artifactName.toString();
383 * Create Resource artifact model from the AAI xml model string.
385 * @param resourceModel
386 * Model of the resource artifact
387 * @param aaiResourceModel
388 * AAI model as string
389 * @return Generated {@link Artifact} model for the resource
391 private Artifact getResourceArtifact(Resource resourceModel, String aaiResourceModel) {
392 final String resourceArtifactLabel = getArtifactLabel(resourceModel);
393 MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel);
394 final byte[] bytes = aaiResourceModel.getBytes();
396 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
397 GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes));
398 artifact.setName(getArtifactName(resourceModel));
399 artifact.setLabel(resourceArtifactLabel);
400 artifact.setDescription("AAI Resource Model");
405 * @param generationData
406 * @param artifactName
409 private boolean isContained(GenerationData generationData, final String artifactName) {
410 return generationData.getResultData().stream()
411 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
415 * Create Service artifact model from the AAI XML model.
417 * @param serviceModel
418 * Model of the service artifact
419 * @param aaiServiceModel
420 * AAI model as string
421 * @return Generated {@link Artifact} model for the service
423 private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) {
424 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
425 GeneratorUtil.checkSum(aaiServiceModel.getBytes()), GeneratorUtil.encode(aaiServiceModel.getBytes()));
426 String serviceArtifactName = getArtifactName(serviceModel);
427 String serviceArtifactLabel = getArtifactLabel(serviceModel);
428 artifact.setName(serviceArtifactName);
429 artifact.setLabel(serviceArtifactLabel);
430 artifact.setDescription("AAI Service Model");
434 private int hashCodeUuId(String uuId) {
437 for (int i = 0; i < uuId.length(); i++) {
438 hashcode = 31 * hashcode + uuId.charAt(i);
444 private String truncateName(String name) {
445 String truncatedName = name;
446 if (name != null && name.length() >= 200) {
447 truncatedName = name.substring(0, 199);
449 return truncatedName;
452 private String validateServiceVersion(Map<String, String> additionalParams) {
453 String serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName());
454 if (serviceVersion == null) {
455 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
457 String versionRegex = "^\\d*\\.\\d*$";
458 if (!(serviceVersion.matches(versionRegex))) {
459 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
462 return serviceVersion;