Search in sources :

Example 1 with LinkEndpoint

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);
}
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 2 with LinkEndpoint

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);
}
Also used : Port(org.openkilda.topo.Port) PortQueue(org.openkilda.topo.PortQueue) LinkEndpoint(org.openkilda.topo.LinkEndpoint) Link(org.openkilda.topo.Link)

Example 3 with LinkEndpoint

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);
}
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

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