2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.babel.xml.generator;
23 import java.util.Base64;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import org.onap.aai.babel.logging.ApplicationMsgs;
29 import org.onap.aai.babel.logging.LogHelper;
30 import org.onap.aai.babel.service.data.BabelArtifact;
31 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
32 import org.onap.aai.babel.xml.generator.api.AaiArtifactGenerator;
33 import org.onap.aai.babel.xml.generator.data.AdditionalParams;
34 import org.onap.aai.babel.xml.generator.data.Artifact;
35 import org.onap.aai.babel.xml.generator.data.GenerationData;
36 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
37 import org.onap.aai.babel.xml.generator.data.GroupType;
38 import org.onap.aai.cl.api.Logger;
41 * This class is responsible for generating XML model artifacts from a collection of CSAR artifacts.
43 public class ModelGenerator implements ArtifactGenerator {
45 private static final Logger logger = LogHelper.INSTANCE;
47 private static final String VERSION_DELIMITER = ".";
48 private static final String VERSION_DELIMITER_REGEXP = "\\" + VERSION_DELIMITER;
49 private static final String DEFAULT_SERVICE_VERSION = "1.0";
52 * Invokes the TOSCA artifact generator API with the input artifacts.
55 * @param csarArtifacts the input artifacts
56 * @return {@link List} of output artifacts
57 * @throws XmlArtifactGenerationException if there is an error trying to generate XML artifacts
60 public List<BabelArtifact> generateArtifacts(byte[] csarArchive, List<Artifact> csarArtifacts)
61 throws XmlArtifactGenerationException {
62 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
63 "Generating XML for " + csarArtifacts.size() + " CSAR artifacts.");
65 // Get the service version to pass into the generator
66 String toscaVersion = csarArtifacts.get(0).getVersion();
67 String serviceVersion = getServiceVersion(toscaVersion);
68 logger.debug("The service version is " + serviceVersion);
69 Map<String, String> additionalParams = new HashMap<>();
70 additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), serviceVersion);
72 // Call ArtifactGenerator API
73 logger.debug("Obtaining instance of ArtifactGenerationService");
74 org.onap.aai.babel.xml.generator.api.ArtifactGenerator generator = new AaiArtifactGenerator();
75 logger.debug("About to call generationService.generateArtifact()");
76 GenerationData data = generator.generateArtifact(csarArchive, csarArtifacts, additionalParams);
77 logger.debug("Call generationService.generateArtifact() has finished");
79 // Convert results into BabelArtifacts
80 if (data.getErrorData().isEmpty()) {
81 return data.getResultData().stream().map(a -> new BabelArtifact(a.getName(), ArtifactType.MODEL,
82 new String(Base64.getDecoder().decode(a.getPayload())))).collect(Collectors.toList());
84 throw new XmlArtifactGenerationException(
85 "Error occurred during artifact generation: " + data.getErrorData().toString());
90 * Creates an instance of an input artifact for the generator.
92 * @param payload the payload downloaded from SDC
93 * @param artifactName name of the artifact to create
94 * @param artifactVersion version of the artifact to create
95 * @return an {@link Artifact} object constructed from the payload and artifactInfo
97 public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
98 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
100 // Convert payload into an input Artifact
101 String checksum = GeneratorUtil.checkSum(payload);
102 byte[] encodedPayload = GeneratorUtil.encode(payload);
103 Artifact artifact = new Artifact("TOSCA", GroupType.DEPLOYMENT.name(), checksum, encodedPayload);
104 artifact.setName(artifactName);
105 artifact.setLabel(artifactName);
106 artifact.setDescription(artifactName);
107 artifact.setVersion(artifactVersion);
111 private static String getServiceVersion(String artifactVersion) {
112 logger.debug("Artifact version=" + artifactVersion);
113 String serviceVersion;
115 int majorVersion = Integer.parseInt(artifactVersion.split(VERSION_DELIMITER_REGEXP)[0]);
116 serviceVersion = majorVersion + VERSION_DELIMITER + "0";
117 } catch (Exception e) {
118 logger.warn(ApplicationMsgs.DISTRIBUTION_EVENT,
119 "Error generating service version from artifact version: " + artifactVersion
120 + ". Using default service version of: " + DEFAULT_SERVICE_VERSION + ". Error details: "
122 return DEFAULT_SERVICE_VERSION;
125 return serviceVersion;