Search in sources :

Example 1 with Topology

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());
}
Also used : Topology(org.openkilda.topo.Topology) Test(org.junit.Test)

Example 2 with Topology

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Topology(org.openkilda.topo.Topology) Switch(org.openkilda.topo.Switch) TopologyProcessingException(org.openkilda.topo.exceptions.TopologyProcessingException) LinkEndpoint(org.openkilda.topo.LinkEndpoint) HashMap(java.util.HashMap) Map(java.util.Map) Link(org.openkilda.topo.Link) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Topology

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);
}
Also used : Switch(org.openkilda.topo.Switch) HashMap(java.util.HashMap) Topology(org.openkilda.topo.Topology) Link(org.openkilda.topo.Link) LinkEndpoint(org.openkilda.topo.LinkEndpoint)

Example 4 with Topology

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());
}
Also used : Topology(org.openkilda.topo.Topology) Link(org.openkilda.topo.Link) Test(org.junit.Test)

Example 5 with Topology

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);
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Topology(org.openkilda.topo.Topology) Switch(org.openkilda.topo.Switch) TopologyProcessingException(org.openkilda.topo.exceptions.TopologyProcessingException) LinkEndpoint(org.openkilda.topo.LinkEndpoint) NodeDto(org.openkilda.topo.builders.TeTopologyParser.TopologyDto.NodeDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Link(org.openkilda.topo.Link)

Aggregations

Topology (org.openkilda.topo.Topology)5 Link (org.openkilda.topo.Link)4 HashMap (java.util.HashMap)3 LinkEndpoint (org.openkilda.topo.LinkEndpoint)3 Switch (org.openkilda.topo.Switch)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 TopologyProcessingException (org.openkilda.topo.exceptions.TopologyProcessingException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 NodeDto (org.openkilda.topo.builders.TeTopologyParser.TopologyDto.NodeDto)1