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