2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.cnf.service;
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;
51 public class CnfAdapterService {
52 private static final Logger logger = LoggerFactory.getLogger(CnfAdapterService.class);
54 private RestTemplate restTemplate;
55 private static final String INSTANCE_CREATE_PATH = "/v1/instance";
56 private static final String HEALTH_CHECK = "/v1/healthcheck";
58 public ResponseEntity<String> healthCheck() {
60 logger.info("CnfAdapterService createInstance called");
61 ResponseEntity<String> result = null;
64 logger.info("CnfAdapterService createInstance called");
66 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
67 // This needs to be added as well
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);
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());
80 } catch (HttpStatusCodeException e) {
81 logger.error("Error in Multicloud, e");
82 String responseString = e.getResponseBodyAsString();
83 return ResponseEntity.status(e.getStatusCode()).body(responseString);
87 public ResponseEntity<InstanceResponse> createInstance(BpmnInstanceRequest bpmnInstanceRequest)
88 throws JsonParseException, JsonMappingException, IOException {
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());
102 logger.error("k8sProfileName should not be null");
103 return instanceResponse;
105 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
106 // This needs to be added as well
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());
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);
127 public ResponseEntity<InstanceResponse> getInstanceByInstanceId(String instanceId)
128 throws JsonParseException, JsonMappingException, IOException {
130 logger.info("CnfAdapterService createInstance called");
131 ResponseEntity<InstanceResponse> instanceResponse = null;
134 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
135 // This needs to be added as well
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());
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);
157 public ResponseEntity<InstanceStatusResponse> getInstanceStatusByInstanceId(String instanceId)
158 throws JsonParseException, JsonMappingException, IOException {
160 logger.info("CnfAdapterService createInstance called");
161 ResponseEntity<InstanceStatusResponse> instanceResponse = null;
164 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
165 // This needs to be added as well
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());
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());
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);
189 public ResponseEntity<InstanceMiniResponseList> getInstanceByRBNameOrRBVersionOrProfileName(String rbName,
190 String rbVersion, String profileName) throws JsonParseException, JsonMappingException, IOException {
192 logger.info("CnfAdapterService createInstance called");
193 ResponseEntity<InstanceMiniResponseList> instanceMiniResponseList = null;
196 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
197 // This needs to be added as well
199 String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
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());
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);
221 public ResponseEntity<String> deleteInstanceByInstanceId(String instanceId)
222 throws JsonParseException, JsonMappingException, IOException {
224 logger.info("CnfAdapterService createInstance called");
225 ResponseEntity<String> result = null;
228 // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
229 // This needs to be added as well
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);
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());
243 } catch (HttpStatusCodeException e) {
244 logger.error("Error in Multicloud, e");
245 String responseString = e.getResponseBodyAsString();
246 return ResponseEntity.status(e.getStatusCode()).body(responseString);
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);
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); }
265 protected HttpEntity<?> getHttpEntity(MulticloudInstanceRequest request) {
266 HttpHeaders headers = getHttpHeaders();
267 return new HttpEntity<>(request, headers);