2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 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;
24 import java.util.Base64;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.onap.aai.babel.logging.ApplicationMsgs;
30 import org.onap.aai.babel.logging.LogHelper;
31 import org.onap.aai.babel.service.data.BabelArtifact;
32 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
33 import org.onap.aai.babel.xml.generator.api.AaiArtifactGenerator;
34 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
35 import org.onap.aai.babel.xml.generator.data.Artifact;
36 import org.onap.aai.babel.xml.generator.data.GenerationData;
37 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
38 import org.onap.aai.babel.xml.generator.data.GroupType;
39 import org.onap.aai.cl.api.Logger;
42 * This class is responsible for generating XML model artifacts from a collection of CSAR artifacts.
44 public class ModelGenerator implements ArtifactGenerator {
47 private static final Logger logger = LogHelper.INSTANCE;
49 private static final String VERSION_DELIMITER = ".";
50 private static final String VERSION_DELIMITER_REGEXP = "\\" + VERSION_DELIMITER;
51 private static final String DEFAULT_SERVICE_VERSION = "1.0";
54 * Invokes the TOSCA artifact generator API with the input artifacts.
57 * @param csarArtifacts
59 * @return {@link List} of output artifacts
60 * @throws XmlArtifactGenerationException
61 * if there is an error trying to generate XML artifacts
64 public List<BabelArtifact> generateArtifacts(byte[] csarArchive, List<Artifact> csarArtifacts)
65 throws XmlArtifactGenerationException {
66 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
67 "Generating XML for " + csarArtifacts.size() + " CSAR artifacts.");
69 // Get the service version to pass into the generator
70 String toscaVersion = csarArtifacts.get(0).getVersion();
71 String serviceVersion = getServiceVersion(toscaVersion);
72 logger.debug("The service version is " + serviceVersion);
73 Map<String, String> additionalParams = new HashMap<>();
74 additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), serviceVersion);
76 // Call ArtifactGenerator API
77 logger.debug("Obtaining instance of ArtifactGenerationService");
78 org.onap.aai.babel.xml.generator.api.ArtifactGenerator generator = new AaiArtifactGenerator();
79 logger.debug("About to call generationService.generateArtifact()");
80 GenerationData data = generator.generateArtifact(csarArchive, csarArtifacts, additionalParams);
81 logger.debug("Call generationService.generateArtifact() has finished");
83 // Convert results into BabelArtifacts
84 if (data.getErrorData().isEmpty()) {
85 return data.getResultData().stream().map(a -> new BabelArtifact(a.getName(), ArtifactType.MODEL,
86 new String(Base64.getDecoder().decode(a.getPayload())))).collect(Collectors.toList());
88 throw new XmlArtifactGenerationException(
89 "Error occurred during artifact generation: " + data.getErrorData().toString());
94 * Creates an instance of an input artifact for the generator.
97 * the payload downloaded from SDC
99 * name of the artifact to create
100 * @param artifactVersion
101 * version of the artifact to create
102 * @return an {@link Artifact} object constructed from the payload and artifactInfo
104 public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
105 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
107 // Convert payload into an input Artifact
108 String checksum = GeneratorUtil.checkSum(payload);
109 byte[] encodedPayload = GeneratorUtil.encode(payload);
110 Artifact artifact = new Artifact("TOSCA", GroupType.DEPLOYMENT.name(), checksum, encodedPayload);
111 artifact.setName(artifactName);
112 artifact.setLabel(artifactName);
113 artifact.setDescription(artifactName);
114 artifact.setVersion(artifactVersion);
118 private static String getServiceVersion(String artifactVersion) {
119 logger.debug("Artifact version=" + artifactVersion);
120 String serviceVersion;
122 int majorVersion = Integer.parseInt(artifactVersion.split(VERSION_DELIMITER_REGEXP)[0]);
123 serviceVersion = majorVersion + VERSION_DELIMITER + "0";
124 } catch (Exception e) {
125 logger.warn(ApplicationMsgs.DISTRIBUTION_EVENT,
126 "Error generating service version from artifact version: " + artifactVersion
127 + ". Using default service version of: " + DEFAULT_SERVICE_VERSION + ". Error details: "
129 return DEFAULT_SERVICE_VERSION;
132 return serviceVersion;