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;
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 {
46 private static final Logger logger = LogHelper.INSTANCE;
48 private static final String VERSION_DELIMITER = ".";
49 private static final String VERSION_DELIMITER_REGEXP = "\\" + VERSION_DELIMITER;
50 private static final String DEFAULT_SERVICE_VERSION = "1.0";
53 * Invokes the TOSCA artifact generator API with the input artifacts.
56 * @param csarArtifacts
58 * @return {@link List} of output artifacts
59 * @throws XmlArtifactGenerationException
60 * if there is an error trying to generate XML artifacts
63 public List<BabelArtifact> generateArtifacts(byte[] csarArchive, List<Artifact> csarArtifacts)
64 throws XmlArtifactGenerationException {
65 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
66 "Generating XML for " + csarArtifacts.size() + " CSAR artifacts.");
68 // Get the service version to pass into the generator
69 String toscaVersion = csarArtifacts.get(0).getVersion();
70 String serviceVersion = getServiceVersion(toscaVersion);
71 logger.debug("The service version is " + serviceVersion);
72 Map<String, String> additionalParams = new HashMap<>();
73 additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), serviceVersion);
75 // Call ArtifactGenerator API
76 logger.debug("Obtaining instance of ArtifactGenerationService");
77 org.onap.aai.babel.xml.generator.api.ArtifactGenerator generator = new AaiArtifactGenerator();
78 logger.debug("About to call generationService.generateArtifact()");
79 GenerationData data = generator.generateArtifact(csarArchive, csarArtifacts, additionalParams);
80 logger.debug("Call generationService.generateArtifact() has finished");
82 // Convert results into BabelArtifacts
83 if (data.getErrorData().isEmpty()) {
84 return data.getResultData().stream().map(a -> new BabelArtifact(a.getName(), ArtifactType.MODEL,
85 new String(Base64.getDecoder().decode(a.getPayload())))).collect(Collectors.toList());
87 throw new XmlArtifactGenerationException(
88 "Error occurred during artifact generation: " + data.getErrorData().toString());
93 * Creates an instance of an input artifact for the generator.
96 * the payload downloaded from SDC
98 * name of the artifact to create
99 * @param artifactVersion
100 * version of the artifact to create
101 * @return an {@link Artifact} object constructed from the payload and artifactInfo
103 public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
104 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
106 // Convert payload into an input Artifact
107 String checksum = GeneratorUtil.checkSum(payload);
108 byte[] encodedPayload = GeneratorUtil.encode(payload);
109 Artifact artifact = new Artifact("TOSCA", GroupType.DEPLOYMENT.name(), checksum, encodedPayload);
110 artifact.setName(artifactName);
111 artifact.setLabel(artifactName);
112 artifact.setDescription(artifactName);
113 artifact.setVersion(artifactVersion);
117 private static String getServiceVersion(String artifactVersion) {
118 logger.debug("Artifact version=" + artifactVersion);
119 String serviceVersion;
121 int majorVersion = Integer.parseInt(artifactVersion.split(VERSION_DELIMITER_REGEXP)[0]);
122 serviceVersion = majorVersion + VERSION_DELIMITER + "0";
123 } catch (Exception e) {
124 logger.warn(ApplicationMsgs.DISTRIBUTION_EVENT,
125 "Error generating service version from artifact version: " + artifactVersion
126 + ". Using default service version of: " + DEFAULT_SERVICE_VERSION + ". Error details: "
128 return DEFAULT_SERVICE_VERSION;
131 return serviceVersion;