2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.modelloader.restclient;
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;
43 * HTTPS Client for interfacing with Babel.
47 public class BabelServiceClientImpl implements BabelServiceClient {
49 private static final Logger logger = LoggerFactory.getInstance().getLogger(BabelServiceClientImpl.class);
50 private final ModelLoaderConfig config;
51 private final RestTemplate restTemplate;
53 public BabelServiceClientImpl(ModelLoaderConfig config, RestTemplate restTemplate) {
55 this.restTemplate = restTemplate;
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());
65 String resourceUrl = config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl();
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);
72 ResponseEntity<List<org.onap.aai.modelloader.babel.BabelArtifact>> artifactResponse = restTemplate.exchange(resourceUrl, HttpMethod.POST, entity, new ParameterizedTypeReference<List<org.onap.aai.modelloader.babel.BabelArtifact>>() {});
74 if (logger.isDebugEnabled()) {
75 logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT,
76 "Babel response " + artifactResponse.getStatusCode() + " " + artifactResponse.getBody().toString());
79 if (!artifactResponse.getStatusCode().equals(HttpStatus.OK)) {
80 throw new BabelServiceClientException(artifactResponse.getBody().toString());
83 List<BabelArtifact> babelArtifact = artifactResponse.getBody().stream()
84 .map(this::mapBabelDto)
85 .collect(Collectors.toList());
90 private BabelArtifact mapBabelDto(org.onap.aai.modelloader.babel.BabelArtifact babelDto) {
91 ArtifactType artifactType = babelDto.getType() == null
93 : ArtifactType.valueOf(babelDto.getType().name());
94 return new BabelArtifact(
97 babelDto.getPayload());