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 javax.ws.rs.core.Response;
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 LogHelper logger = LogHelper.INSTANCE;
48 private static final String DEFAULT_SERVICE_VERSION = "1.0";
51 * Invokes the TOSCA artifact generator API with the input artifacts.
54 * @param csarArtifacts
56 * @return {@link List} of output artifacts
57 * @throws XmlArtifactGenerationException
58 * if there is an error trying to generate XML artifacts
61 public List<BabelArtifact> generateArtifacts(byte[] csarArchive, List<Artifact> csarArtifacts)
62 throws XmlArtifactGenerationException {
63 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
64 "Generating XML for " + csarArtifacts.size() + " CSAR artifacts.");
66 // Get the service version to pass into the generator
67 String toscaVersion = csarArtifacts.get(0).getVersion();
68 String serviceVersion = getServiceVersion(toscaVersion);
69 logger.debug("The service version is " + serviceVersion);
70 Map<String, String> additionalParams = new HashMap<>();
71 additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), serviceVersion);
73 // Call ArtifactGenerator API
74 logger.debug("Obtaining instance of ArtifactGenerationService");
75 org.onap.aai.babel.xml.generator.api.ArtifactGenerator generator = new AaiArtifactGenerator();
76 logger.debug("About to call generationService.generateArtifact()");
77 GenerationData data = generator.generateArtifact(csarArchive, csarArtifacts, additionalParams);
78 logger.debug("Call generationService.generateArtifact() has finished");
80 // Convert results into BabelArtifacts
81 if (data.getErrorData().isEmpty()) {
82 return data.getResultData().stream().map(a -> new BabelArtifact(a.getName(), ArtifactType.MODEL,
83 new String(Base64.getDecoder().decode(a.getPayload())))).collect(Collectors.toList());
85 throw new XmlArtifactGenerationException(
86 "Error occurred during artifact generation: " + data.getErrorData().toString());
91 * Creates an instance of an input artifact for the generator.
94 * the payload downloaded from SDC
96 * name of the artifact to create
97 * @param artifactVersion
98 * version of the artifact to create
99 * @return an {@link Artifact} object constructed from the payload and artifactInfo
101 public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
102 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
104 // Convert payload into an input Artifact
105 String checksum = GeneratorUtil.checkSum(payload);
106 byte[] encodedPayload = GeneratorUtil.encode(payload);
107 Artifact artifact = new Artifact("TOSCA", GroupType.DEPLOYMENT.name(), checksum, encodedPayload);
108 artifact.setName(artifactName);
109 artifact.setLabel(artifactName);
110 artifact.setDescription(artifactName);
111 artifact.setVersion(artifactVersion);
115 private static String getServiceVersion(String artifactVersion) {
116 logger.debug("Artifact version=" + artifactVersion);
118 return String.valueOf(Float.parseFloat(artifactVersion));
119 } catch (Exception e) {
120 logger.setContextValue(LogHelper.MdcParameter.RESPONSE_CODE, String.valueOf(Response.Status.BAD_REQUEST.getStatusCode()));
121 logger.setContextValue(LogHelper.MdcParameter.RESPONSE_DESCRIPTION, "Invalid Artifact version");
122 logger.warn(ApplicationMsgs.DISTRIBUTION_EVENT,
123 "Error generating service version from artifact version: " + artifactVersion
124 + ". Using default service version of: " + DEFAULT_SERVICE_VERSION + ". Error details: "
126 return DEFAULT_SERVICE_VERSION;