Improved logging for VNF image extraction
[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 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         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);
71
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");
78
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());
83         } else {
84             throw new XmlArtifactGenerationException(
85                     "Error occurred during artifact generation: " + data.getErrorData().toString());
86         }
87     }
88
89     /**
90      * Creates an instance of an input artifact for the generator.
91      *
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
96      */
97     public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
98         logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
99
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);
108         return artifact;
109     }
110
111     private static String getServiceVersion(String artifactVersion) {
112         logger.debug("Artifact version=" + artifactVersion);
113         String serviceVersion;
114         try {
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: "
121                             + e);
122             return DEFAULT_SERVICE_VERSION;
123         }
124
125         return serviceVersion;
126     }
127 }