65e0ada02fc496ca820c4bfc9dcd209e30540b3c
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / ModelGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.babel.xml.generator;
22
23 import java.util.Base64;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
39
40 /**
41  * This class is responsible for generating xml model artifacts from a collection of csar file artifacts
42  */
43 public class ModelGenerator implements ArtifactGenerator {
44
45     private static final Logger logger = LogHelper.INSTANCE;
46
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";
50
51     /**
52      * Invokes the TOSCA artifact generator API with the input artifacts.
53      *
54      * @param csarArchive
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
58      */
59     @Override
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.");
64
65         // Get the service version to pass into the generator
66         String toscaVersion = csarArtifacts.get(0).getVersion();
67         logger.debug(
68                 "Getting the service version for Tosca Version of the yml file.  The Tosca Version is " + toscaVersion);
69         String serviceVersion = getServiceVersion(toscaVersion);
70         logger.debug("The service version is " + serviceVersion);
71         Map<String, String> additionalParams = new HashMap<>();
72         additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), serviceVersion);
73
74         // Call ArtifactGenerator API
75         logger.debug("Obtaining instance of ArtifactGenerationService");
76         org.onap.aai.babel.xml.generator.api.ArtifactGenerator generator = new AaiArtifactGenerator();
77         logger.debug("About to call generationService.generateArtifact()");
78         GenerationData data = generator.generateArtifact(csarArchive, csarArtifacts, additionalParams);
79         logger.debug("Call generationService.generateArtifact() has finished");
80
81         // Convert results into BabelArtifacts
82         if (data.getErrorData().isEmpty()) {
83             return data.getResultData().stream().map(a -> new BabelArtifact(a.getName(), ArtifactType.MODEL,
84                     new String(Base64.getDecoder().decode(a.getPayload())))).collect(Collectors.toList());
85         } else {
86             throw new XmlArtifactGenerationException(
87                     "Error occurred during artifact generation: " + data.getErrorData().toString());
88         }
89     }
90
91     /**
92      * Creates an instance of an input artifact for the generator.
93      *
94      * @param payload the payload downloaded from SDC
95      * @param artifactName name of the artifact to create
96      * @param artifactVersion version of the artifact to create
97      * @return an {@link Artifact} object constructed from the payload and artifactInfo
98      */
99     public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
100         logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
101
102         // Convert payload into an input Artifact
103         String checksum = GeneratorUtil.checkSum(payload);
104         byte[] encodedPayload = GeneratorUtil.encode(payload);
105         Artifact artifact = new Artifact("TOSCA", GroupType.DEPLOYMENT.name(), checksum, encodedPayload);
106         artifact.setName(artifactName);
107         artifact.setLabel(artifactName);
108         artifact.setDescription(artifactName);
109         artifact.setVersion(artifactVersion);
110         return artifact;
111     }
112
113     private static String getServiceVersion(String artifactVersion) {
114         String serviceVersion;
115
116         try {
117             String[] versionParts = artifactVersion.split(VERSION_DELIMITER_REGEXP);
118             Integer majorVersion = Integer.parseInt(versionParts[0]);
119
120             serviceVersion = majorVersion + VERSION_DELIMITER + "0";
121         } catch (Exception e) {
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: "
125                             + e);
126             return DEFAULT_SERVICE_VERSION;
127         }
128
129         return serviceVersion;
130     }
131 }