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