Open Kilda Java Documentation
LinksUtils.java
Go to the documentation of this file.
1 package org.openkilda;
2 
3 import static java.lang.String.format;
4 import static java.util.Base64.getEncoder;
11 
15 
16 import com.fasterxml.jackson.core.type.TypeReference;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import org.glassfish.jersey.client.ClientConfig;
19 
20 import java.io.IOException;
21 import java.util.List;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.HttpHeaders;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 
29 public final class LinksUtils {
30 
31  private static final String auth = topologyUsername + ":" + topologyPassword;
32  private static final String authHeaderValue = "Basic " + getEncoder().encodeToString(auth.getBytes());
33 
34  private LinksUtils() {
35  }
36 
42  public static List<LinkDto> dumpLinks() {
43  System.out.println("\n==> Northbound Dump Links");
44 
45  long current = System.currentTimeMillis();
46  Client client = ClientBuilder.newClient(new ClientConfig());
47 
48  Response response = client
49  .target(northboundEndpoint)
50  .path("/api/v1/links")
51  .request(MediaType.APPLICATION_JSON)
52  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
53  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
54  .get();
55 
56  System.out.println(String.format("===> Response = %s", response.toString()));
57  System.out.println(String.format("===> Topology-Engine Dump Links Time: %,.3f", getTimeDuration(current)));
58 
59  try {
60  List<LinkDto> links = new ObjectMapper().readValue(
61  response.readEntity(String.class), new TypeReference<List<LinkDto>>() {
62  });
63 
64  //LOGGER.debug(String.format("====> Data = %s", links));
65  return links;
66 
67  } catch (IOException ex) {
68  throw new TopologyProcessingException(format("Unable to parse the links '%s'.", response.toString()), ex);
69  }
70  }
71 
75  public static boolean islFail(String switchName, String portNo) {
76  System.out.println("\n==> Set ISL Discovery Failed");
77 
78  long current = System.currentTimeMillis();
79  Client client = ClientBuilder.newClient(new ClientConfig());
80  Response result = client
81  .target(trafficEndpoint)
82  .path("/cutlink")
83  .queryParam("switch", switchName)
84  .queryParam("port", portNo)
85  .request()
86  .post(Entity.json(""));
87 
88  System.out.println(String.format("===> Response = %s", result.toString()));
89  System.out.println(String.format("===> Set ISL Discovery Failed Time: %,.3f", getTimeDuration(current)));
90 
91  return result.getStatus() == 200;
92  }
93 
97  public static boolean addLink(String srcSwitchName, String destSwitchName) {
98  System.out.println("\n==> Adding link");
99 
100  Client client = ClientBuilder.newClient(new ClientConfig());
101  Response result = client
102  .target(mininetEndpoint)
103  .path("/links")
104  .request(MediaType.APPLICATION_JSON)
105  .post(Entity.json(
106  String.format("{\"links\":[{\"node1\": \"%s\", \"node2\": \"%s\"}]}",
107  srcSwitchName, destSwitchName)));
108 
109  return result.getStatus() == 200;
110  }
111 
112 }
list result
Definition: plan-d.py:72
static final String CORRELATION_ID
Definition: Utils.java:43
static final String northboundEndpoint
static double getTimeDuration(final long current)
Definition: FlowUtils.java:605