2 * ============LICENSE_START=======================================================
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
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.apihandlerinfra;
23 import org.apache.http.HttpStatus;
24 import org.onap.so.db.request.beans.InfraActiveRequests;
25 import org.onap.so.db.request.data.controller.InstanceNameDuplicateCheckRequest;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.core.ParameterizedTypeReference;
30 import org.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.HttpRequest;
34 import org.springframework.http.client.ClientHttpRequestExecution;
35 import org.springframework.http.client.ClientHttpRequestInterceptor;
36 import org.springframework.http.client.ClientHttpResponse;
37 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
38 import org.springframework.stereotype.Component;
39 import org.springframework.web.client.HttpClientErrorException;
40 import org.springframework.web.client.RestTemplate;
41 import uk.co.blackpepper.bowman.Client;
42 import uk.co.blackpepper.bowman.ClientFactory;
43 import uk.co.blackpepper.bowman.Configuration;
44 import uk.co.blackpepper.bowman.RestTemplateConfigurer;
46 import javax.annotation.PostConstruct;
47 import java.io.IOException;
49 import java.util.HashMap;
50 import java.util.List;
53 @Component("RequestDbClient")
54 public class RequestsDbClient {
56 private Client<InfraActiveRequests> infraActiveRequestClient;
58 @Value("${mso.adapters.requestDb.endpoint}")
59 private String endpoint;
61 @Value("${mso.adapters.requestDb.auth}")
62 private String msoAdaptersAuth;
64 private String getOrchestrationFilterURI = "/infraActiveRequests/getOrchestrationFiltersFromInfraActive/";
66 private String checkVnfIdStatus = "/infraActiveRequests/checkVnfIdStatus/";
68 private String infraActiveRequestURI = "/infraActiveRequests/";
70 private String checkInstanceNameDuplicate = "/infraActiveRequests/checkInstanceNameDuplicate";
72 private String cloudOrchestrationFiltersFromInfraActive = "/infraActiveRequests/getCloudOrchestrationFiltersFromInfraActive";
74 private HttpHeaders headers;
77 private RestTemplate restTemplate;
81 getOrchestrationFilterURI = endpoint + getOrchestrationFilterURI;
82 infraActiveRequestURI = endpoint + infraActiveRequestURI;
83 checkVnfIdStatus = endpoint + checkVnfIdStatus;
84 checkInstanceNameDuplicate = endpoint + checkInstanceNameDuplicate;
85 cloudOrchestrationFiltersFromInfraActive = endpoint + cloudOrchestrationFiltersFromInfraActive;
86 headers = new HttpHeaders();
87 headers.set("Authorization", msoAdaptersAuth);
90 public RequestsDbClient() {
91 ClientFactory clientFactory = Configuration.builder().setRestTemplateConfigurer(new RestTemplateConfigurer() {
93 public void configure(RestTemplate restTemplate) {
95 restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {
97 public ClientHttpResponse intercept(HttpRequest request, byte[] body,
98 ClientHttpRequestExecution execution) throws IOException {
100 request.getHeaders().add("Authorization", msoAdaptersAuth);
101 return execution.execute(request, body);
105 }).build().buildClientFactory();
106 infraActiveRequestClient = clientFactory.create(InfraActiveRequests.class);
109 public List<InfraActiveRequests> getCloudOrchestrationFiltersFromInfraActive(Map<String, String> orchestrationMap){
110 URI uri = getUri(cloudOrchestrationFiltersFromInfraActive);
111 HttpEntity<Map> entity = new HttpEntity<>(orchestrationMap, headers);
113 return restTemplate.exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {}).getBody();
114 }catch(HttpClientErrorException e){
115 if(HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()){
121 public InfraActiveRequests getInfraActiveRequestbyRequestId(String requestId) {
122 return this.getSingleInfraActiveRequests(this.getUri(endpoint + "/infraActiveRequests/" + requestId));
125 public List<InfraActiveRequests> getOrchestrationFiltersFromInfraActive(Map<String, List<String>> orchestrationMap) {
126 URI uri = getUri(getOrchestrationFilterURI);
127 HttpEntity<Map<String, List<String>>> entity = new HttpEntity<>(orchestrationMap, headers);
128 return restTemplate.exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {}).getBody();
131 public InfraActiveRequests checkVnfIdStatus(String operationalEnvironmentId) {
132 URI uri = getUri(checkVnfIdStatus + operationalEnvironmentId);
133 return restTemplate.exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, InfraActiveRequests.class).getBody();
135 public InfraActiveRequests checkInstanceNameDuplicate(HashMap<String, String> instanceIdMap, String instanceName, String requestScope) {
136 URI uri = getUri(checkInstanceNameDuplicate);
137 HttpEntity<InstanceNameDuplicateCheckRequest> entity = new HttpEntity<>(new InstanceNameDuplicateCheckRequest(instanceIdMap, instanceName, requestScope), headers);
139 return restTemplate.exchange(uri, HttpMethod.POST, entity, InfraActiveRequests.class).getBody();
140 }catch(HttpClientErrorException e){
141 if(HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()){
149 public void save(InfraActiveRequests infraActiveRequests) {
150 URI uri = getUri(infraActiveRequestURI);
151 HttpEntity<InfraActiveRequests> entity = new HttpEntity<>(infraActiveRequests, headers);
152 restTemplate.postForLocation(uri, entity);
155 protected InfraActiveRequests getSingleInfraActiveRequests(URI uri) {
156 return infraActiveRequestClient.get(uri);
159 public void updateInfraActiveRequests(InfraActiveRequests request) {
160 infraActiveRequestClient.put(request);
163 protected URI getUri(String uri) {
164 return URI.create(uri);
168 public RestTemplate restTemplate() {
169 return new RestTemplate( new HttpComponentsClientHttpRequestFactory());