Search in sources :

Example 1 with Link

use of nl.uva.cs.lobcder.rest.wrappers.Link in project lobcder by skoulouzis.

the class SDNSweep method pushARPFlow.

private void pushARPFlow() throws InterruptedException, IOException, ParserConfigurationException, SAXException {
    Iterator<Switch> swIter = getAllSwitches().iterator();
    List<String> arpFlows = new ArrayList<>();
    while (swIter.hasNext()) {
        Switch sw = swIter.next();
        String flow = "{\"switch\":\"" + sw.dpid + "\", " + "\"name\":\"arp" + sw.dpid + "\", " + "\"ether-type\":\"0x0806\", " + "\"actions\":\"output=flood\"}";
        arpFlows.add(flow);
        Collection<NetworkEntity> nes = getNetworkEntitiesForSwDPI(sw.dpid);
        Iterator<NetworkEntity> neIter = nes.iterator();
        while (neIter.hasNext()) {
            NetworkEntity ne = neIter.next();
            for (String mac : ne.getMac()) {
                for (AttachmentPoint ap : ne.getAttachmentPoint()) {
                    flow = "{\"switch\": \"" + sw.dpid + "\", " + "\"name\":\"flow-to-" + mac + "\", " + "\"cookie\":\"0\", " + "\"priority\":\"32768\", " + "\"dst-mac\":\"" + mac + "\"," + "\"active\":\"true\", " + "\"actions\":" + "\"output=" + ap.getPort() + "\"}";
                    arpFlows.add(flow);
                }
            }
        }
    }
    List<Link> links = getAllSwitchLinks();
    StringBuilder sb = new StringBuilder();
    for (Link l : links) {
        Collection<NetworkEntity> nes = getNetworkEntitiesForSwDPI(l.dstSwitch);
        for (NetworkEntity neDst : nes) {
            for (String mac : neDst.getMac()) {
                sb.append("{\"switch\": \"").append(l.srcSwitch).append("\"," + "\"name\":\"").append(l.srcSwitch).append("-to-").append(mac).append("\", " + "\"cookie\":\"0\", " + "\"priority\":\"32768\", " + "\"dst-mac\":\"").append(mac).append("\"," + "\"active\":\"true\"," + "\"actions\":\"output=").append(l.srcPort).append("\"}\"");
                arpFlows.add(sb.toString());
                // Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, sb.toString());
                sb.setLength(0);
            }
        }
        nes = getNetworkEntitiesForSwDPI(l.srcSwitch);
        for (NetworkEntity neDst : nes) {
            for (String mac : neDst.getMac()) {
                sb.append("{\"switch\": \"").append(l.dstSwitch).append("\"," + "\"name\":\"").append(l.dstSwitch).append("-to-").append(mac).append("\", " + "\"cookie\":\"0\", " + "\"priority\":\"32768\", " + "\"dst-mac\":\"").append(mac).append("\"," + "\"active\":\"true\"," + "\"actions\":\"output=").append(l.dstPort).append("\"}\"");
                arpFlows.add(sb.toString());
                // Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, sb.toString());
                sb.setLength(0);
            }
        }
    }
    pushFlows(arpFlows);
}
Also used : NetworkEntity(nl.uva.cs.lobcder.rest.wrappers.NetworkEntity) ArrayList(java.util.ArrayList) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) Link(nl.uva.cs.lobcder.rest.wrappers.Link)

Example 2 with Link

use of nl.uva.cs.lobcder.rest.wrappers.Link in project lobcder by skoulouzis.

the class SDNSweep method fillInLinkGaps.

