Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / babel / BabelArtifactService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.modelloader.babel;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.stream.Collectors;
25
26 import org.onap.aai.babel.service.data.BabelArtifact;
27 import org.onap.aai.babel.service.data.BabelRequest;
28 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.modelloader.entity.Artifact;
32 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
33 import org.onap.aai.modelloader.notification.BabelArtifactConverter;
34 import org.onap.aai.modelloader.notification.ProcessToscaArtifactsException;
35 import org.onap.aai.modelloader.restclient.BabelServiceClient;
36 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
37 import org.springframework.stereotype.Service;
38
39 @Service
40 public class BabelArtifactService {
41
42   private static Logger logger = LoggerFactory.getInstance().getLogger(BabelArtifactService.class);
43
44   private final BabelServiceClient babelServiceClient;
45   private final BabelArtifactConverter babelArtifactConverter;
46
47   public BabelArtifactService(BabelServiceClient babelServiceClient, BabelArtifactConverter babelArtifactConverter) {
48     this.babelServiceClient = babelServiceClient;
49     this.babelArtifactConverter = babelArtifactConverter;
50   }
51
52   public void invokeBabelService(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, BabelRequest babelRequest, String distributionId)
53           throws ProcessToscaArtifactsException {
54       try {
55           logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
56                   "Posting artifact: " + babelRequest.getArtifactName() + ", service version: " + babelRequest.getArtifactVersion()
57                           + ", artifact version: " + babelRequest.getArtifactVersion());
58   
59           List<BabelArtifact> babelArtifacts =
60                 babelServiceClient.postArtifact(babelRequest, distributionId);
61   
62           // Sort Babel artifacts based on type
63           Map<ArtifactType, List<BabelArtifact>> artifactMap =
64                   babelArtifacts.stream().collect(Collectors.groupingBy(BabelArtifact::getType));
65   
66           if (artifactMap.containsKey(BabelArtifact.ArtifactType.MODEL)) {
67               modelArtifacts.addAll(
68                       babelArtifactConverter.convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL)));
69               artifactMap.remove(BabelArtifact.ArtifactType.MODEL);
70           }
71   
72           if (artifactMap.containsKey(BabelArtifact.ArtifactType.VNFCATALOG)) {
73               catalogArtifacts.addAll(babelArtifactConverter
74                       .convertToCatalog(artifactMap.get(BabelArtifact.ArtifactType.VNFCATALOG)));
75               artifactMap.remove(BabelArtifact.ArtifactType.VNFCATALOG);
76           }
77   
78           // Log unexpected artifact types
79           if (!artifactMap.isEmpty()) {
80               logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
81                       babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion()
82                               + ". Unexpected artifact types returned by the babel service: "
83                               + artifactMap.keySet().toString());
84           }
85   
86       } catch (BabelArtifactParsingException e) {
87           logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
88                   "Error for artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + " " + e);
89           throw new ProcessToscaArtifactsException(
90                   "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage());
91       } catch (Exception e) {
92           logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST",
93                   "Error posting artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + " to Babel: "
94                           + e.getLocalizedMessage());
95           throw new ProcessToscaArtifactsException(
96                   "An error occurred while calling the Babel service: " + e.getLocalizedMessage());
97       }
98   }
99   
100 }