Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / CsarToXmlConverter.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.csar;
22
23 import java.util.List;
24 import java.util.Objects;
25 import org.apache.commons.lang.time.StopWatch;
26 import org.onap.aai.babel.csar.extractor.InvalidArchiveException;
27 import org.onap.aai.babel.csar.extractor.YamlExtractor;
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.xml.generator.ModelGenerator;
32 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
33 import org.onap.aai.babel.xml.generator.data.Artifact;
34
35 /**
36  * This class is responsible for converting content in a csar archive into one or more xml artifacts.
37  */
38 public class CsarToXmlConverter {
39     private static final LogHelper logger = LogHelper.INSTANCE;
40
41     /**
42      * This method is responsible for extracting one or more yaml files from the given csarArtifact and then using them
43      * to generate xml artifacts.
44      *
45      * @param csarArchive the artifact that contains the csar archive to generate xml artifacts from
46      * @param name the name of the archive file
47      * @param version the version of the archive file
48      * @return List<org.openecomp.sdc.generator.data.Artifact> a list of generated xml artifacts
49      * @throws CsarConverterException if there is an error either extracting the yaml files or generating xml artifacts
50      */
51     public List<BabelArtifact> generateXmlFromCsar(byte[] csarArchive, String name, String version)
52             throws CsarConverterException {
53
54         StopWatch stopwatch = new StopWatch();
55         stopwatch.start();
56
57         validateArguments(csarArchive, name, version);
58
59         logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
60                 "Starting to process csarArchive to convert contents to xml artifacts");
61         List<BabelArtifact> xmlArtifacts;
62
63         try {
64             logger.debug("Calling YamlExtractor to extract ymlFiles");
65             List<Artifact> ymlFiles = YamlExtractor.extract(csarArchive, name, version);
66
67             logger.debug("Calling XmlArtifactGenerator to generateXmlArtifacts");
68             xmlArtifacts = new ModelGenerator().generateArtifacts(csarArchive, ymlFiles);
69
70             logger.debug(xmlArtifacts.size() + " xml artifacts have been generated");
71         } catch (InvalidArchiveException e) {
72             throw new CsarConverterException(
73                     "An error occurred trying to extract the yml files from the csar file : " + e);
74         } catch (XmlArtifactGenerationException e) {
75             throw new CsarConverterException(
76                     "An error occurred trying to generate xml files from a collection of yml files : " + e);
77         } finally {
78             logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
79         }
80
81         return xmlArtifacts;
82     }
83
84     private void validateArguments(byte[] csarArchive, String name, String version) {
85         Objects.requireNonNull(csarArchive);
86         Objects.requireNonNull(name);
87         Objects.requireNonNull(version);
88     }
89 }