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;
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 content in a csar archive into one or more xml artifacts.
39 public class CsarToXmlConverter {
40 private static final LogHelper logger = LogHelper.INSTANCE;
43 * This method is responsible for extracting one or more yaml files from the given csarArtifact and then using them
44 * to generate xml artifacts.
46 * @param csarArchive the artifact that contains the csar archive to generate xml artifacts from
47 * @param name the name of the archive file
48 * @param version the version of the archive file
49 * @return List<org.onap.sdc.generator.data.Artifact> a list of generated xml artifacts
50 * @throws CsarConverterException if there is an error either extracting the yaml files or generating xml artifacts
52 public List<BabelArtifact> generateXmlFromCsar(byte[] csarArchive, String name, String version)
53 throws CsarConverterException {
55 StopWatch stopwatch = new StopWatch();
58 validateArguments(csarArchive, name, version);
60 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
61 "Starting to process csarArchive to convert contents to xml artifacts");
62 List<BabelArtifact> xmlArtifacts;
65 logger.debug("Calling YamlExtractor to extract ymlFiles");
66 List<Artifact> ymlFiles = YamlExtractor.extract(csarArchive, name, version);
68 logger.debug("Calling XmlArtifactGenerator to generateXmlArtifacts");
69 xmlArtifacts = new ModelGenerator().generateArtifacts(csarArchive, ymlFiles);
71 logger.debug(xmlArtifacts.size() + " xml artifacts have been generated");
72 } catch (InvalidArchiveException e) {
73 throw new CsarConverterException(
74 "An error occurred trying to extract the yml files from the csar file : " + e);
75 } catch (XmlArtifactGenerationException e) {
76 throw new CsarConverterException(
77 "An error occurred trying to generate xml files from a collection of yml files : " + e);
79 logger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
85 private void validateArguments(byte[] csarArchive, String name, String version) {
86 Objects.requireNonNull(csarArchive);
87 Objects.requireNonNull(name);
88 Objects.requireNonNull(version);