use of org.openkilda.topo.LinkEndpoint 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.LinkEndpoint in project open-kilda by telstra.
the class TestTopologyBuilder method linkSwitches.
private static final void linkSwitches(Map<String, Link> links, Switch s1, Switch s2) {
Port p1 = new Port(s1, String.format("PORTAA%03d", 1));
Port p2 = new Port(s2, String.format("PORTBB%03d", 1));
;
PortQueue q1 = new PortQueue(p1, String.format("QUEUE%03d", 1));
PortQueue q2 = new PortQueue(p2, String.format("QUEUE%03d", 1));
;
LinkEndpoint e1 = new LinkEndpoint(q1);
LinkEndpoint e2 = new LinkEndpoint(q2);
Link link1 = new Link(e1, e2);
Link link2 = new Link(e2, e1);
links.put(link1.getShortSlug(), link1);
links.put(link2.getShortSlug(), link2);
}
use of org.openkilda.topo.LinkEndpoint 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