e8122e778459d779b57993f244cb4957af8c81ee
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / adapter / cnf / CnfAdapterClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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
21 package org.onap.so.client.adapter.cnf;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.persistence.EntityNotFoundException;
26 import javax.ws.rs.core.UriBuilder;
27 import org.apache.http.HttpStatus;
28 import org.onap.so.client.adapter.cnf.entities.InstanceRequest;
29 import org.onap.so.client.adapter.cnf.entities.InstanceResponse;
30 import org.onap.so.client.adapter.cnf.entities.UpgradeInstanceResponse;
31 import org.onap.so.client.adapter.cnf.entities.UpgradeInstanceRequest;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.core.env.Environment;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.retry.annotation.Backoff;
42 import org.springframework.retry.annotation.Retryable;
43 import org.springframework.stereotype.Component;
44 import org.springframework.web.client.HttpClientErrorException;
45 import org.springframework.web.client.HttpServerErrorException;
46 import org.springframework.web.client.RestTemplate;
47
48 @Component
49 public class CnfAdapterClient {
50
51     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterClient.class);
52
53     @Autowired
54     private RestTemplate restTemplate;
55
56     @Autowired
57     private Environment env;
58
59     private static final String INSTANCE_CREATE_PATH = "/api/cnf-adapter/v1/instance";
60
61     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
62     public InstanceResponse createVfModule(InstanceRequest request) throws CnfAdapterClientException {
63         try {
64             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
65             // for configuration
66             String uri = "http://so-cnf-adapter:8090";
67             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
68             HttpEntity<?> entity = getHttpEntity(request);
69             ResponseEntity<InstanceResponse> result =
70                     restTemplate.exchange(endpoint, HttpMethod.POST, entity, InstanceResponse.class);
71             return result.getBody();
72         } catch (HttpClientErrorException e) {
73             logger.error("Error Calling CNF Adapter, e");
74             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
75                 throw new EntityNotFoundException(e.getResponseBodyAsString());
76             }
77             throw e;
78         }
79     }
80
81
82     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
83     public void deleteVfModule(String heatStackId) throws CnfAdapterClientException {
84         try {
85             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
86             // for configuration
87             String uri = "http://so-cnf-adapter:8090";
88             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH + "/" + heatStackId).build().toString();
89             HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
90             restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class);
91         } catch (HttpClientErrorException e) {
92             logger.error("Error Calling CNF Adapter, e");
93             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
94                 throw new EntityNotFoundException(e.getResponseBodyAsString());
95             }
96             throw e;
97         }
98     }
99
100     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
101     public InstanceResponse healthcheck() throws CnfAdapterClientException {
102         try {
103             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
104             // for configuration
105             String uri = "http://so-cnf-adapter:8090";
106             String endpoint = UriBuilder.fromUri(uri).path("/api/cnf-adapter/v1/healthcheck").build().toString();
107             HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
108             ResponseEntity<InstanceResponse> result =
109                     restTemplate.exchange(endpoint, HttpMethod.GET, entity, InstanceResponse.class);
110             return result.getBody();
111         } catch (HttpClientErrorException e) {
112             logger.error("Error Calling CNF Adapter, e");
113             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
114                 throw new EntityNotFoundException(e.getResponseBodyAsString());
115             }
116             throw e;
117         }
118     }
119
120     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
121     public UpgradeInstanceResponse upgradeVfModule(UpgradeInstanceRequest request) throws CnfAdapterClientException {
122         try {
123             String uri = "http://so-cnf-adapter:8090";
124             String endpoint = UriBuilder.fromUri(uri).path("/api/cnf-adapter/v1/instance/{instanceID}/upgrade").build()
125                     .toString();
126             HttpEntity<?> entity = getHttpEntity(request);
127             ResponseEntity<UpgradeInstanceResponse> result =
128                     restTemplate.exchange(endpoint, HttpMethod.POST, entity, UpgradeInstanceResponse.class);
129             return result.getBody();
130         } catch (HttpClientErrorException e) {
131             logger.error("Error Calling CNF Adapter, e");
132             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
133                 throw new EntityNotFoundException(e.getResponseBodyAsString());
134             }
135             throw e;
136         }
137     }
138
139     protected HttpHeaders getHttpHeaders() {
140         HttpHeaders headers = new HttpHeaders();
141         List<MediaType> acceptableMediaTypes = new ArrayList<>();
142         acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
143         headers.setAccept(acceptableMediaTypes);
144         headers.setContentType(MediaType.APPLICATION_JSON);
145         /*
146          * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"),
147          * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION,
148          * "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes())); } } catch
149          * (GeneralSecurityException e) { logger.error("Security exception", e); }
150          */
151         return headers;
152     }
153
154     protected HttpEntity<?> getHttpEntity(InstanceRequest request) {
155         HttpHeaders headers = getHttpHeaders();
156         return new HttpEntity<>(request, headers);
157     }
158
159     protected HttpEntity<?> getHttpEntity(UpgradeInstanceRequest request) {
160         HttpHeaders headers = getHttpHeaders();
161         return new HttpEntity<>(request, headers);
162     }
163 }