2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.csar;
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;
37 * This class is responsible for converting CSAR content into one or more XML artifacts.
39 public class CsarToXmlConverter {
40 private static final LogHelper logger = LogHelper.INSTANCE;
42 private final YamlExtractor yamlExtractor;
44 public CsarToXmlConverter() {
45 yamlExtractor = new YamlExtractor();
49 * This method is responsible for generating Artifacts from YAML files within CSAR content.
52 * the artifact that contains the csar archive to generate XML artifacts from
54 * the name of the archive file
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
61 public List<BabelArtifact> generateXmlFromCsar(byte[] csarArchive, String name, String version)
62 throws CsarConverterException {
63 validateArguments(csarArchive, name, version);
65 StopWatch stopwatch = new StopWatch();
68 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
69 "Starting to process csarArchive to convert contents to XML artifacts");
70 List<BabelArtifact> xmlArtifacts;
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);
83 logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
89 private void validateArguments(byte[] csarArchive, String name, String version) {
90 Objects.requireNonNull(csarArchive);
91 Objects.requireNonNull(name);
92 Objects.requireNonNull(version);