Add spring-boot actuator to model-loader
[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.ArrayList;
23 import java.util.List;
24
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;
37
38 @Service
39 public class BabelArtifactService {
40
41     private static Logger logger = LoggerFactory.getInstance().getLogger(BabelArtifactService.class);
42
43     private final BabelServiceClient babelServiceClient;
44     private final BabelArtifactConverter babelArtifactConverter;
45
46     public BabelArtifactService(BabelServiceClient babelServiceClient, BabelArtifactConverter babelArtifactConverter) {
47         this.babelServiceClient = babelServiceClient;
48         this.babelArtifactConverter = babelArtifactConverter;
49     }
50
51     public List<Artifact> invokeBabelService(BabelRequest babelRequest, String distributionId)
52             throws ProcessToscaArtifactsException {
53         try {
54             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
55                     "Posting artifact: " + babelRequest.getArtifactName() + ", service version: "
56                             + babelRequest.getArtifactVersion()
57                             + ", artifact version: " + babelRequest.getArtifactVersion());
58
59             List<BabelArtifact> babelArtifacts = babelServiceClient.postArtifact(babelRequest, distributionId);
60
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));
66                     } else {
67                         convertedArtifacts.add(babelArtifactConverter.convertToCatalog(babelArtifact));
68                     }
69                 }
70             }
71
72             return convertedArtifacts;
73
74         } catch (BabelArtifactParsingException e) {
75             logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
76                     "Error for artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion()
77                             + " " + e);
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()
83                             + " to Babel: "
84                             + e.getLocalizedMessage());
85             throw new ProcessToscaArtifactsException(
86                     "An error occurred while calling the Babel service: " + e.getLocalizedMessage());
87         }
88     }
89
90     private boolean isUnknownType(BabelArtifact babelArtifact) {
91         if (babelArtifact.getType() == ArtifactType.MODEL || babelArtifact.getType() == ArtifactType.VNFCATALOG) {
92             return false;
93         } else {
94             logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
95                     babelArtifact.getName() + " " + babelArtifact.getType()
96                             + ". Unexpected artifact types returned by the babel service: "
97                             + babelArtifact.getPayload());
98             return true;
99         }
100     }
101
102 }