private ArrayList<Link> fillInLinkGaps(List<Link> switchLinks) throws IOException, ParserConfigurationException, SAXException {
    ArrayList<Link> possibleLinks = new ArrayList<>();
    for (Switch srcSW : getAllSwitches()) {
        String srcSWIP = srcSW.inetAddress.replaceAll("/", "").substring(0, srcSW.inetAddress.indexOf(":") - 1);
        int srcPort = 0;
        for (NetworkEntity ne : getNetworkEntitiesForSwDPI(srcSW.dpid)) {
            for (AttachmentPoint ap : ne.getAttachmentPoint()) {
                for (Port p : srcSW.ports) {
                    for (Link existingLink : switchLinks) {
                        if (ap.getPort() != (Number) p.portNumber && p.portNumber != existingLink.srcPort && ap.getPort().intValue() <= 500 && p.portNumber <= 500) {
                            srcPort = p.portNumber;
                            break;
                        }
                    }
                }
            }
        }
        int dstPort;
        Link l = null;
        for (String dstSWIP : findConnectedSwitches(srcSWIP)) {
            Switch dstSW = getSwitches().get(dstSWIP);
            for (NetworkEntity ne : getNetworkEntitiesForSwDPI(dstSW.dpid)) {
                for (AttachmentPoint ap : ne.getAttachmentPoint()) {
                    for (Port p : dstSW.ports) {
                        for (Link existingLink : switchLinks) {
                            if (ap.getPort() != (Number) p.portNumber && existingLink.dstPort != p.portNumber && ap.getPort().intValue() <= 500 && p.portNumber <= 500 && srcPort <= 500) {
                                dstPort = p.portNumber;
                                l = new Link();
                                l.srcSwitch = srcSW.dpid;
                                l.srcPort = srcPort;
                                l.dstSwitch = dstSW.dpid;
                                l.dstPort = dstPort;
                                possibleLinks.add(l);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    for (Link possible : possibleLinks) {
        String possibleSRC = possible.srcSwitch + "-" + possible.srcPort;
        String possibleDST = possible.dstSwitch + "-" + possible.dstPort;
        for (Link existing : switchLinks) {
            String existingSRC = existing.srcSwitch + "-" + existing.srcPort;
            String existingDST = existing.dstSwitch + "-" + existing.dstPort;
            if (!possibleSRC.equals(existingSRC) && !possibleDST.equals(existingDST) && !possibleSRC.equals(existingDST) && !possibleDST.equals(existingSRC)) {
                Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, "{0}-{1} -> {2}-{3}", new Object[] { possible.srcSwitch, possible.srcPort, possible.dstSwitch, possible.dstPort });
            }
        }
    }
    ArrayList<Link> addedLinks = new ArrayList<>();
    // }
    return addedLinks;
}
Also used : NetworkEntity(nl.uva.cs.lobcder.rest.wrappers.NetworkEntity) ArrayList(java.util.ArrayList) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) Link(nl.uva.cs.lobcder.rest.wrappers.Link) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint)

Example 3 with Link

use of nl.uva.cs.lobcder.rest.wrappers.Link in project lobcder by skoulouzis.

the class SDNControllerClient method getShortestPath.

public List<DefaultWeightedEdge> getShortestPath(String dest, Set<String> sources) throws InterruptedException, IOException {
    if (graph == null) {
        graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
    }
    Collection<SDNSweep.Switch> switchesColl = SDNSweep.getSwitches().values();
    List<SDNSweep.Switch> switches;
    if (switchesColl instanceof List) {
        switches = (List) switchesColl;
    } else {
        switches = new ArrayList(switchesColl);
    }
    for (int i = 0; i < switches.size(); i++) {
        List<Port> ports = switches.get(i).ports;
        if (ports != null) {
            for (int j = 0; j < ports.size(); j++) {
                for (int k = 0; k < ports.size(); k++) {
                    if (ports.get(j).state == 0 && ports.get(k).state == 0 && j != k) {
                        String vertex1 = switches.get(i).dpid + "-" + ports.get(j).portNumber;
                        String vertex2 = switches.get(i).dpid + "-" + ports.get(k).portNumber;
                        // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "From: {0} to: {1}", new Object[]{vertex1, vertex2});
                        if (!graph.containsVertex(vertex1)) {
                            graph.addVertex(vertex1);
                        }
                        if (!graph.containsVertex(vertex2)) {
                            graph.addVertex(vertex2);
                        }
                        DefaultWeightedEdge e1;
                        if (!graph.containsEdge(vertex1, vertex2)) {
                            e1 = graph.addEdge(vertex1, vertex2);
                        } else {
                            e1 = graph.getEdge(vertex1, vertex2);
                        }
                        graph.setEdgeWeight(e1, 1);
                    }
                }
            }
        }
    }
    // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "Destination: {0}", new Object[]{dest});
    if (!graph.containsVertex(dest)) {
        graph.addVertex(dest);
    }
    NetworkEntity destinationEntityArray = SDNSweep.getNetworkEntity(dest);
    // for (SDNSweep.NetworkEntity ne : destinationEntityArray) {
    if (destinationEntityArray != null) {
        for (AttachmentPoint ap : destinationEntityArray.getAttachmentPoint()) {
            String vertex = ap.getSwitchDPID() + "-" + ap.getPort();
            // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "vertex: {0}", new Object[]{vertex});
            if (!graph.containsVertex(vertex)) {
                graph.addVertex(vertex);
            }
            DefaultWeightedEdge e1;
            if (!graph.containsEdge(dest, vertex)) {
                e1 = graph.addEdge(dest, vertex);
            } else {
                e1 = graph.getEdge(dest, vertex);
            }
            // Don't calculate the cost from the destination to the switch.
            // There is nothing we can do about it so why waste cycles ?
            // graph.setEdgeWeight(e1, 2);
            graph.setEdgeWeight(e1, getCost(dest, vertex));
        }
    }
    // List<NetworkEntity> sourceEntityArray = getNetworkEntity(sources);
    for (String s : sources) {
        NetworkEntity ne = SDNSweep.getNetworkEntity(s);
        if (ne != null) {
            for (String ip : ne.getIpv4()) {
                if (!graph.containsVertex(ip)) {
                    graph.addVertex(ip);
                }
                for (AttachmentPoint ap : ne.getAttachmentPoint()) {
                    String vertex = ap.getSwitchDPID() + "-" + ap.getPort();
                    if (!graph.containsVertex(vertex)) {
                        graph.addVertex(vertex);
                    }
                    DefaultWeightedEdge e2;
                    if (!graph.containsEdge(ip, vertex)) {
                        e2 = graph.addEdge(ip, vertex);
                    } else {
                        e2 = graph.getEdge(ip, vertex);
                    }
                    graph.setEdgeWeight(e2, getCost(ip, vertex));
                }
            }
        }
    }
    List<Link> links = SDNSweep.getSwitchLinks();
    for (Link l : links) {
        String srcVertex = l.srcSwitch + "-" + l.srcPort;
        if (!graph.containsVertex(srcVertex)) {
            graph.addVertex(srcVertex);
        }
        String dstVertex = l.dstSwitch + "-" + l.dstPort;
        if (!graph.containsVertex(dstVertex)) {
            graph.addVertex(dstVertex);
        }
        DefaultWeightedEdge e3;
        if (!graph.containsEdge(srcVertex, dstVertex)) {
            e3 = graph.addEdge(srcVertex, dstVertex);
        } else {
            e3 = graph.getEdge(srcVertex, dstVertex);
        }
        // Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, "dstVertex: {0}", new Object[]{dstVertex});
        graph.setEdgeWeight(e3, getCost(srcVertex, dstVertex));
    }
    double cost = Double.MAX_VALUE;
    List<DefaultWeightedEdge> shortestPath = null;
    // exportGraph();
    StringBuilder msg = new StringBuilder();
    msg.append("\n");
    for (String s : sources) {
        if (graph.containsVertex(dest) && graph.containsVertex(s)) {
            List<DefaultWeightedEdge> shorPath = null;
            shorPath = DijkstraShortestPath.findPathBetween(graph, s, dest);
            double w = 0;
            if (shorPath != null) {
                for (DefaultWeightedEdge e : shorPath) {
                    w += graph.getEdgeWeight(e);
                }
                DefaultWeightedEdge p = shorPath.get(0);
                String e = graph.getEdgeSource(p);
                msg.append("source: ").append(e).append(" cost: ").append(w).append("\n");
                if (w <= cost) {
                    cost = w;
                    shortestPath = shorPath;
                    if (cost <= sources.size() + 1) {
                        break;
                    }
                }
            }
        }
    }
    Logger.getLogger(SDNControllerClient.class.getName()).log(Level.INFO, msg.toString());
    return shortestPath;
}
Also used : Port(nl.uva.cs.lobcder.catalogue.SDNSweep.Port) NetworkEntity(nl.uva.cs.lobcder.rest.wrappers.NetworkEntity) ArrayList(java.util.ArrayList) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) AttachmentPoint(nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint) DefaultWeightedEdge(org.jgrapht.graph.DefaultWeightedEdge) ArrayList(java.util.ArrayList) List(java.util.List) Link(nl.uva.cs.lobcder.rest.wrappers.Link)

Example 4 with Link

use of nl.uva.cs.lobcder.rest.wrappers.Link in project lobcder by skoulouzis.

the class SDNSweep method getAllSwitchLinks.

private List<Link> getAllSwitchLinks() throws IOException, ParserConfigurationException, SAXException {
    if (getSwitchLinks() == null) {
        switchLinks = new ArrayList<>();
    }
    if (getSwitchLinks().isEmpty() || getIterations() % 5 == 0) {
        WebResource webResource = getClient().resource(getUri() + ":" + getFloodlightPort());
        WebResource res = webResource.path("wm").path("topology").path("links").path("json");
        switchLinks = res.get(new GenericType<List<Link>>() {
        });
        res = webResource.path("wm").path("topology").path("external-links").path("json");
        getSwitchLinks().addAll(res.get(new GenericType<List<Link>>() {
        }));
    // ArrayList<Link> addedLinks = fillInLinkGaps(switchLinks);
    // switchLinks.addAll(addedLinks);
    // Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, switchLinks.toString());
    }
    return getSwitchLinks();
}
Also used : GenericType(com.sun.jersey.api.client.GenericType) WebResource(com.sun.jersey.api.client.WebResource) Link(nl.uva.cs.lobcder.rest.wrappers.Link)

Example 5 with Link

use of nl.uva.cs.lobcder.rest.wrappers.Link in project lobcder by skoulouzis.

the class SDNSweep method pushFlowIntoOnePort.

private void pushFlowIntoOnePort() throws IOException, ParserConfigurationException, SAXException {
    // get(0).dpid;
    String s1 = getAllSwitches().iterator().next().dpid;
    // get(1).dpid;
    String s2 = getAllSwitches().iterator().next().dpid;
    Number s1ToS2Port = 0;
    Number s2ToS1Port = 0;
    for (Link l : getAllSwitchLinks()) {
        if (l.srcSwitch.equals(s1)) {
            s1ToS2Port = l.srcPort;
            s2ToS1Port = l.dstPort;
            break;
        } else if (l.srcSwitch.equals(s2)) {
            s1ToS2Port = l.dstPort;
            s2ToS1Port = l.srcPort;
            break;
        }
    }
    List<String> s1Hosts = new ArrayList<>();
    List<String> s2Hosts = new ArrayList<>();
    for (NetworkEntity ne : getAllNetworkEntites()) {
        if (ne.getAttachmentPoint().get(0).getSwitchDPID().equals(s1)) {
            s1Hosts.add(ne.getIpv4().get(0));
        } else if (ne.getAttachmentPoint().get(0).getSwitchDPID().equals(s2)) {
            s2Hosts.add(ne.getIpv4().get(0));
        }
    }
    List<String> rules = new ArrayList<>();
    // s1 to s2
    for (String h1 : s1Hosts) {
        for (String h2 : s2Hosts) {
            String rule1To2 = "{\"switch\": \"" + s1 + "\", \"name\":\"" + h1 + "To" + h2 + "\", \"cookie\":\"0\", \"priority\":\"10\", " + "\"src-mac\":\"" + getNetworkEntitesCache().get(h1).getMac().get(0) + "\", \"ingress-port\":\"" + getNetworkEntitesCache().get(h1).getAttachmentPoint().get(0).getPort() + "\", " + "\"dst-mac\": \"" + getNetworkEntitesCache().get(h2).getMac().get(0) + "\", \"active\":\"true\"," + "\"actions\":\"output=" + s1ToS2Port + "\"}";
            rules.add(rule1To2);
        // Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, rule1To2);
        }
    }
    // s2 to s1
    for (String h1 : s2Hosts) {
        for (String h2 : s1Hosts) {
            String rule2To1 = "{\"switch\": \"" + s2 + "\", \"name\":\"" + h1 + "To" + h2 + "\", \"cookie\":\"0\", \"priority\":\"10\", " + "\"src-mac\":\"" + getNetworkEntitesCache().get(h1).getMac().get(0) + "\", \"ingress-port\":\"" + getNetworkEntitesCache().get(h1).getAttachmentPoint().get(0).getPort() + "\", " + "\"dst-mac\": \"" + getNetworkEntitesCache().get(h2).getMac().get(0) + "\", \"active\":\"true\"," + "\"actions\":\"output=" + s2ToS1Port + "\"}";
            rules.add(rule2To1);
        // Logger.getLogger(SDNSweep.class.getName()).log(Level.INFO, rule2To1);
        }
    }
    pushFlows(rules);
    flowPushed = true;
}
Also used : NetworkEntity(nl.uva.cs.lobcder.rest.wrappers.NetworkEntity) ArrayList(java.util.ArrayList) Link(nl.uva.cs.lobcder.rest.wrappers.Link)

Aggregations

Link (nl.uva.cs.lobcder.rest.wrappers.Link)5 ArrayList (java.util.ArrayList)4 NetworkEntity (nl.uva.cs.lobcder.rest.wrappers.NetworkEntity)4 AttachmentPoint (nl.uva.cs.lobcder.rest.wrappers.AttachmentPoint)3 GenericType (com.sun.jersey.api.client.GenericType)1 WebResource (com.sun.jersey.api.client.WebResource)1 List (java.util.List)1 Port (nl.uva.cs.lobcder.catalogue.SDNSweep.Port)1 DefaultWeightedEdge (org.jgrapht.graph.DefaultWeightedEdge)1