This enhancement will enable Babel to process artifacts of version n.n
[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 (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
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     private static final Logger logger = LogHelper.INSTANCE;
47
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";
51
52     /**
53      * Invokes the TOSCA artifact generator API with the input artifacts.
54      *
55      * @param csarArchive
56      * @param csarArtifacts
57      *            the input artifacts
58      * @return {@link List} of output artifacts
59      * @throws XmlArtifactGenerationException
60      *             if there is an error trying to generate XML artifacts
61      */
62     @Override
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.");
67
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);
74
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");
81
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());
86         } else {
87             throw new XmlArtifactGenerationException(
88                     "Error occurred during artifact generation: " + data.getErrorData().toString());
89         }
90     }
91
92     /**
93      * Creates an instance of an input artifact for the generator.
94      *
95      * @param payload
96      *            the payload downloaded from SDC
97      * @param artifactName
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
102      */
103     public static Artifact createArtifact(byte[] payload, String artifactName, String artifactVersion) {
104         logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Creating artifact for: " + artifactName);
105
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);
114         return artifact;
115     }
116
117     private static String getServiceVersion(String artifactVersion) {
118         logger.debug("Artifact version=" + artifactVersion );
119         
120         // As of 1902, AAI-16260, we no longer edit the passed in artifact/service version.
121         try {
122                 // just make sure it's an integer 
123             Integer.parseInt(artifactVersion.split(VERSION_DELIMITER_REGEXP)[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         logger.debug("Use Artifact version as the serviceVersion=" + artifactVersion );
133         return artifactVersion;
134     }
135 }