Merge "Update of developer info for bb"
[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.ResponseEntity;
38 import org.springframework.retry.annotation.Backoff;
39 import org.springframework.retry.annotation.Retryable;
40 import org.springframework.stereotype.Component;
41 import org.springframework.web.client.HttpClientErrorException;
42 import org.springframework.web.client.HttpServerErrorException;
43 import org.springframework.web.client.RestTemplate;
44
45 @Component
46 public class CnfAdapterClient {
47
48     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterClient.class);
49
50     @Autowired
51     private RestTemplate restTemplate;
52
53     @Autowired
54     private Environment env;
55
56     private static final String INSTANCE_CREATE_PATH = "/api/multicloud-k8s/v1/v1/instance";
57
58     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
59     public InstanceResponse createVfModule(InstanceRequest request) throws CnfAdapterClientException {
60         try {
61             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
62             // for configuration
63             String uri = "https://localhost:32780"; // TODO: What is the correct uri?
64             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
65             HttpEntity<?> entity = getHttpEntity(request);
66             ResponseEntity<InstanceResponse> result =
67                     restTemplate.exchange(endpoint, HttpMethod.POST, entity, InstanceResponse.class);
68             return result.getBody();
69         } catch (HttpClientErrorException e) {
70             logger.error("Error Calling CNF Adapter, e");
71             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
72                 throw new EntityNotFoundException(e.getResponseBodyAsString());
73             }
74             throw e;
75         }
76     }
77
78     protected HttpHeaders getHttpHeaders() {
79         HttpHeaders headers = new HttpHeaders();
80         List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>();
81         acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
82         headers.setAccept(acceptableMediaTypes);
83         /*
84          * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"),
85          * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION,
86          * "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes())); } } catch
87          * (GeneralSecurityException e) { logger.error("Security exception", e); }
88          */
89         return headers;
90     }
91
92     protected HttpEntity<?> getHttpEntity(InstanceRequest request) {
93         HttpHeaders headers = getHttpHeaders();
94         return new HttpEntity<>(request, headers);
95     }
96
97 }