Depend on sdc-tosca version 1.5.0
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / api / AaiArtifactGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.xml.generator.api;
23
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;
30 import java.util.Map;
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;
61 import org.slf4j.MDC;
62
63 public class AaiArtifactGenerator implements ArtifactGenerator {
64
65     private static Logger log = LogHelper.INSTANCE;
66
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";
75
76     private AaiModelGenerator modelGenerator = new AaiModelGenerator();
77
78     @Override
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));
86         }
87
88         try {
89             ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
90         } catch (IOException e) {
91             log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation);
92             return createErrorData(e);
93         }
94
95         Path csarPath;
96
97         try {
98             csarPath = createTempFile(csarArchive);
99         } catch (IOException e) {
100             log.error(ApplicationMsgs.TEMP_FILE_ERROR, e);
101             return createErrorData(e);
102         }
103
104         try {
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);
111         } finally {
112             FileUtils.deleteQuietly(csarPath.toFile());
113         }
114     }
115
116     private GenerationData createErrorData(Exception e) {
117         GenerationData generationData = new GenerationData();
118         generationData.add(ArtifactType.AAI.name(), e.getMessage());
119         return generationData;
120     }
121
122     /**
123      * Generate model artifacts for the Service and its associated Resources.
124      *
125      * @param serviceVersion
126      * @param csarHelper
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)
131      */
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);
138         }
139
140         Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
141
142         MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
143
144         List<Resource> resources = generateResourceModels(csarHelper, serviceNodeTemplates, serviceModel);
145
146         // Generate the A&AI XML model for the Service.
147         final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
148
149         // Build a Babel Artifact to be returned to the caller.
150         GenerationData generationData = new GenerationData();
151         generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
152
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")) //
159                                 .orElse(false);
160                 if (!isProvidingService) {
161                     generateResourceArtifact(generationData, childResource);
162                 }
163             }
164         }
165
166         return generationData;
167     }
168
169     /**
170      * Create a Service from the provided metadata
171      *
172      * @param serviceVersion
173      * @param properties
174      * @return
175      */
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);
181         return serviceModel;
182     }
183
184     /**
185      * @param csarHelper
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)
191      */
192     private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
193             Service serviceModel) throws XmlArtifactGenerationException {
194         final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
195
196         List<Resource> resources = new ArrayList<>();
197
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);
202             } else {
203                 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
204             }
205         }
206
207         return resources;
208     }
209
210     /**
211      * @param csarHelper
212      * @param serviceModel
213      * @param resources
214      * @param serviceGroups
215      * @param parser
216      * @param nodeTemplate
217      * @throws XmlArtifactGenerationException
218      *             if the configured widget mappings do not support processed widget type(s)
219      */
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);
224
225         if (model != null) {
226             if (nodeTemplate.getMetaData() != null) {
227                 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
228             }
229
230             parser.addRelatedModel(serviceModel, model);
231             if (model.getModelType() == ModelType.RESOURCE) {
232                 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
233             }
234         } else {
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));
241
242                     Resource groupModel = parser.createInstanceGroupModel(
243                             parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
244                     serviceModel.addResource(groupModel);
245                     resources.add(groupModel);
246                 }
247             }
248         }
249     }
250
251     private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
252         String nodeTypeName = nodeTemplate.getType();
253
254         log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
255
256         Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
257
258         if (model != null) {
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);
263             }
264         }
265
266         return model;
267     }
268
269     /**
270      * @param csarHelper
271      * @param resources
272      * @param parser
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)
277      */
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");
283             return;
284         }
285
286         Map<String, String> serviceMetadata = serviceVfNode.getMetaData().getAllProperties();
287         resourceModel.populateModelIdentificationInformation(serviceMetadata);
288
289         parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode));
290
291         List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
292                 .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
293
294         if (serviceVfList != null) {
295             parser.processVfModules(resources, resourceModel, serviceVfNode);
296         }
297
298         if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
299             resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT"));
300         }
301
302         resources.addAll(parser.processInstanceGroups(resourceModel, serviceVfNode));
303         resources.add(resourceModel);
304     }
305
306     /**
307      * Return all child Node Templates (via Substitution Mappings) that do not have a type ending VnfConfiguration.
308      *
309      * @param nodeTemplate
310      *            the parent Node Template
311      * @return the child Node Templates which are not a VNF Configuration type
312      */
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());
320     }
321
322     private boolean isVNFType(NodeTemplate nt) {
323         return nt.getType().endsWith("VnfConfiguration");
324     }
325
326     /**
327      * @param generationData
328      * @param resource
329      * @throws XmlArtifactGenerationException
330      */
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)));
336         }
337     }
338
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);
343         return path;
344     }
345
346     /**
347      * Create the artifact label for an AAI model.
348      *
349      * @param model
350      * @return the artifact label as String
351      */
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 +]+", "-");
359     }
360
361     /**
362      * Method to generate the artifact name for an AAI model.
363      *
364      * @param model
365      *            AAI artifact model
366      * @return Model artifact name
367      */
368     private String getArtifactName(Model model) {
369         StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
370         artifactName.append("-");
371
372         String truncatedArtifactName = truncateName(model.getModelName());
373         artifactName.append(truncatedArtifactName);
374
375         artifactName.append("-");
376         artifactName.append(model.getModelTypeName());
377         artifactName.append("-");
378         artifactName.append(model.getModelVersion());
379
380         artifactName.append(".");
381         artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
382         return artifactName.toString();
383     }
384
385     /**
386      * Create Resource artifact model from the AAI xml model string.
387      *
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
393      */
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();
398
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");
404         return artifact;
405     }
406
407     /**
408      * @param generationData
409      * @param artifactName
410      * @return
411      */
412     private boolean isContained(GenerationData generationData, final String artifactName) {
413         return generationData.getResultData().stream()
414                 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
415     }
416
417     /**
418      * Create Service artifact model from the AAI XML model.
419      *
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
425      */
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");
434         return artifact;
435     }
436
437     private int hashCodeUuId(String uuId) {
438         int hashcode = 0;
439         for (int i = 0; i < uuId.length(); i++) {
440             hashcode = 31 * hashcode + uuId.charAt(i);
441         }
442         return hashcode;
443     }
444
445     private String truncateName(String name) {
446         String truncatedName = name;
447         if (name.length() >= 200) {
448             truncatedName = name.substring(0, 199);
449         }
450         return truncatedName;
451     }
452
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);
457         } else {
458             String versionRegex = "^[1-9]\\d*(\\.0)$";
459             if (!(serviceVersion.matches(versionRegex))) {
460                 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
461             }
462         }
463         return serviceVersion;
464     }
465 }