This enhancement will enable Babel to process artifacts of version n.n
[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 | ClassCastException | 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         Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
135
136         MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
137
138         List<Resource> resources = generateResourceModels(csarHelper, serviceModel);
139
140         // Generate the A&AI XML model for the Service.
141         final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
142
143         // Build a Babel Artifact to be returned to the caller.
144         GenerationData generationData = new GenerationData();
145         generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
146
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")) //
153                                 .orElse(false);
154                 if (!isProvidingService) {
155                     generateResourceArtifact(generationData, childResource);
156                 }
157             }
158         }
159
160         return generationData;
161     }
162
163     /**
164      * Create a Service from the provided metadata
165      *
166      * @param serviceVersion
167      * @param properties
168      * @return
169      */
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);
175         return serviceModel;
176     }
177
178     /**
179      * @param csarHelper
180      * @param serviceModel
181      * @return the generated Models
182      * @throws XmlArtifactGenerationException
183      *             if the configured widget mappings do not support processed widget type(s)
184      */
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);
191         }
192
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);
199             } else {
200                 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
201             }
202         }
203
204         return resources;
205     }
206
207     /**
208      * @param csarHelper
209      * @param serviceModel
210      * @param resources
211      * @param serviceGroups
212      * @param parser
213      * @param nodeTemplate
214      * @throws XmlArtifactGenerationException
215      *             if the configured widget mappings do not support processed widget type(s)
216      */
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);
221
222         if (model != null) {
223             if (nodeTemplate.getMetaData() != null) {
224                 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
225             }
226
227             parser.addRelatedModel(serviceModel, model);
228             if (model.getModelType() == ModelType.RESOURCE) {
229                 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
230             }
231         } else {
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));
238
239                     Resource groupModel = parser.createInstanceGroupModel(
240                             parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
241                     serviceModel.addResource(groupModel);
242                     resources.add(groupModel);
243                 }
244             }
245         }
246     }
247
248     private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
249         String nodeTypeName = nodeTemplate.getType();
250
251         log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
252
253         Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
254
255         if (model != null) {
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);
260             }
261         }
262
263         return model;
264     }
265
266     /**
267      * @param csarHelper
268      * @param resources
269      * @param parser
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)
274      */
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");
280             return;
281         }
282
283         Map<String, String> serviceMetadata = serviceVfNode.getMetaData().getAllProperties();
284         resourceModel.populateModelIdentificationInformation(serviceMetadata);
285
286         parser.processResourceModels(resourceModel, getNonVnfChildren(serviceVfNode));
287
288         List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
289                 .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
290
291         if (serviceVfList != null) {
292             parser.processVfModules(resources, resourceModel, serviceVfNode);
293         }
294
295         if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
296             resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT"));
297         }
298
299         resources.addAll(parser.processInstanceGroups(resourceModel, serviceVfNode));
300         resources.add(resourceModel);
301     }
302
303     /**
304      * Return all child Node Templates (via Substitution Mappings) that do not have a type ending VnfConfiguration.
305      *
306      * @param nodeTemplate
307      *            the parent Node Template
308      * @return the child Node Templates which are not a VNF Configuration type
309      */
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());
317     }
318
319     private boolean isVNFType(NodeTemplate nt) {
320         return nt.getType().endsWith("VnfConfiguration");
321     }
322
323     /**
324      * @param generationData
325      * @param resource
326      * @throws XmlArtifactGenerationException
327      */
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)));
333         }
334     }
335
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);
340         return path;
341     }
342
343     /**
344      * Create the artifact label for an AAI model.
345      *
346      * @param model
347      * @return the artifact label as String
348      */
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 +]+", "-");
356     }
357
358     /**
359      * Method to generate the artifact name for an AAI model.
360      *
361      * @param model
362      *            AAI artifact model
363      * @return Model artifact name
364      */
365     private String getArtifactName(Model model) {
366         StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
367         artifactName.append("-");
368
369         String truncatedArtifactName = truncateName(model.getModelName());
370         artifactName.append(truncatedArtifactName);
371
372         artifactName.append("-");
373         artifactName.append(model.getModelTypeName());
374         artifactName.append("-");
375         artifactName.append(model.getModelVersion());
376
377         artifactName.append(".");
378         artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
379         return artifactName.toString();
380     }
381
382     /**
383      * Create Resource artifact model from the AAI xml model string.
384      *
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
390      */
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();
395
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");
401         return artifact;
402     }
403
404     /**
405      * @param generationData
406      * @param artifactName
407      * @return
408      */
409     private boolean isContained(GenerationData generationData, final String artifactName) {
410         return generationData.getResultData().stream()
411                 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
412     }
413
414     /**
415      * Create Service artifact model from the AAI XML model.
416      *
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
422      */
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");
431         return artifact;
432     }
433
434     private int hashCodeUuId(String uuId) {
435         int hashcode = 0;
436         if (uuId != null) {
437             for (int i = 0; i < uuId.length(); i++) {
438                 hashcode = 31 * hashcode + uuId.charAt(i);
439             }
440         }
441         return hashcode;
442     }
443
444     private String truncateName(String name) {
445         String truncatedName = name;
446         if (name != null && name.length() >= 200) {
447             truncatedName = name.substring(0, 199);
448         }
449         return truncatedName;
450     }
451
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);
456         } else {
457             String versionRegex = "^\\d*\\.\\d*$";
458             if (!(serviceVersion.matches(versionRegex))) {
459                 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
460             }
461         }
462         return serviceVersion;
463     }
464 }