Refactor model generation algorithm
[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 © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.babel.xml.generator.api;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.aai.babel.logging.ApplicationMsgs;
33 import org.onap.aai.babel.logging.LogHelper;
34 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
35 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
36 import org.onap.aai.babel.xml.generator.data.Artifact;
37 import org.onap.aai.babel.xml.generator.data.ArtifactType;
38 import org.onap.aai.babel.xml.generator.data.GenerationData;
39 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
40 import org.onap.aai.babel.xml.generator.data.GroupType;
41 import org.onap.aai.babel.xml.generator.model.Model;
42 import org.onap.aai.babel.xml.generator.model.ProvidingService;
43 import org.onap.aai.babel.xml.generator.model.Resource;
44 import org.onap.aai.babel.xml.generator.model.Service;
45 import org.onap.aai.babel.xml.generator.model.TunnelXconnectWidget;
46 import org.onap.aai.cl.api.Logger;
47 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
48 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
49 import org.onap.sdc.toscaparser.api.NodeTemplate;
50 import org.slf4j.MDC;
51
52 public class AaiArtifactGenerator implements ArtifactGenerator {
53
54         private static Logger log = LogHelper.INSTANCE;
55
56         private static final String MDC_PARAM_MODEL_INFO = "ARTIFACT_MODEL_INFO";
57         private static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml";
58         private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA = "Service tosca missing from list of input artifacts";
59         private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION = "Cannot generate artifacts. Service version is not specified";
60         private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = "Cannot generate artifacts. Service version is incorrect";
61
62         private AaiModelGenerator modelGenerator = new AaiModelGeneratorImpl();
63
64         @Override
65         public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input,
66                         Map<String, String> additionalParams) {
67                 Path csarPath;
68
69                 try {
70                         csarPath = createTempFile(csarArchive);
71                 } catch (IOException e) {
72                         log.error(ApplicationMsgs.TEMP_FILE_ERROR, e);
73                         return createErrorData(e);
74                 }
75
76                 try {
77                         ArtifactGeneratorToscaParser.initWidgetConfiguration();
78                         ArtifactGeneratorToscaParser.initGroupFilterConfiguration();
79                         ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance()
80                                         .getSdcCsarHelper(csarPath.toAbsolutePath().toString());
81                         return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper);
82                 } catch (Exception e) {
83                         log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
84                         return createErrorData(e);
85                 } finally {
86                         FileUtils.deleteQuietly(csarPath.toFile());
87                 }
88         }
89
90         private GenerationData createErrorData(Exception e) {
91                 GenerationData generationData = new GenerationData();
92                 generationData.add(ArtifactType.AAI.name(), e.getMessage());
93                 return generationData;
94         }
95
96         /**
97          * Generate model artifacts for the Service and its associated Resources.
98          *
99          * @param serviceVersion
100          * @param csarHelper
101          *            interface to the TOSCA parser
102          * @return the generated Artifacts (containing XML models)
103          */
104         private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) {
105                 List<NodeTemplate> serviceNodeTemplates = csarHelper.getServiceNodeTemplates();
106                 if (serviceNodeTemplates == null) {
107                         throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
108                 }
109
110                 Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
111
112                 MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
113
114                 List<Resource> resources = generateResourceModels(csarHelper, serviceNodeTemplates, serviceModel);
115
116                 // Generate the A&AI XML model for the Service.
117                 final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
118
119                 // Build a Babel Artifact to be returned to the caller.
120                 GenerationData generationData = new GenerationData();
121                 generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
122
123                 // For each Resource, generate the A&AI XML model and then create an additional Artifact for that model.
124                 for (Resource resource : resources) {
125                         generateResourceArtifact(generationData, resource);
126                         for (Resource childResource : resource.getResources()) {
127                                 if (!(childResource instanceof ProvidingService)) {
128                                         generateResourceArtifact(generationData, childResource);
129                                 }
130                         }
131                 }
132
133                 return generationData;
134         }
135
136         /**
137          * Create a Service from the provided metadata
138          *
139          * @param serviceVersion
140          * @param properties
141          * @return
142          */
143         private Service createServiceModel(final String serviceVersion, Map<String, String> properties) {
144                 log.debug("Processing (TOSCA) Service object");
145                 Service serviceModel = new Service();
146                 serviceModel.setModelVersion(serviceVersion);
147                 serviceModel.populateModelIdentificationInformation(properties);
148                 return serviceModel;
149         }
150
151         /**
152          * @param csarHelper
153          * @param serviceNodeTemplates
154          * @param serviceModel
155          * @return the generated Models
156          */
157         private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
158                         Service serviceModel) {
159                 final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
160
161                 List<Resource> resources = new ArrayList<>();
162
163                 for (NodeTemplate nodeTemplate : serviceNodeTemplates) {
164                         if (nodeTemplate.getMetaData() != null) {
165                                 generateModelFromNodeTemplate(csarHelper, serviceModel, resources, parser, nodeTemplate);
166                         } else {
167                                 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
168                         }
169                 }
170
171                 return resources;
172         }
173
174         private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
175                         List<Resource> resources, ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
176                 String nodeTypeName = parser.normaliseNodeTypeName(nodeTemplate);
177                 Model model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
178                 if (model != null) {
179                         if (nodeTemplate.getMetaData() != null) {
180                                 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
181                         }
182
183                         parser.addRelatedModel(serviceModel, model);
184                         if (model instanceof Resource) {
185                                 generateResourceModel(csarHelper, resources, parser, nodeTemplate, nodeTypeName);
186                         }
187                 }
188         }
189
190         private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
191                         ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate, String nodeTypeName) {
192                 log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
193                 Model resourceModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
194
195                 Map<String, String> serviceMetadata = nodeTemplate.getMetaData().getAllProperties();
196                 resourceModel.populateModelIdentificationInformation(serviceMetadata);
197
198                 parser.processResourceModels(resourceModel, csarHelper.getNodeTemplateChildren(nodeTemplate));
199
200                 if (csarHelper.getServiceVfList() != null) {
201                         parser.processVfModules(resources, resourceModel, nodeTemplate);
202                 }
203
204                 if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
205                         resourceModel.addWidget(new TunnelXconnectWidget());
206                 }
207
208                 resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate));
209                 resources.add((Resource) resourceModel);
210         }
211
212         /**
213          * @param generationData
214          * @param resource
215          */
216         private void generateResourceArtifact(GenerationData generationData, Resource resource) {
217                 if (!isContained(generationData, getArtifactName(resource))) {
218                         log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
219                         generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
220                 }
221         }
222
223         private Path createTempFile(byte[] bytes) throws IOException {
224                 log.debug("Creating temp file on file system for the csar");
225                 Path path = Files.createTempFile("temp", ".csar");
226                 Files.write(path, bytes);
227                 return path;
228         }
229
230         /**
231          * Create the artifact label for an AAI model.
232          *
233          * @param model
234          * @return the artifact label as String
235          */
236         private String getArtifactLabel(Model model) {
237                 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
238                 artifactName.append("-");
239                 artifactName.append(model.getModelType().name().toLowerCase());
240                 artifactName.append("-");
241                 artifactName.append(hashCodeUuId(model.getModelNameVersionId()));
242                 return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-");
243         }
244
245         /**
246          * Method to generate the artifact name for an AAI model.
247          *
248          * @param model
249          *            AAI artifact model
250          * @return Model artifact name
251          */
252         private String getArtifactName(Model model) {
253                 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
254                 artifactName.append("-");
255
256                 String truncatedArtifactName = truncateName(model.getModelName());
257                 artifactName.append(truncatedArtifactName);
258
259                 artifactName.append("-");
260                 artifactName.append(model.getModelType().name().toLowerCase());
261                 artifactName.append("-");
262                 artifactName.append(model.getModelVersion());
263
264                 artifactName.append(".");
265                 artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
266                 return artifactName.toString();
267         }
268
269         /**
270          * Create Resource artifact model from the AAI xml model string.
271          *
272          * @param resourceModel
273          *            Model of the resource artifact
274          * @param aaiResourceModel
275          *            AAI model as string
276          * @return Generated {@link Artifact} model for the resource
277          */
278         private Artifact getResourceArtifact(Model resourceModel, String aaiResourceModel) {
279                 final String resourceArtifactLabel = getArtifactLabel(resourceModel);
280                 MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel);
281                 final byte[] bytes = aaiResourceModel.getBytes();
282
283                 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
284                                 GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes));
285                 artifact.setName(getArtifactName(resourceModel));
286                 artifact.setLabel(resourceArtifactLabel);
287                 artifact.setDescription(ArtifactGeneratorToscaParser.getArtifactDescription(resourceModel));
288                 return artifact;
289         }
290
291         /**
292          * @param generationData
293          * @param artifactName
294          * @return
295          */
296         private boolean isContained(GenerationData generationData, final String artifactName) {
297                 return generationData.getResultData().stream()
298                                 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
299         }
300
301         /**
302          * Create Service artifact model from the AAI XML model.
303          *
304          * @param serviceModel
305          *            Model of the service artifact
306          * @param aaiServiceModel
307          *            AAI model as string
308          * @return Generated {@link Artifact} model for the service
309          */
310         private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) {
311                 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
312                                 GeneratorUtil.checkSum(aaiServiceModel.getBytes()), GeneratorUtil.encode(aaiServiceModel.getBytes()));
313                 String serviceArtifactName = getArtifactName(serviceModel);
314                 String serviceArtifactLabel = getArtifactLabel(serviceModel);
315                 artifact.setName(serviceArtifactName);
316                 artifact.setLabel(serviceArtifactLabel);
317                 String description = ArtifactGeneratorToscaParser.getArtifactDescription(serviceModel);
318                 artifact.setDescription(description);
319                 return artifact;
320         }
321
322         private int hashCodeUuId(String uuId) {
323                 int hashcode = 0;
324                 for (int i = 0; i < uuId.length(); i++) {
325                         hashcode = 31 * hashcode + uuId.charAt(i);
326                 }
327                 return hashcode;
328         }
329
330         private String truncateName(String name) {
331                 String truncatedName = name;
332                 if (name.length() >= 200) {
333                         truncatedName = name.substring(0, 199);
334                 }
335                 return truncatedName;
336         }
337
338         private String validateServiceVersion(Map<String, String> additionalParams) {
339                 String serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName());
340                 if (serviceVersion == null) {
341                         throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
342                 } else {
343                         String versionRegex = "^[1-9]\\d*(\\.0)$";
344                         if (!(serviceVersion.matches(versionRegex))) {
345                                 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
346                         }
347                 }
348                 return serviceVersion;
349         }
350 }