Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / restclient / BabelServiceClientImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 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
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
22 package org.onap.aai.modelloader.restclient;
23
24 import java.util.List;
25 import java.util.stream.Collectors;
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.config.ModelLoaderConfig;
32 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
33 import org.springframework.core.ParameterizedTypeReference;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Component;
40 import org.springframework.web.client.RestTemplate;
41
42 /**
43  * HTTPS Client for interfacing with Babel.
44  *
45  */
46 @Component
47 public class BabelServiceClientImpl implements BabelServiceClient {
48
49     private static final Logger logger = LoggerFactory.getInstance().getLogger(BabelServiceClientImpl.class);
50     private final ModelLoaderConfig config;
51     private final RestTemplate restTemplate;
52
53     public BabelServiceClientImpl(ModelLoaderConfig config, RestTemplate restTemplate) {
54         this.config = config;
55         this.restTemplate = restTemplate;
56     }
57
58     @Override
59     public List<BabelArtifact> postArtifact(BabelRequest babelRequest, String transactionId) throws BabelServiceClientException {
60         if (logger.isInfoEnabled()) {
61             logger.info(ModelLoaderMsgs.BABEL_REST_REQUEST_PAYLOAD, " Artifact Name: " + babelRequest.getArtifactName()
62                     + " Artifact version: " + babelRequest.getArtifactVersion() + " Artifact payload: " + babelRequest.getCsar());
63         }
64
65         String resourceUrl = config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl();
66
67         HttpHeaders headers = new HttpHeaders();
68         headers.set(AaiRestClient.HEADER_TRANS_ID, transactionId);
69         headers.set(AaiRestClient.HEADER_FROM_APP_ID, AaiRestClient.ML_APP_NAME);
70         HttpEntity<BabelRequest> entity = new HttpEntity<>(babelRequest, headers);
71
72         ResponseEntity<List<BabelArtifact>> artifactResponse = restTemplate.exchange(resourceUrl, HttpMethod.POST, entity, new ParameterizedTypeReference<List<BabelArtifact>>() {});
73
74         if (logger.isDebugEnabled()) {
75             logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT,
76                     "Babel response " + artifactResponse.getStatusCode() + " " + artifactResponse.getBody().toString());
77         }
78
79         if (!artifactResponse.getStatusCode().equals(HttpStatus.OK)) {
80             throw new BabelServiceClientException(artifactResponse.getBody().toString());
81         }
82
83         return artifactResponse.getBody();
84     }
85 }