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.List;
30 import java.util.Optional;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.aai.babel.logging.ApplicationMsgs;
34 import org.onap.aai.babel.logging.LogHelper;
35 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
36 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
37 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
38 import org.onap.aai.babel.xml.generator.data.Artifact;
39 import org.onap.aai.babel.xml.generator.data.ArtifactType;
40 import org.onap.aai.babel.xml.generator.data.GenerationData;
41 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
42 import org.onap.aai.babel.xml.generator.data.GroupType;
43 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
44 import org.onap.aai.babel.xml.generator.model.Model;
45 import org.onap.aai.babel.xml.generator.model.Resource;
46 import org.onap.aai.babel.xml.generator.model.Service;
47 import org.onap.aai.babel.xml.generator.model.Widget;
48 import org.onap.aai.babel.xml.generator.model.WidgetType;
49 import org.onap.aai.babel.xml.generator.types.ModelType;
50 import org.onap.aai.cl.api.Logger;
51 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
52 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
53 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
54 import org.onap.sdc.toscaparser.api.Group;
55 import org.onap.sdc.toscaparser.api.NodeTemplate;
56 import org.onap.sdc.toscaparser.api.elements.Metadata;
59 public class AaiArtifactGenerator implements ArtifactGenerator {
61 private static Logger log = LogHelper.INSTANCE;
63 private static final String MDC_PARAM_MODEL_INFO = "ARTIFACT_MODEL_INFO";
64 private static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml";
65 private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA =
66 "Service tosca missing from list of input artifacts";
67 private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION =
68 "Cannot generate artifacts. Service version is not specified";
69 private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION =
70 "Cannot generate artifacts. Service version is incorrect";
72 private AaiModelGenerator modelGenerator = new AaiModelGenerator();
75 public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input,
76 Map<String, String> additionalParams) {
77 String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
78 if (configLocation == null) {
79 throw new IllegalArgumentException(
80 String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND,
81 ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
85 ArtifactGeneratorToscaParser.initWidgetConfiguration();
86 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
87 } catch (IOException e) {
88 log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation);
89 return createErrorData(e);
95 csarPath = createTempFile(csarArchive);
96 } catch (IOException e) {
97 log.error(ApplicationMsgs.TEMP_FILE_ERROR, e);
98 return createErrorData(e);
102 ISdcCsarHelper csarHelper =
103 SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString());
104 return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper);
105 } catch (SdcToscaParserException | XmlArtifactGenerationException e) {
106 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
107 return createErrorData(e);
109 FileUtils.deleteQuietly(csarPath.toFile());
113 private GenerationData createErrorData(Exception e) {
114 GenerationData generationData = new GenerationData();
115 generationData.add(ArtifactType.AAI.name(), e.getMessage());
116 return generationData;
120 * Generate model artifacts for the Service and its associated Resources.
122 * @param serviceVersion
124 * interface to the TOSCA parser
125 * @return the generated Artifacts (containing XML models)
126 * @throws XmlArtifactGenerationException
127 * if the configured widget mappings do not support processed widget type(s)
129 public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper)
130 throws XmlArtifactGenerationException {
131 List<NodeTemplate> serviceNodeTemplates = csarHelper.getServiceNodeTemplates();
132 if (serviceNodeTemplates == null) {
133 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
136 Service serviceModel = createServiceModel(serviceVersion, csarHelper.getServiceMetadataAllProperties());
138 MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel));
140 List<Resource> resources = generateResourceModels(csarHelper, serviceNodeTemplates, serviceModel);
142 // Generate the A&AI XML model for the Service.
143 final String serviceArtifact = modelGenerator.generateModelFor(serviceModel);
145 // Build a Babel Artifact to be returned to the caller.
146 GenerationData generationData = new GenerationData();
147 generationData.add(getServiceArtifact(serviceModel, serviceArtifact));
149 // For each Resource, generate the A&AI XML model and then create an additional Artifact for that model.
150 for (Resource resource : resources) {
151 generateResourceArtifact(generationData, resource);
152 for (Resource childResource : resource.getResources()) {
153 boolean isProvidingService =
154 (boolean) Optional.ofNullable(childResource.getProperties().get("providingService")) //
156 if (!isProvidingService) {
157 generateResourceArtifact(generationData, childResource);
162 return generationData;
166 * Create a Service from the provided metadata
168 * @param serviceVersion
172 private Service createServiceModel(final String serviceVersion, Map<String, String> properties) {
173 log.debug("Processing (TOSCA) Service object");
174 Service serviceModel = new Service();
175 serviceModel.setModelVersion(serviceVersion);
176 serviceModel.populateModelIdentificationInformation(properties);
182 * @param serviceNodeTemplates
183 * @param serviceModel
184 * @return the generated Models
185 * @throws XmlArtifactGenerationException
186 * if the configured widget mappings do not support processed widget type(s)
188 private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
189 Service serviceModel) throws XmlArtifactGenerationException {
190 final List<Group> serviceGroups = csarHelper.getGroupsOfTopologyTemplate();
191 final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
193 List<Resource> resources = new ArrayList<>();
195 for (NodeTemplate nodeTemplate : serviceNodeTemplates) {
196 if (nodeTemplate.getMetaData() != null) {
197 generateModelFromNodeTemplate(csarHelper, serviceModel, resources, serviceGroups, parser, nodeTemplate);
199 log.warn(ApplicationMsgs.MISSING_SERVICE_METADATA, nodeTemplate.getName());
208 * @param serviceModel
210 * @param serviceGroups
212 * @param nodeTemplate
213 * @throws XmlArtifactGenerationException
214 * if the configured widget mappings do not support processed widget type(s)
216 private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
217 List<Resource> resources, final List<Group> serviceGroups, ArtifactGeneratorToscaParser parser,
218 NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
219 Resource model = getModelFor(parser, nodeTemplate);
222 if (nodeTemplate.getMetaData() != null) {
223 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
226 parser.addRelatedModel(serviceModel, model);
227 if (model.getModelType() == ModelType.RESOURCE) {
228 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
231 for (Group group : serviceGroups) {
232 ArrayList<String> members = group.getMembers();
233 if (members != null && members.contains(nodeTemplate.getName())
234 && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
235 log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(),
236 group.getType(), members));
238 Resource groupModel = parser.createInstanceGroupModel(
239 parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
240 serviceModel.addResource(groupModel);
241 resources.add(groupModel);
247 private Resource getModelFor(ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
248 String nodeTypeName = nodeTemplate.getType();
250 log.debug("Processing resource " + nodeTypeName + ": " + nodeTemplate.getMetaData().getValue("UUID"));
252 Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
255 Metadata metadata = nodeTemplate.getMetaData();
256 if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties())
257 && model.hasWidgetType("VF")) {
258 model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
269 * @param nodeTemplate
270 * @throws XmlArtifactGenerationException
271 * if the configured widget mappings do not support processed widget type(s)
273 private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
274 ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
275 Resource resourceModel = getModelFor(parser, nodeTemplate);
276 if (resourceModel == null) {
277 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model");
281 Map<String, String> serviceMetadata = nodeTemplate.getMetaData().getAllProperties();
282 resourceModel.populateModelIdentificationInformation(serviceMetadata);
284 parser.processResourceModels(resourceModel, csarHelper.getNodeTemplateChildren(nodeTemplate));
286 if (csarHelper.getServiceVfList() != null) {
287 parser.processVfModules(resources, resourceModel, nodeTemplate);
290 if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
291 resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT"));
294 resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate));
295 resources.add(resourceModel);
299 * @param generationData
301 * @throws XmlArtifactGenerationException
303 private void generateResourceArtifact(GenerationData generationData, Resource resource)
304 throws XmlArtifactGenerationException {
305 if (!isContained(generationData, getArtifactName(resource))) {
306 log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
307 generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
311 private Path createTempFile(byte[] bytes) throws IOException {
312 log.debug("Creating temp file on file system for the csar");
313 Path path = Files.createTempFile("temp", ".csar");
314 Files.write(path, bytes);
319 * Create the artifact label for an AAI model.
322 * @return the artifact label as String
324 private String getArtifactLabel(Model model) {
325 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
326 artifactName.append("-");
327 artifactName.append(model.getModelTypeName());
328 artifactName.append("-");
329 artifactName.append(hashCodeUuId(model.getModelNameVersionId()));
330 return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-");
334 * Method to generate the artifact name for an AAI model.
338 * @return Model artifact name
340 private String getArtifactName(Model model) {
341 StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
342 artifactName.append("-");
344 String truncatedArtifactName = truncateName(model.getModelName());
345 artifactName.append(truncatedArtifactName);
347 artifactName.append("-");
348 artifactName.append(model.getModelTypeName());
349 artifactName.append("-");
350 artifactName.append(model.getModelVersion());
352 artifactName.append(".");
353 artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION);
354 return artifactName.toString();
358 * Create Resource artifact model from the AAI xml model string.
360 * @param resourceModel
361 * Model of the resource artifact
362 * @param aaiResourceModel
363 * AAI model as string
364 * @return Generated {@link Artifact} model for the resource
366 private Artifact getResourceArtifact(Resource resourceModel, String aaiResourceModel) {
367 final String resourceArtifactLabel = getArtifactLabel(resourceModel);
368 MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel);
369 final byte[] bytes = aaiResourceModel.getBytes();
371 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
372 GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes));
373 artifact.setName(getArtifactName(resourceModel));
374 artifact.setLabel(resourceArtifactLabel);
375 artifact.setDescription("AAI Resource Model");
380 * @param generationData
381 * @param artifactName
384 private boolean isContained(GenerationData generationData, final String artifactName) {
385 return generationData.getResultData().stream()
386 .anyMatch(artifact -> StringUtils.equals(artifact.getName(), artifactName));
390 * Create Service artifact model from the AAI XML model.
392 * @param serviceModel
393 * Model of the service artifact
394 * @param aaiServiceModel
395 * AAI model as string
396 * @return Generated {@link Artifact} model for the service
398 private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) {
399 Artifact artifact = new Artifact(ArtifactType.MODEL_INVENTORY_PROFILE.name(), GroupType.DEPLOYMENT.name(),
400 GeneratorUtil.checkSum(aaiServiceModel.getBytes()), GeneratorUtil.encode(aaiServiceModel.getBytes()));
401 String serviceArtifactName = getArtifactName(serviceModel);
402 String serviceArtifactLabel = getArtifactLabel(serviceModel);
403 artifact.setName(serviceArtifactName);
404 artifact.setLabel(serviceArtifactLabel);
405 artifact.setDescription("AAI Service Model");
409 private int hashCodeUuId(String uuId) {
411 for (int i = 0; i < uuId.length(); i++) {
412 hashcode = 31 * hashcode + uuId.charAt(i);
417 private String truncateName(String name) {
418 String truncatedName = name;
419 if (name.length() >= 200) {
420 truncatedName = name.substring(0, 199);
422 return truncatedName;
425 private String validateServiceVersion(Map<String, String> additionalParams) {
426 String serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName());
427 if (serviceVersion == null) {
428 throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
430 String versionRegex = "^[1-9]\\d*(\\.0)$";
431 if (!(serviceVersion.matches(versionRegex))) {
432 throw new IllegalArgumentException(String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION));
435 return serviceVersion;