2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.babel.csar;
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;
36 * This class is responsible for converting CSAR content into one or more XML artifacts.
38 public class CsarToXmlConverter {
39 private static final LogHelper logger = LogHelper.INSTANCE;
41 private final YamlExtractor yamlExtractor;
43 public CsarToXmlConverter() {
44 yamlExtractor = new YamlExtractor();
48 * This method is responsible for generating Artifacts from YAML files within CSAR content.
51 * the artifact that contains the csar archive to generate XML artifacts from
53 * the name of the archive file
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
60 public List<BabelArtifact> generateXmlFromCsar(byte[] csarArchive, String name, String version)
61 throws CsarConverterException {
62 validateArguments(csarArchive, name, version);
64 StopWatch stopwatch = new StopWatch();
67 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
68 "Starting to process csarArchive to convert contents to XML artifacts");
69 List<BabelArtifact> xmlArtifacts;
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);
82 logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
88 private void validateArguments(byte[] csarArchive, String name, String version) {
89 Objects.requireNonNull(csarArchive);
90 Objects.requireNonNull(name);
91 Objects.requireNonNull(version);