06c09e343128aa86ab9ae559c815bf81762f8431
[so.git] / adapters / mso-cnf-adapter / src / main / java / org / onap / so / adapters / cnf / service / CnfAdapterService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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
21 package org.onap.so.adapters.cnf.service;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.persistence.EntityNotFoundException;
27 import javax.ws.rs.core.UriBuilder;
28 import org.apache.http.HttpStatus;
29 import org.onap.so.adapters.cnf.model.BpmnInstanceRequest;
30 import org.onap.so.adapters.cnf.model.InstanceMiniResponse;
31 import org.onap.so.adapters.cnf.model.InstanceMiniResponseList;
32 import org.onap.so.adapters.cnf.model.InstanceResponse;
33 import org.onap.so.adapters.cnf.model.InstanceStatusResponse;
34 import org.onap.so.adapters.cnf.model.MulticloudInstanceRequest;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Service;
44 import org.springframework.web.client.HttpClientErrorException;
45 import org.springframework.web.client.HttpStatusCodeException;
46 import org.springframework.web.client.RestTemplate;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.databind.JsonMappingException;
49
50 @Service
51 public class CnfAdapterService {
52     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterService.class);
53     @Autowired
54     private RestTemplate restTemplate;
55     private static final String INSTANCE_CREATE_PATH = "/v1/instance";
56     private static final String HEALTH_CHECK = "/v1/healthcheck";
57
58     public ResponseEntity<String> healthCheck() {
59
60         logger.info("CnfAdapterService createInstance called");
61         ResponseEntity<String> result = null;
62         try {
63
64             logger.info("CnfAdapterService createInstance called");
65
66             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
67             // This needs to be added as well
68             // for configuration
69             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
70             String endpoint = UriBuilder.fromUri(uri).path(HEALTH_CHECK).build().toString();
71             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
72             result = restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, String.class);
73             return result;
74         } catch (HttpClientErrorException e) {
75             logger.error("Error Calling Multicloud, e");
76             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
77                 throw new EntityNotFoundException(e.getResponseBodyAsString());
78             }
79             throw e;
80         } catch (HttpStatusCodeException e) {
81             logger.error("Error in Multicloud, e");
82             String responseString = e.getResponseBodyAsString();
83             return ResponseEntity.status(e.getStatusCode()).body(responseString);
84         }
85     }
86
87     public ResponseEntity<InstanceResponse> createInstance(BpmnInstanceRequest bpmnInstanceRequest)
88             throws JsonParseException, JsonMappingException, IOException {
89         try {
90             logger.info("CnfAdapterService createInstance called");
91             MulticloudInstanceRequest multicloudInstanceRequest = new MulticloudInstanceRequest();
92             ResponseEntity<InstanceResponse> instanceResponse = null;
93             if (bpmnInstanceRequest.getK8sRBProfileName() != null) {
94                 multicloudInstanceRequest.setCloudRegion(bpmnInstanceRequest.getCloudRegionId());
95                 multicloudInstanceRequest.setLabels(bpmnInstanceRequest.getLabels());
96                 multicloudInstanceRequest.setOverrideValues(bpmnInstanceRequest.getOverrideValues());
97                 multicloudInstanceRequest.setProfileName(bpmnInstanceRequest.getK8sRBProfileName());
98                 multicloudInstanceRequest.setRbName(bpmnInstanceRequest.getModelInvariantId());
99                 multicloudInstanceRequest.setRbVersion(bpmnInstanceRequest.getModelVersionId());
100                 multicloudInstanceRequest.setVfModuleUuid(bpmnInstanceRequest.getVfModuleUUID());
101             } else {
102                 logger.error("k8sProfileName should not be null");
103                 return instanceResponse;
104             }
105             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
106             // This needs to be added as well
107             // for configuration
108             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
109             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
110             HttpEntity<?> entity = getHttpEntity(multicloudInstanceRequest);
111             instanceResponse = restTemplate.exchange(endpoint, HttpMethod.POST, entity, InstanceResponse.class);
112             return instanceResponse;
113         } catch (HttpClientErrorException e) {
114             logger.error("Error Calling Multicloud, e");
115             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
116                 throw new EntityNotFoundException(e.getResponseBodyAsString());
117             }
118             throw e;
119         } catch (HttpStatusCodeException e) {
120             logger.error("Error in Multicloud, e");
121             String responseString = e.getResponseBodyAsString();
122             InstanceResponse result = new InstanceResponse(responseString.trim());
123             return ResponseEntity.status(e.getStatusCode()).body(result);
124         }
125     }
126
127     public ResponseEntity<InstanceResponse> getInstanceByInstanceId(String instanceId)
128             throws JsonParseException, JsonMappingException, IOException {
129
130         logger.info("CnfAdapterService createInstance called");
131         ResponseEntity<InstanceResponse> instanceResponse = null;
132         try {
133
134             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
135             // This needs to be added as well
136             // for configuration
137             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
138             String path = "/v1/instance/" + instanceId;
139             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
140             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
141             instanceResponse = restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, InstanceResponse.class);
142             return instanceResponse;
143         } catch (HttpClientErrorException e) {
144             logger.error("Error Calling Multicloud, e");
145             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
146                 throw new EntityNotFoundException(e.getResponseBodyAsString());
147             }
148             throw e;
149         } catch (HttpStatusCodeException e) {
150             logger.error("Error in Multicloud, e");
151             String responseString = e.getResponseBodyAsString();
152             InstanceResponse result = new InstanceResponse(responseString.trim());
153             return ResponseEntity.status(e.getStatusCode()).body(result);
154         }
155     }
156
157     public ResponseEntity<InstanceStatusResponse> getInstanceStatusByInstanceId(String instanceId)
158             throws JsonParseException, JsonMappingException, IOException {
159
160         logger.info("CnfAdapterService createInstance called");
161         ResponseEntity<InstanceStatusResponse> instanceResponse = null;
162         try {
163
164             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
165             // This needs to be added as well
166             // for configuration
167             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
168             String path = "/v1/instance/" + instanceId + "/status";
169             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
170             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
171             instanceResponse =
172                     restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, InstanceStatusResponse.class);
173             return instanceResponse;
174         } catch (HttpClientErrorException e) {
175             logger.error("Error Calling Multicloud, e");
176             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
177                 throw new EntityNotFoundException(e.getResponseBodyAsString());
178             }
179             throw e;
180         } catch (HttpStatusCodeException e) {
181             logger.error("Error in Multicloud, e");
182             String responseString = e.getResponseBodyAsString();
183             InstanceStatusResponse result = new InstanceStatusResponse(responseString.trim());
184             return ResponseEntity.status(e.getStatusCode()).body(result);
185         }
186
187     }
188
189     public ResponseEntity<InstanceMiniResponseList> getInstanceByRBNameOrRBVersionOrProfileName(String rbName,
190             String rbVersion, String profileName) throws JsonParseException, JsonMappingException, IOException {
191
192         logger.info("CnfAdapterService createInstance called");
193         ResponseEntity<InstanceMiniResponseList> instanceMiniResponseList = null;
194         try {
195
196             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
197             // This needs to be added as well
198             // for configuration
199             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
200             String path =
201                     "/v1/instance" + "?rb-name=" + rbName + "&rb-version=" + rbVersion + "&profile-name=" + profileName;
202             String endPoint = uri + path;
203             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
204             instanceMiniResponseList =
205                     restTemplate.exchange(endPoint, HttpMethod.GET, requestEntity, InstanceMiniResponseList.class);
206             return instanceMiniResponseList;
207         } catch (HttpClientErrorException e) {
208             logger.error("Error Calling Multicloud, e");
209             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
210                 throw new EntityNotFoundException(e.getResponseBodyAsString());
211             }
212             throw e;
213         } catch (HttpStatusCodeException e) {
214             logger.error("Error in Multicloud, e");
215             String responseString = e.getResponseBodyAsString();
216             InstanceMiniResponseList result = new InstanceMiniResponseList(responseString.trim());
217             return ResponseEntity.status(e.getStatusCode()).body(result);
218         }
219     }
220
221     public ResponseEntity<String> deleteInstanceByInstanceId(String instanceId)
222             throws JsonParseException, JsonMappingException, IOException {
223
224         logger.info("CnfAdapterService createInstance called");
225         ResponseEntity<String> result = null;
226         try {
227
228             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
229             // This needs to be added as well
230             // for configuration
231             String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
232             String path = "/v1/instance/" + instanceId;
233             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
234             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
235             result = restTemplate.exchange(endpoint, HttpMethod.DELETE, requestEntity, String.class);
236             return result;
237         } catch (HttpClientErrorException e) {
238             logger.error("Error Calling Multicloud, e");
239             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
240                 throw new EntityNotFoundException(e.getResponseBodyAsString());
241             }
242             throw e;
243         } catch (HttpStatusCodeException e) {
244             logger.error("Error in Multicloud, e");
245             String responseString = e.getResponseBodyAsString();
246             return ResponseEntity.status(e.getStatusCode()).body(responseString);
247         }
248     }
249
250     protected HttpHeaders getHttpHeaders() {
251         HttpHeaders headers = new HttpHeaders();
252         List<MediaType> acceptableMediaTypes = new ArrayList<>();
253         acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
254         headers.setAccept(acceptableMediaTypes);
255         headers.setContentType(MediaType.APPLICATION_JSON);
256         /*
257          * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"),
258          * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION,
259          * "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes())); } } catch
260          * (GeneralSecurityException e) { logger.error("Security exception", e); }
261          */
262         return headers;
263     }
264
265     protected HttpEntity<?> getHttpEntity(MulticloudInstanceRequest request) {
266         HttpHeaders headers = getHttpHeaders();
267         return new HttpEntity<>(request, headers);
268     }
269 }