Merge "adding test case for Csar distribution Issue-ID: SO-3080"
[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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.env.Environment;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.retry.annotation.Backoff;
40 import org.springframework.retry.annotation.Retryable;
41 import org.springframework.stereotype.Component;
42 import org.springframework.web.client.HttpClientErrorException;
43 import org.springframework.web.client.HttpServerErrorException;
44 import org.springframework.web.client.RestTemplate;
45
46 @Component
47 public class CnfAdapterClient {
48
49     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterClient.class);
50
51     @Autowired
52     private RestTemplate restTemplate;
53
54     @Autowired
55     private Environment env;
56
57     private static final String INSTANCE_CREATE_PATH = "/api/cnf-adapter/v1/instance";
58
59     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
60     public InstanceResponse createVfModule(InstanceRequest request) throws CnfAdapterClientException {
61         try {
62             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
63             // for configuration
64             String uri = "http://so-cnf-adapter:8090";
65             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
66             HttpEntity<?> entity = getHttpEntity(request);
67             ResponseEntity<InstanceResponse> result =
68                     restTemplate.exchange(endpoint, HttpMethod.POST, entity, InstanceResponse.class);
69             return result.getBody();
70         } catch (HttpClientErrorException e) {
71             logger.error("Error Calling CNF Adapter, e");
72             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
73                 throw new EntityNotFoundException(e.getResponseBodyAsString());
74             }
75             throw e;
76         }
77     }
78
79
80     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
81     public void deleteVfModule(String heatStackId) throws CnfAdapterClientException {
82         try {
83             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
84             // for configuration
85             String uri = "http://so-cnf-adapter:8090";
86             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH + "/" + heatStackId).build().toString();
87             HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
88             restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class);
89         } catch (HttpClientErrorException e) {
90             logger.error("Error Calling CNF Adapter, e");
91             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
92                 throw new EntityNotFoundException(e.getResponseBodyAsString());
93             }
94             throw e;
95         }
96     }
97
98     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
99     public InstanceResponse healthcheck() throws CnfAdapterClientException {
100         try {
101             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
102             // for configuration
103             String uri = "http://so-cnf-adapter:8090";
104             String endpoint = UriBuilder.fromUri(uri).path("/api/cnf-adapter/v1/healthcheck").build().toString();
105             HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
106             ResponseEntity<InstanceResponse> result =
107                     restTemplate.exchange(endpoint, HttpMethod.GET, entity, InstanceResponse.class);
108             return result.getBody();
109         } catch (HttpClientErrorException e) {
110             logger.error("Error Calling CNF Adapter, e");
111             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
112                 throw new EntityNotFoundException(e.getResponseBodyAsString());
113             }
114             throw e;
115         }
116     }
117
118     protected HttpHeaders getHttpHeaders() {
119         HttpHeaders headers = new HttpHeaders();
120         List<MediaType> acceptableMediaTypes = new ArrayList<>();
121         acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
122         headers.setAccept(acceptableMediaTypes);
123         headers.setContentType(MediaType.APPLICATION_JSON);
124         /*
125          * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"),
126          * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION,
127          * "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes())); } } catch
128          * (GeneralSecurityException e) { logger.error("Security exception", e); }
129          */
130         return headers;
131     }
132
133     protected HttpEntity<?> getHttpEntity(InstanceRequest request) {
134         HttpHeaders headers = getHttpHeaders();
135         return new HttpEntity<>(request, headers);
136     }
137
138 }