2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2024 Deutsche Telekom AG Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.modelloader.babel;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.onap.aai.babel.service.data.BabelArtifact;
26 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
27 import org.onap.aai.babel.service.data.BabelRequest;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.modelloader.entity.Artifact;
31 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
32 import org.onap.aai.modelloader.notification.BabelArtifactConverter;
33 import org.onap.aai.modelloader.notification.ProcessToscaArtifactsException;
34 import org.onap.aai.modelloader.restclient.BabelServiceClient;
35 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
36 import org.springframework.stereotype.Service;
39 public class BabelArtifactService {
41 private static Logger logger = LoggerFactory.getInstance().getLogger(BabelArtifactService.class);
43 private final BabelServiceClient babelServiceClient;
44 private final BabelArtifactConverter babelArtifactConverter;
46 public BabelArtifactService(BabelServiceClient babelServiceClient, BabelArtifactConverter babelArtifactConverter) {
47 this.babelServiceClient = babelServiceClient;
48 this.babelArtifactConverter = babelArtifactConverter;
51 public List<Artifact> invokeBabelService(BabelRequest babelRequest, String distributionId)
52 throws ProcessToscaArtifactsException {
54 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
55 "Posting artifact: " + babelRequest.getArtifactName() + ", service version: "
56 + babelRequest.getArtifactVersion()
57 + ", artifact version: " + babelRequest.getArtifactVersion());
59 List<BabelArtifact> babelArtifacts = babelServiceClient.postArtifact(babelRequest, distributionId);
61 List<Artifact> convertedArtifacts = new ArrayList<>();
62 for(BabelArtifact babelArtifact : babelArtifacts) {
63 if(!isUnknownType(babelArtifact)) {
64 if(babelArtifact.getType() == ArtifactType.MODEL) {
65 convertedArtifacts.addAll(babelArtifactConverter.convertToModel(babelArtifact));
67 convertedArtifacts.add(babelArtifactConverter.convertToCatalog(babelArtifact));
72 return convertedArtifacts;
74 } catch (BabelArtifactParsingException e) {
75 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
76 "Error for artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion()
78 throw new ProcessToscaArtifactsException(
79 "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage());
80 } catch (Exception e) {
81 logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST",
82 "Error posting artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion()
84 + e.getLocalizedMessage());
85 throw new ProcessToscaArtifactsException(
86 "An error occurred while calling the Babel service: " + e.getLocalizedMessage());
90 private boolean isUnknownType(BabelArtifact babelArtifact) {
91 if (babelArtifact.getType() == ArtifactType.MODEL || babelArtifact.getType() == ArtifactType.VNFCATALOG) {
94 logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
95 babelArtifact.getName() + " " + babelArtifact.getType()
96 + ". Unexpected artifact types returned by the babel service: "
97 + babelArtifact.getPayload());