aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/babel/csar/CsarToXmlConverter.java
blob: 9e1ff6e65200054bc2cb8aaa83d798e6963064f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.csar;

import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.time.StopWatch;
import org.onap.aai.babel.csar.extractor.InvalidArchiveException;
import org.onap.aai.babel.csar.extractor.YamlExtractor;
import org.onap.aai.babel.logging.ApplicationMsgs;
import org.onap.aai.babel.logging.LogHelper;
import org.onap.aai.babel.service.data.BabelArtifact;
import org.onap.aai.babel.xml.generator.ModelGenerator;
import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
import org.onap.aai.babel.xml.generator.data.Artifact;

/**
 * This class is responsible for converting CSAR content into one or more XML artifacts.
 */
public class CsarToXmlConverter {
    private static final LogHelper logger = LogHelper.INSTANCE;

    private final YamlExtractor yamlExtractor;

    public CsarToXmlConverter() {
        yamlExtractor = new YamlExtractor();
    }

    /**
     * This method is responsible for generating Artifacts from YAML files within CSAR content.
     *
     * @param csarArchive
     *            the artifact that contains the csar archive to generate XML artifacts from
     * @param name
     *            the name of the archive file
     * @param version
     *            the version of the archive file
     * @return a list of generated XML artifacts
     * @throws CsarConverterException
     *             if there is an error either extracting the YAML files or generating XML artifacts
     */
    public List<BabelArtifact> generateXmlFromCsar(byte[] csarArchive, String name, String version)
            throws CsarConverterException {
        validateArguments(csarArchive, name, version);

        StopWatch stopwatch = new StopWatch();
        stopwatch.start();

        logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
                "Starting to process csarArchive to convert contents to XML artifacts");
        List<BabelArtifact> xmlArtifacts;

        try {
            List<Artifact> ymlFiles = yamlExtractor.extract(csarArchive, name, version);
            xmlArtifacts = new ModelGenerator().generateArtifacts(csarArchive, ymlFiles);
            logger.debug(xmlArtifacts.size() + " XML artifact(s) have been generated");
        } catch (InvalidArchiveException e) {
            throw new CsarConverterException(
                    "An error occurred trying to extract the YMAL files from the csar file : " + e);
        } catch (XmlArtifactGenerationException e) {
            throw new CsarConverterException(
                    "An error occurred trying to generate XML files from a collection of YAML files : " + e);
        } finally {
            logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
        }

        return xmlArtifacts;
    }

    private void validateArguments(byte[] csarArchive, String name, String version) {
        Objects.requireNonNull(csarArchive);
        Objects.requireNonNull(name);
        Objects.requireNonNull(version);
    }
}