Open Kilda Java Documentation
ControllerUtils.java
Go to the documentation of this file.
1 package org.openkilda.atdd.utils.controller;
2 
3 import static org.awaitility.Awaitility.await;
4 
7 
8 import com.fasterxml.jackson.core.JsonProcessingException;
9 import com.fasterxml.jackson.core.type.TypeReference;
10 import com.spotify.docker.client.exceptions.DockerCertificateException;
11 import com.spotify.docker.client.exceptions.DockerException;
12 import org.glassfish.jersey.client.ClientConfig;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 
16 import java.io.IOException;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.TimeUnit;
20 import javax.ws.rs.ProcessingException;
21 import javax.ws.rs.client.Client;
22 import javax.ws.rs.client.ClientBuilder;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 
27 public class ControllerUtils {
28  private static final Logger LOGGER = LoggerFactory.getLogger(ControllerUtils.class);
29  private final Client restClient;
30 
31  public ControllerUtils() throws DockerCertificateException, DockerException, InterruptedException {
32  restClient = ClientBuilder.newClient(new ClientConfig());
33  }
34 
35  public void restart() throws DockerException, InterruptedException {
36  await().atMost(10, TimeUnit.SECONDS)
37  .until(this::isAlive);
38  }
39 
40  public void addStaticFlow(StaticFlowEntry flow) throws JsonProcessingException, FloodlightQueryException {
41  Response response;
42  String json = Utils.MAPPER.writeValueAsString(flow);
43  try {
44  response = restClient.target(DefaultParameters.FLOODLIGHT_ENDPOINT)
45  .path("/wm/staticentrypusher/json")
46  .request()
47  .accept(MediaType.APPLICATION_JSON)
48  .post(Entity.json(json));
49  } catch (ProcessingException e) {
50  throw new FloodlightQueryException(e);
51  }
52 
53  if (response.getStatus() != 200) {
54  throw new FloodlightQueryException(201, response.getStatus());
55  }
56  }
57 
59  Response response;
60  try {
61  response = restClient.target(DefaultParameters.FLOODLIGHT_ENDPOINT)
62  .path(String.format("/wm/staticentrypusher/list/%s/json", dpId))
63  .request()
64  .accept(MediaType.APPLICATION_JSON)
65  .get();
66  } catch (ProcessingException e) {
67  throw new FloodlightQueryException(e);
68  }
69 
70  if (response.getStatus() != 200) {
71  throw new FloodlightQueryException(200, response.getStatus());
72  }
73 
74  String json = response.readEntity(String.class);
75  try {
76  return Utils.MAPPER.readValue(json, DpIdEntriesList.class);
77  } catch (IOException e) {
78  throw new FloodlightQueryException("Can't parse FloodLight response", e);
79  }
80  }
81 
83  return this.listStaticEntries("all");
84  }
85 
86  public List<CoreFlowEntry> listCoreFlows(String dpId) throws FloodlightQueryException, DpIdNotFoundException {
87  Response response;
88  try {
89  response = restClient.target(DefaultParameters.FLOODLIGHT_ENDPOINT)
90  .path(String.format("/wm/core/switch/%s/flow/json", dpId))
91  .request()
92  .accept(MediaType.APPLICATION_JSON)
93  .get();
94  } catch (ProcessingException e) {
95  throw new FloodlightQueryException(e);
96  }
97 
98  if (response.getStatus() != 200) {
99  throw new FloodlightQueryException(200, response.getStatus());
100  }
101 
102  final String flowsKey = "flows";
103  String json = response.readEntity(String.class);
104  LOGGER.debug("FloodLight switch \"{}\" list flows response: {}", dpId, json);
105  try {
106  Map<String, Object> keyChecker = Utils.MAPPER.readValue(json, new TypeReference<Map<String, Object>>() {
107  });
108  if (!keyChecker.containsKey(flowsKey)) {
109  throw new DpIdNotFoundException();
110  }
111 
112  Map<String, List<CoreFlowEntry>> wrapper = Utils.MAPPER.readValue(
113  json, new TypeReference<Map<String, List<CoreFlowEntry>>>() {
114  });
115  return wrapper.get(flowsKey);
116  } catch (IOException e) {
117  throw new FloodlightQueryException("Can't parse FloodLight response", e);
118  }
119  }
120 
121  public boolean isAlive() {
122  Response response;
123  try {
124  response = restClient.target(DefaultParameters.FLOODLIGHT_ENDPOINT)
125  .path("/wm/core/controller/summary/json")
126  .request()
127  .get();
128  } catch (ProcessingException e) {
129  LOGGER.info("floodlight is unavailable");
130  return false;
131  }
132 
133  boolean alive = response != null;
134  if (alive) {
135  LOGGER.info("floodlight available");
136  }
137  return alive;
138  }
139 
143  public SwitchEntry getSwitchPorts(String switchId) throws FloodlightQueryException {
144  Response response;
145  try {
146  response = restClient.target(DefaultParameters.FLOODLIGHT_ENDPOINT)
147  .path(String.format("wm/core/switch/%s/port-desc/json", switchId))
148  .request()
149  .accept(MediaType.APPLICATION_JSON)
150  .get();
151  } catch (ProcessingException e) {
152  throw new FloodlightQueryException(e);
153  }
154 
155  if (response.getStatus() != 200) {
156  throw new FloodlightQueryException(200, response.getStatus());
157  }
158 
159  String json = response.readEntity(String.class);
160  try {
161  return Utils.MAPPER.readValue(json, SwitchEntry.class);
162  } catch (IOException e) {
163  throw new FloodlightQueryException("Can't parse FloodLight response", e);
164  }
165  }
166 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
List< CoreFlowEntry > listCoreFlows(String dpId)
static final String FLOODLIGHT_ENDPOINT