Fix checkstyle issues including javadoc
[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-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
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
22 package org.onap.aai.babel.xml.generator;
23
24 import java.util.Base64;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
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;
40
41 /**
42  * This class is responsible for generating XML model artifacts from a collection of CSAR artifacts.
43  */
44 public class ModelGenerator implements ArtifactGenerator {
45
46
47     private static final Logger logger = LogHelper.INSTANCE;
48
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";
52
53     /**
54      * Invokes the TOSCA artifact generator API with the input artifacts.
55      *
56      * @param csarArchive
57      * @param csarArtifacts
58      *     the input artifacts
59      * @return {@link List} of output artifacts
60      * @throws XmlArtifactGenerationException
61      *     if there is an error trying to generate XML artifacts
62      */
63     @Override
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.");
68
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);
75
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");
82
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());
87         } else {
88             throw new XmlArtifactGenerationException(
89                     "Error occurred during artifact generation: " + data.getErrorData().toString());
90         }
91     }
92
93     /**
94      * Creates an instance of an input artifact for the generator.
95      *
96      * @param payload
97      *     the payload downloaded from SDC
98      * @param artifactName
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
103      */
104     public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
105         logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
106
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);
115         return artifact;
116     }
117
118     private static String getServiceVersion(String artifactVersion) {
119         logger.debug("Artifact version=" + artifactVersion);
120         String serviceVersion;
121         try {
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: "
128                             + e);
129             return DEFAULT_SERVICE_VERSION;
130         }
131
132         return serviceVersion;
133     }
134 }