721fe2fe6b90c2fc9fa99e7e1a341743d2d4b881
[so.git] /
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.apihandlerinfra;
22
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;
45
46 import javax.annotation.PostConstruct;
47 import java.io.IOException;
48 import java.net.URI;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52
53 @Component("RequestDbClient")
54 public class RequestsDbClient {
55
56         private Client<InfraActiveRequests> infraActiveRequestClient;
57
58         @Value("${mso.adapters.db.spring.endpoint:}")
59         private String endpoint;
60         
61         @Value("${mso.db.auth:}")
62         private String msoAdaptersAuth;
63
64         private String getOrchestrationFilterURI = "/getOrchestrationFiltersFromInfraActive/";
65
66         private String checkVnfIdStatus = "/infraActiveRequests/checkVnfIdStatus/";
67
68         private String infraActiveRequestURI = "/infraActiveRequests/";
69
70         private String checkInstanceNameDuplicate = "/infraActiveRequests/checkInstanceNameDuplicate";
71
72         private String cloudOrchestrationFiltersFromInfraActive = "/infraActiveRequests/getCloudOrchestrationFiltersFromInfraActive";
73
74         private HttpHeaders headers;
75
76         @Autowired
77         private RestTemplate restTemplate;
78
79         @PostConstruct
80         public void init() {
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);
88         }
89
90         public RequestsDbClient() {
91                 ClientFactory clientFactory = Configuration.builder().setRestTemplateConfigurer(new RestTemplateConfigurer() {
92
93                         public void configure(RestTemplate restTemplate) {
94
95                                 restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {
96
97                                         public ClientHttpResponse intercept(HttpRequest request, byte[] body,
98                                                         ClientHttpRequestExecution execution) throws IOException {
99
100                                                 request.getHeaders().add("Authorization", msoAdaptersAuth);
101                                                 return execution.execute(request, body);
102                                         }
103                                 });
104                         }
105                 }).build().buildClientFactory();
106                 infraActiveRequestClient = clientFactory.create(InfraActiveRequests.class);
107        
108         }
109         public List<InfraActiveRequests> getCloudOrchestrationFiltersFromInfraActive(Map<String, String> orchestrationMap){
110                 URI uri = getUri(cloudOrchestrationFiltersFromInfraActive);
111                 HttpEntity<Map> entity = new HttpEntity<>(orchestrationMap, headers);
112                 try{
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()){
116                                 return null;
117                         }
118                         throw e;
119                 }
120         }
121         public InfraActiveRequests getInfraActiveRequestbyRequestId(String requestId) {
122                 return this.getSingleInfraActiveRequests(this.getUri(endpoint + "/infraActiveRequests/" + requestId));
123         }
124
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();
129         }
130
131         public InfraActiveRequests checkVnfIdStatus(String operationalEnvironmentId) {
132                 URI uri = getUri(checkVnfIdStatus + operationalEnvironmentId);
133                 return restTemplate.exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, InfraActiveRequests.class).getBody();
134         }
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);
138                 try{
139                         return restTemplate.exchange(uri, HttpMethod.POST, entity, InfraActiveRequests.class).getBody();
140                 }catch(HttpClientErrorException e){
141                         if(HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()){
142                                 return null;
143                         }
144                         throw e;
145                 }
146
147         }
148
149         public void save(InfraActiveRequests infraActiveRequests) {
150                 URI uri = getUri(infraActiveRequestURI);
151                 HttpEntity<InfraActiveRequests> entity = new HttpEntity<>(infraActiveRequests, headers);
152                 restTemplate.postForLocation(uri, entity);
153         }
154
155         protected InfraActiveRequests getSingleInfraActiveRequests(URI uri) {
156                 return infraActiveRequestClient.get(uri);
157         }
158
159         public void updateInfraActiveRequests(InfraActiveRequests request) {            
160                 infraActiveRequestClient.put(request);
161         }
162
163         protected URI getUri(String uri) {
164                 return URI.create(uri);
165         }
166
167         @Bean
168         public RestTemplate restTemplate() {
169                 return new RestTemplate( new HttpComponentsClientHttpRequestFactory());
170         }
171 }