use of org.openkilda.topo.Topology in project open-kilda by telstra.
the class TeTopologyParserTest method shouldParseJsonWithoutRelations.
@Test
public void shouldParseJsonWithoutRelations() {
Topology result = TeTopologyParser.parseTopologyEngineJson("{\"nodes\":[" + "{\"name\":\"sw1\"}, " + "{\"name\":\"sw2\"}" + "]}");
assertEquals("Expected 2 switches to be parsed", 2, result.getSwitches().size());
assertEquals("Expected 0 link to be parsed", 0, result.getLinks().size());
}
use of org.openkilda.topo.Topology in project open-kilda by telstra.
the class TestTopologyBuilder method buildTopoFromTestJson.
/**
* This implements the ability to translate a test topology into a Topology object
*
* @param jsonDoc A JSON doc that matches the syntax of the json files in test/resources/topology
* @return The topology represented in the text file.
*/
@SuppressWarnings("unchecked")
public static final Topology buildTopoFromTestJson(String jsonDoc) {
Map<String, Switch> switches = new HashMap<>();
Map<String, Switch> altSwitchId = new HashMap<>();
Map<String, Link> links = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
Map<String, ArrayList<Map<String, String>>> root;
try {
root = mapper.readValue(jsonDoc, Map.class);
} catch (IOException ex) {
throw new TopologyProcessingException(format("Unable to parse the topology '%s'.", jsonDoc), ex);
}
// populate switches first
ArrayList<Map<String, String>> jsonSwitches = root.get("switches");
for (Map<String, String> s : jsonSwitches) {
String id = normalSwitchID(s.get("dpid"));
Switch newSwitch = new Switch(id);
switches.put(id, newSwitch);
if (s.get("name") != null)
altSwitchId.put(s.get("name"), newSwitch);
}
// now populate links
ArrayList<Map<String, String>> jsonLinks = root.get("links");
for (Map<String, String> l : jsonLinks) {
String srcId = l.get("node1");
String dstId = l.get("node2");
Switch src = switches.get(srcId);
if (src == null)
src = altSwitchId.get(srcId);
Switch dst = switches.get(dstId);
if (dst == null)
dst = altSwitchId.get(dstId);
Link link = new Link(new LinkEndpoint(src, null, null), new LinkEndpoint(dst, null, null));
links.put(link.getShortSlug(), link);
}
return new Topology(switches, links);
}
use of org.openkilda.topo.Topology in project open-kilda by telstra.
the class TestTopologyBuilder method buildLinearTopo.
/**
* buildLinearTopo models the Linear topology from Mininet
*
* Also, use names / ids similar to how mininet uses them
*/
public static final Topology buildLinearTopo(int numSwitches) {
// Add Switches
Map<String, Switch> switches = new HashMap<>();
Map<String, Link> links = new HashMap<>();
for (int i = 0; i < numSwitches; i++) {
String switchID = intToSwitchId(i + 1);
switches.put(switchID, new Switch(switchID));
}
// Add links between switches
// is A-->B = 2 switches, 1 link.
int numLinks = numSwitches - 1;
for (int i = 0; i < numLinks; i++) {
Switch s1 = switches.get(intToSwitchId(i + 1));
Switch s2 = switches.get(intToSwitchId(i + 2));
linkSwitches(links, s1, s2);
}
return new Topology(switches, links);
}
use of org.openkilda.topo.Topology in project open-kilda by telstra.
the class TeTopologyParserTest method shouldParseJson.
@Test
public void shouldParseJson() {
Topology result = TeTopologyParser.parseTopologyEngineJson("{\"nodes\":[" + "{\"name\":\"sw1\", \"outgoing_relationships\": [\"sw2\"]}, " + "{\"name\":\"sw2\", \"outgoing_relationships\": []}" + "]}");
assertEquals("Expected 2 switches to be parsed", 2, result.getSwitches().size());
assertEquals("Expected 1 link to be parsed", 1, result.getLinks().size());
assertTrue("Expected 'sw1' switch to be parsed", result.getSwitches().containsKey("SW1"));
assertTrue("Expected 'sw2' switch to be parsed", result.getSwitches().containsKey("SW2"));
Link link = result.getLinks().values().iterator().next();
assertEquals("Expected a link from sw1 to be parsed", "SW1", link.getSrc().getTopoSwitch().getId());
assertEquals("Expected a link to sw2 to be parsed", "SW2", link.getDst().getTopoSwitch().getId());
}
use of org.openkilda.topo.Topology in project open-kilda by telstra.
the class TeTopologyParser method parseTopologyEngineJson.
public static Topology parseTopologyEngineJson(String json) {
TopologyDto topologyDto;
ObjectMapper mapper = new ObjectMapper();
try {
topologyDto = mapper.readValue(json, TopologyDto.class);
} catch (IOException ex) {
throw new TopologyProcessingException(format("Unable to parse the topology '%s'.", json), ex);
}
Map<String, Switch> switches = new HashMap<>();
// Assemble the switches from the provided nodes.
topologyDto.getNodes().forEach(node -> {
String name = node.getName();
if (Strings.isNullOrEmpty(name)) {
throw new TopologyProcessingException("The node must have a name.");
}
Objects.requireNonNull(name, "The name must be provided");
String switchId = name.toUpperCase();
switches.put(switchId, new Switch(switchId));
});
Map<String, Link> links = new HashMap<>();
// Assemble the links from the provided outgoing_relationships.
topologyDto.getNodes().forEach(node -> {
String srcId = node.getName().toUpperCase();
List<NodeDto> relations = node.getOutgoingRelationships();
if (relations != null) {
relations.forEach(relation -> {
String dstId = relation.getName().toUpperCase();
Link link = new Link(// TODO: probably reuse the same endpoint. Why not?
new LinkEndpoint(switches.get(srcId), null, null), new LinkEndpoint(switches.get(dstId), null, null));
links.put(link.getShortSlug(), link);
});
}
});
return new Topology(switches, links);
}
Aggregations