Open Kilda Java Documentation
ASwitchServiceImpl.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.service.aswitch;
17 
19 
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Qualifier;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.MediaType;
28 import org.springframework.stereotype.Service;
29 import org.springframework.web.client.RestTemplate;
30 
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 
39 @Service
40 public class ASwitchServiceImpl implements ASwitchService {
41 
42  private static final Logger LOGGER = LoggerFactory.getLogger(ASwitchServiceImpl.class);
43 
44  @Autowired
45  @Qualifier("aSwitchRestTemplate")
46  private RestTemplate restTemplate;
47 
48  @Override
49  public void addFlows(List<ASwitchFlow> flows) {
50  restTemplate.exchange("/flows", HttpMethod.POST,
51  new HttpEntity<>(flows, buildJsonHeaders()), String.class);
52  LOGGER.info("Added flows: {}", flows.stream()
53  .map(flow -> String.format("%s->%s", flow.getInPort(), flow.getOutPort()))
54  .collect(Collectors.toList()));
55  }
56 
57  @Override
58  public void removeFlows(List<ASwitchFlow> flows) {
59  restTemplate.exchange("/flows", HttpMethod.DELETE,
60  new HttpEntity<>(flows, buildJsonHeaders()), String.class);
61  LOGGER.info("Removed flows: {}", flows.stream()
62  .map(flow -> String.format("%s->%s", flow.getInPort(), flow.getOutPort()))
63  .collect(Collectors.toList()));
64  }
65 
66  @Override
67  public List<ASwitchFlow> getAllFlows() {
68  ASwitchFlow[] flows = restTemplate.exchange("/flows", HttpMethod.GET,
69  new HttpEntity(buildJsonHeaders()), ASwitchFlow[].class).getBody();
70  return Arrays.asList(flows);
71  }
72 
73  @Override
74  public void portsUp(List<Integer> ports) {
75  restTemplate.exchange("/ports", HttpMethod.POST,
76  new HttpEntity<>(ports, buildJsonHeaders()), String.class);
77  LOGGER.info("Brought up ports: {}", ports);
78  }
79 
80  @Override
81  public void portsDown(List<Integer> ports) {
82  restTemplate.exchange("/ports", HttpMethod.DELETE,
83  new HttpEntity<>(ports, buildJsonHeaders()), String.class);
84  LOGGER.info("Brought down ports: {}", ports);
85  }
86 
87  private HttpHeaders buildJsonHeaders() {
88  HttpHeaders headers = new HttpHeaders();
89  headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
90  return headers;
91  }
92 }