Open Kilda Java Documentation
DefaultServiceConfig.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.testing.config;
17 
19 
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.context.annotation.Bean;
24 import org.springframework.context.annotation.ComponentScan;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.context.annotation.Profile;
27 import org.springframework.context.annotation.PropertySource;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.client.BufferingClientHttpRequestFactory;
30 import org.springframework.http.client.ClientHttpRequestInterceptor;
31 import org.springframework.http.client.ClientHttpResponse;
32 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
33 import org.springframework.http.client.support.BasicAuthorizationInterceptor;
34 import org.springframework.web.client.DefaultResponseErrorHandler;
35 import org.springframework.web.client.ResponseErrorHandler;
36 import org.springframework.web.client.RestClientResponseException;
37 import org.springframework.web.client.RestTemplate;
38 import org.springframework.web.util.DefaultUriBuilderFactory;
39 
40 import java.io.IOException;
41 import java.util.List;
42 
43 @Configuration
44 @Profile("default")
45 @PropertySource("file:${kilda.config.file:kilda.properties}")
46 @ComponentScan(basePackages = {"org.openkilda.testing.service", "org.openkilda.testing.tools"})
47 public class DefaultServiceConfig {
48 
49  @Bean(name = "northboundRestTemplate")
50  public RestTemplate northboundRestTemplate(
51  @Value("${northbound.endpoint}") String endpoint,
52  @Value("${northbound.username}") String username,
53  @Value("${northbound.password}") String password) {
54  return buildRestTemplateWithAuth(endpoint, username, password);
55  }
56 
57  @Bean(name = "floodlightRestTemplate")
58  public RestTemplate floodlightRestTemplate(
59  @Value("${floodlight.endpoint}") String endpoint,
60  @Value("${floodlight.username}") String username,
61  @Value("${floodlight.password}") String password) {
62  return buildRestTemplateWithAuth(endpoint, username, password);
63  }
64 
65  @Bean(name = "topologyEngineRestTemplate")
66  public RestTemplate topologyEngineRestTemplate(
67  @Value("${topology-engine-rest.endpoint}") String endpoint,
68  @Value("${topology-engine-rest.username}") String username,
69  @Value("${topology-engine-rest.password}") String password) {
70  return buildRestTemplateWithAuth(endpoint, username, password);
71  }
72 
73  @Bean(name = "traffExamRestTemplate")
74  public RestTemplate traffExamRestTemplate() {
75  RestTemplate restTemplate = buildLoggingRestTemplate();
76  restTemplate.setErrorHandler(buildErrorHandler());
77  return restTemplate;
78  }
79 
80  @Bean(name = "aSwitchRestTemplate")
81  public RestTemplate aswitchRestTemplate(@Value("${aswitch.endpoint}") String endpoint) {
82  return buildLoggingRestTemplate(endpoint);
83  }
84 
85  private RestTemplate buildLoggingRestTemplate(String endpoint) {
86  final RestTemplate restTemplate = buildLoggingRestTemplate();
87  restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(endpoint));
88  return restTemplate;
89  }
90 
91  private RestTemplate buildLoggingRestTemplate() {
92  final RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(
93  new HttpComponentsClientHttpRequestFactory()));
94  List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
95  interceptors.add(new LoggingRequestInterceptor());
96  restTemplate.setErrorHandler(buildErrorHandler());
97  return restTemplate;
98  }
99 
100  private RestTemplate buildRestTemplateWithAuth(String endpoint, String username, String password) {
101  RestTemplate restTemplate = buildLoggingRestTemplate(endpoint);
102  restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
103  return restTemplate;
104  }
105 
106  private ResponseErrorHandler buildErrorHandler() {
107  return new DefaultResponseErrorHandler() {
108  private final Logger logger = LoggerFactory.getLogger(ResponseErrorHandler.class);
109 
110  @Override
111  public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
112  try {
113  super.handleError(clientHttpResponse);
114  } catch (RestClientResponseException e) {
115  if (e.getRawStatusCode() != HttpStatus.NOT_FOUND.value()) {
116  logger.error("HTTP response with status {} and body '{}'", e.getRawStatusCode(),
117  e.getResponseBodyAsString());
118  }
119  throw e;
120  }
121  }
122  };
123  }
124 }
name
Definition: setup.py:24
def status()
Definition: rest.py:593