Open Kilda Java Documentation
TopologyPrinter.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 
18 import com.fasterxml.jackson.core.JsonFactory;
19 import com.fasterxml.jackson.core.JsonGenerator;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.SerializationFeature;
23 
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.util.TreeSet;
27 
31 public class TopologyPrinter {
32 
36  public static final String toJson(ITopology topo, boolean pretty) throws IOException {
37  ObjectMapper mapper = new ObjectMapper();
38  if (pretty)
39  mapper.enable(SerializationFeature.INDENT_OUTPUT);
40 
41  StringWriter sw = new StringWriter();
42  JsonFactory f = mapper.getFactory();
43 
44  try(JsonGenerator g = f.createGenerator(sw)) {
45  g.writeStartObject();
46  // use TreeSet to sort the list
47  g.writeObjectField("switches", new TreeSet<String>(topo.getSwitches().keySet()));
48  g.writeObjectField("links", new TreeSet<String>(topo.getLinks().keySet()));
49  g.writeEndObject();
50  }
51 
52  return sw.toString();
53  }
54 
55  public static final String toMininetJson(ITopology topo) throws IOException {
56  return toMininetJson(topo, "kilda", "floodlight", "6653");
57  }
58 
59 
63  public static final String toMininetJson(ITopology topo, String host, String name, String port) {
64  StringBuilder sb = new StringBuilder(80);
65  sb.append("{").append(makeControllerSection(host,name,port));
66  sb.append(",").append(makeSwitchSection(topo));
67  sb.append(",").append(makeLinkSection(topo));
68  return sb.append("}").toString();
69  }
70 
71  public static final String makeControllerSection(String host, String name, String port) {
72  StringBuilder sb = new StringBuilder(80);
73  sb.append("\"controllers\": [{\n");
74  sb.append("\"host\": \"" + host + "\",\n");
75  sb.append("\"name\": \"" + name + "\",\n");
76  sb.append("\"port\": " + port);
77  sb.append("}]\n");
78  return sb.toString();
79  }
80 
81  public static final String makeSwitchSection(ITopology topo) {
82  StringBuilder sb = new StringBuilder(80);
83  sb.append("\"switches\": [\n");
84  String separator = "";
85  for (String id : topo.getSwitches().keySet()) {
86  sb.append(separator);
87  sb.append("{\"dpid\": \"" + id + "\",");
88  sb.append("\"name\": \"" + convertSwitchId2Name(id) + "\"}\n");
89  separator = ", ";
90  }
91  sb.append("]");
92  return sb.toString();
93  }
94 
99  public static final String convertSwitchId2Name(String id){
100  return id.replace(":","").substring(8);
101  }
102 
103  public static final String makeLinkSection(ITopology topo) {
104  StringBuilder sb = new StringBuilder(80);
105  sb.append("\"links\": [\n");
106  String separator = "";
107  for (Link link : topo.getLinks().values()) {
108  sb.append(separator);
109  // NB .. the code in mininet_rest uses the switch name, not id
110  sb.append("{\"node1\": \"" +
111  convertSwitchId2Name(link.getSrc().getTopoSwitch().getId()) + "\",");
112  sb.append( "\"node2\": \"" +
113  convertSwitchId2Name(link.getDst().getTopoSwitch().getId()) + "\"}\n");
114  separator = ", ";
115  }
116  sb.append("]");
117  return sb.toString();
118  }
119 
120  public static void main(String[] args) throws IOException {
122  System.out.println("t = " + t);
123 
124  System.out.println("mininet json = \n" + TopologyPrinter.toMininetJson(t));
125  }
126 
127 }
static final String convertSwitchId2Name(String id)
static final String toJson(ITopology topo, boolean pretty)
static final String toMininetJson(ITopology topo)
name
Definition: setup.py:24
static final String makeControllerSection(String host, String name, String port)
static final Topology buildLinearTopo(int numSwitches)
static final String makeSwitchSection(ITopology topo)
static final String toMininetJson(ITopology topo, String host, String name, String port)
topo
Definition: plan-b.py:45
static final String makeLinkSection(ITopology topo)
static void main(String[] args)