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