Open Kilda Java Documentation
TopologyHelp.java
Go to the documentation of this file.
1 /* Copyright 2017 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.topo;
17 
21 
25 
26 import org.glassfish.jersey.client.ClientConfig;
27 
28 import java.io.IOException;
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.ClientBuilder;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.GenericType;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 
39 public class TopologyHelp {
40  public static boolean DeleteMininetTopology() {
41  System.out.println("\n==> Delete Mininet Topology");
42 
43  long current = System.currentTimeMillis();
44  Client client = ClientBuilder.newClient(new ClientConfig());
45  Response result = client
46  .target(mininetEndpoint)
47  .path("/cleanup")
48  .request(MediaType.APPLICATION_JSON)
49  .post(null);
50 
51  System.out.println(String.format("===> Response = %s", result.toString()));
52  System.out.println(String.format("===> Delete Mininet Topology Time: %,.3f", getTimeDuration(current)));
53 
54  return result.getStatus() == 200;
55  }
56 
62  public static boolean CreateMininetTopology(String json) {
63  System.out.println("\n==> Create Mininet Topology");
64 
65  long current = System.currentTimeMillis();
66  Client client = ClientBuilder.newClient(new ClientConfig());
67  Response result = client
68  .target(mininetEndpoint)
69  .path("/topology")
70  .request(MediaType.APPLICATION_JSON)
71  .post(Entity.entity(json, MediaType.APPLICATION_JSON));
72 
73  System.out.println(String.format("===> Response = %s", result.toString()));
74  System.out.println(String.format("===> Create Mininet Topology Time: %,.3f", getTimeDuration(current)));
75 
76  return result.getStatus() == 200;
77  }
78 
79  public static boolean TestMininetCreate(String json) {
80  System.out.println("\n==> Create Mininet Random Topology");
81 
82  //
83  // TODO: mininet_rest has been re-written, but not create_random_linear_topology.
84  // It is unclear if this code path is still used ATM (could be useful for scale
85  // testing, but possibly not in its current form.
86  //
87 
88  boolean refactored = false;
89  if (!refactored)
90  throw new UnsupportedOperationException("This code needs refactoring");
91 
92  long current = System.currentTimeMillis();
93  Client client = ClientBuilder.newClient(new ClientConfig());
94  Response result = client
95  .target(mininetEndpoint)
96  .path("/create_random_linear_topology")
97  .request(MediaType.APPLICATION_JSON)
98  .post(Entity.entity(json, MediaType.APPLICATION_JSON));
99 
100  System.out.println(String.format("===> Response = %s", result.toString()));
101  System.out.println(String.format("===> Create Mininet Random Topology Time: %,.3f", getTimeDuration(current)));
102 
103  return result.getStatus() == 200;
104  }
105 
111  public static String GetTopology() {
112  System.out.println("\n==> Get Topology-Engine Topology");
113 
114  long current = System.currentTimeMillis();
115  Client client = ClientBuilder.newClient(new ClientConfig());
116  Response response = client
117  .target(topologyEndpoint)
118  .path("/api/v1/topology/network")
119  .request()
120  .get();
121 
122  System.out.println(String.format("===> Response = %s", response.toString()));
123  System.out.println(String.format("===> Get Topology-Engine Topology Time: %,.3f", getTimeDuration(current)));
124  String result = response.readEntity(String.class);
125  System.out.println(String.format("====> Topology-Engine Topology = %s", result));
126 
127  return result;
128  }
129 
135  public static String ClearTopology() {
136  System.out.println("\n==> Clear Topology-Engine Topology");
137 
138  long current = System.currentTimeMillis();
139  Client client = ClientBuilder.newClient(new ClientConfig());
140  Response response = client
141  .target(topologyEndpoint)
142  .path("/api/v1/topology/clear")
143  .request()
144  .get();
145 
146  System.out.println(String.format("===> Response = %s", response.toString()));
147  System.out.println(String.format("===> Clear Topology-Engine Topology Time: %,.3f", getTimeDuration(current)));
148  String result = response.readEntity(String.class);
149  System.out.println(String.format("====> Topology-Engine Topology = %s", result));
150  return result;
151  }
152 
153  public static ImmutablePair<Flow, Flow> GetFlow(String flowId) {
154  System.out.println("\n==> Topology-Engine Get Flow");
155 
156  Client client = ClientBuilder.newClient(new ClientConfig());
157  Response response = client
158  .target(topologyEndpoint)
159  .path("/api/v1/topology/flows/")
160  .path(flowId)
161  .request(MediaType.APPLICATION_JSON)
162  .get();
163 
164  int status = response.getStatus();
165  if (status != 200) {
166  System.out.println(String.format("====> Error: Topology-Engine Get Flow = %s",
167  response.readEntity(MessageError.class)));
168  return null;
169  }
170 
171  ImmutablePair<Flow, Flow> result = response.readEntity(new GenericType<ImmutablePair<Flow, Flow>>() {});
172  System.out.println(String.format("====> Topology-Engine Get Flow = %s", result));
173  return result;
174  }
175 
176  // FIXME(surabujin): garbage
177  public static void main(String[] args) throws IOException {
178  //TopologyHelp.DeleteTopology();
179  //URL url = Resources.getResource("topologies/partial-topology.json");
180  //String doc = Resources.toString(url, Charsets.UTF_8);
181  //TopologyHelp.CreateTopology(doc);
182 
183  System.out.println("GetTopology(): = " + TopologyHelp.GetTopology());
184  System.out.println("ClearTopology(): = " + TopologyHelp.ClearTopology());
185  }
186 }
static boolean TestMininetCreate(String json)
def status()
Definition: rest.py:593
static void main(String[] args)
static boolean CreateMininetTopology(String json)
list result
Definition: plan-d.py:72
static boolean DeleteMininetTopology()
static double getTimeDuration(final long current)
Definition: FlowUtils.java:605
static ImmutablePair< Flow, Flow > GetFlow(String flowId)