7eb562acdcbd7917c1574c85d34f110382465659
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / BabelArtifactConverter.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.modelloader.notification;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
26 import org.onap.aai.babel.service.data.BabelArtifact;
27 import org.onap.aai.modelloader.entity.Artifact;
28 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
29 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
30 import org.onap.aai.modelloader.entity.model.ModelArtifact;
31 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
32
33 /**
34  * This class is responsible for converting TOSCA artifacts into instances of {@link ModelArtifact} ready for pushing
35  * the converted artifacts .
36  */
37 class BabelArtifactConverter {
38
39     /**
40      * This method converts BabelArtifacts into instances of {@link ModelArtifact}.
41      *
42      * @param xmlArtifacts xml artifacts to be parsed
43      * @return List<org.openecomp.modelloader.entity.Artifact> list of converted model artifacts
44      * @throws BabelArtifactParsingException if an error occurs trying to parse the generated XML files that were
45      *         converted from tosca artifacts
46      */
47     List<Artifact> convertToModel(List<BabelArtifact> xmlArtifacts) throws BabelArtifactParsingException {
48         Objects.requireNonNull(xmlArtifacts);
49         List<Artifact> modelArtifacts = new ArrayList<>();
50         ModelArtifactParser modelArtParser = new ModelArtifactParser();
51
52         // Parse TOSCA payloads
53         for (BabelArtifact xmlArtifact : xmlArtifacts) {
54
55             List<Artifact> parsedArtifacts = modelArtParser.parse(xmlArtifact.getPayload(), xmlArtifact.getName());
56
57             if (parsedArtifacts == null || parsedArtifacts.isEmpty()) {
58                 throw new BabelArtifactParsingException("Could not parse generated XML: " + xmlArtifact.getPayload());
59             }
60
61             modelArtifacts.addAll(parsedArtifacts);
62         }
63
64         return modelArtifacts;
65     }
66
67     /**
68      * This method converts BabelArtifacts into instances of {@link VnfCatalogArtifact}.
69      *
70      * @param xmlArtifacts xml artifacts to be parsed
71      * @return List<org.openecomp.modelloader.entity.Artifact> list of converted catalog artifacts
72      */
73     List<Artifact> convertToCatalog(List<BabelArtifact> xmlArtifacts) {
74         Objects.requireNonNull(xmlArtifacts);
75         List<Artifact> catalogArtifacts = new ArrayList<>();
76
77         for (BabelArtifact xmlArtifact : xmlArtifacts) {
78             catalogArtifacts.add(new VnfCatalogArtifact(xmlArtifact.getPayload()));
79         }
80
81         return catalogArtifacts;
82     }
83 }