16 package org.openkilda.testing.config;
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;
40 import java.io.IOException;
41 import java.util.List;
45 @PropertySource(
"file:${kilda.config.file:kilda.properties}")
46 @ComponentScan(basePackages = {
"org.openkilda.testing.service",
"org.openkilda.testing.tools"})
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); 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); 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); 73 @Bean(name = "traffExamRestTemplate
") 74 public RestTemplate traffExamRestTemplate() { 75 RestTemplate restTemplate = buildLoggingRestTemplate(); 76 restTemplate.setErrorHandler(buildErrorHandler()); 80 @Bean(name = "aSwitchRestTemplate
") 81 public RestTemplate aswitchRestTemplate(@Value("${aswitch.endpoint}
") String endpoint) { 82 return buildLoggingRestTemplate(endpoint); 85 private RestTemplate buildLoggingRestTemplate(String endpoint) { 86 final RestTemplate restTemplate = buildLoggingRestTemplate(); 87 restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(endpoint)); 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()); 100 private RestTemplate buildRestTemplateWithAuth(String endpoint, String username, String password) { 101 RestTemplate restTemplate = buildLoggingRestTemplate(endpoint); 102 restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password)); 106 private ResponseErrorHandler buildErrorHandler() { 107 return new DefaultResponseErrorHandler() { 108 private final Logger logger = LoggerFactory.getLogger(ResponseErrorHandler.class); 111 public void handleError(ClientHttpResponse clientHttpResponse) throws IOException { 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());