16 package org.openkilda.topo.builders;
18 import static java.lang.String.format;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.google.common.primitives.Ints;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
45 @SuppressWarnings(
"unchecked")
47 Map<String, Switch> switches =
new HashMap<>();
48 Map<String, Switch> altSwitchId =
new HashMap<>();
49 Map<String, Link> links =
new HashMap<>();
51 ObjectMapper mapper =
new ObjectMapper();
52 Map<String,ArrayList<Map<String,String>>>
root;
55 root = mapper.readValue(jsonDoc, Map.class);
56 }
catch (IOException ex) {
61 ArrayList<Map<String,String>> jsonSwitches =
root.get(
"switches");
62 for (Map<String,String> s : jsonSwitches) {
63 String
id = normalSwitchID(s.get(
"dpid"));
65 switches.put(
id, newSwitch);
66 if (s.get(
"name") != null)
67 altSwitchId.put(s.get(
"name"),newSwitch);
71 ArrayList<Map<String,String>> jsonLinks =
root.get(
"links");
72 for (Map<String,String> l : jsonLinks) {
73 String srcId = l.get(
"node1");
74 String dstId = l.get(
"node2");
75 Switch src = switches.get(srcId);
76 if (src == null) src = altSwitchId.get(srcId);
77 Switch dst = switches.get(dstId);
78 if (dst == null) dst = altSwitchId.get(dstId);
87 return new Topology(switches, links);
94 private static final String normalSwitchID(String
id){
98 if (
id.contains(
":") ==
false){
99 StringBuilder sb =
new StringBuilder(
id.substring(0,2));
100 for (
int i = 2;
i < 16;
i+=2) {
101 sb.append(
":").append(
id.substring(
i,
i+2));
105 id =
id.toLowerCase();
116 Map<String, Switch> switches =
new HashMap<>();
117 Map<String, Link> links =
new HashMap<>();
119 for (
int i = 0;
i < numSwitches;
i++) {
121 switches.put(switchID,
new Switch(switchID));
125 int numLinks = numSwitches-1;
126 for (
int i = 0;
i < numLinks;
i++) {
129 linkSwitches(links,
s1,
s2);
131 return new Topology(switches, links);
135 byte[] ib = Ints.toByteArray(
i);
136 return String.format(
"DE:AD:BE:EF:%02x:%02x:%02x:%02x",ib[0],ib[1],ib[2],ib[3]);
139 private static final void linkSwitches(Map<String, Link> links,
Switch s1,
Switch s2){
140 Port p1 =
new Port(
s1,String.format(
"PORTAA%03d",1));
141 Port p2 =
new Port(
s2,String.format(
"PORTBB%03d",1));;
156 TreeBuilder tb =
new TreeBuilder(fanout);
157 return tb.build(depth);
163 throw new UnsupportedOperationException();
170 static class TreeBuilder {
171 private int switchId = 1;
174 private Map<String, Switch> switches =
new HashMap<>();
175 private Map<String, Link> links =
new HashMap<>();
177 TreeBuilder(
int fanout){
178 this.fanout = fanout;
184 return new Topology(switches, links);
188 private Switch buildSwitch(
int depth){
190 Switch
s1 =
new Switch(switchID);
191 switches.put(switchID,
s1);
194 for (
int i = 0;
i < fanout;
i++) {
195 Switch
s2 = buildSwitch(depth - 1);
196 linkSwitches(links,
s1,
s2);
static final Topology buildLinearTopo(int numSwitches)
static final Topology buildTorusTopo()
static final Topology buildTopoFromTestJson(String jsonDoc)
static final Topology buildTreeTopo(int depth, int fanout)
static String intToSwitchId(int i)