[Emu] FlowAnalyzer app code and tests

Change-Id: I8b6f83f7d2e76a4e8cf770d9b0018be283e91bf3
diff --git a/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/DefaultMutableTopologyGraph.java b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/DefaultMutableTopologyGraph.java
new file mode 100644
index 0000000..4ea3aa4
--- /dev/null
+++ b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/DefaultMutableTopologyGraph.java
@@ -0,0 +1,28 @@
+package org.onosproject.flowanalyzer;
+
+import org.onlab.graph.MutableAdjacencyListsGraph;
+import org.onosproject.net.topology.TopologyEdge;
+import org.onosproject.net.topology.TopologyGraph;
+import org.onosproject.net.topology.TopologyVertex;
+
+import java.util.Set;
+
+/**
+ * Default implementation of an immutable topology graph based on a generic
+ * implementation of adjacency lists graph.
+ */
+public class DefaultMutableTopologyGraph
+        extends MutableAdjacencyListsGraph<TopologyVertex, TopologyEdge>
+        implements TopologyGraph {
+
+    /**
+     * Creates a topology graph comprising of the specified vertexes and edges.
+     *
+     * @param vertexes set of graph vertexes
+     * @param edges    set of graph edges
+     */
+    public DefaultMutableTopologyGraph(Set<TopologyVertex> vertexes, Set<TopologyEdge> edges) {
+        super(vertexes, edges);
+    }
+
+}
diff --git a/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/FlowAnalyzerTest.java b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/FlowAnalyzerTest.java
new file mode 100644
index 0000000..faa2f5f
--- /dev/null
+++ b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/FlowAnalyzerTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.flowanalyzer;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleExtPayLoad;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.onosproject.net.topology.TopologyService;
+
+import java.util.Arrays;
+import java.util.TreeSet;
+
+import static org.junit.Assert.assertEquals;
+
+
+/**
+ * Created by nikcheerla on 7/20/15.
+ */
+public class FlowAnalyzerTest {
+
+    FlowRuleService flowRuleService = new MockFlowRuleService();
+    TopologyService topologyService;
+    MockLinkService linkService = new MockLinkService();
+
+    @Test
+    @Ignore("This needs to be reworked to be more robust")
+    public void basic() {
+        flowRuleService = new MockFlowRuleService();
+        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 90));
+        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 100));
+        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 150));
+        flowRuleService.applyFlowRules(genFlow("ATL-002", 80, 70));
+        flowRuleService.applyFlowRules(genFlow("ATL-003", 120, 130));
+        flowRuleService.applyFlowRules(genFlow("ATL-004", 50));
+        flowRuleService.applyFlowRules(genFlow("ATL-005", 140, 10));
+
+        linkService.addLink("H00:00:00:00:00:0660", 160, "ATL-005", 140);
+        linkService.addLink("ATL-005", 10, "ATL-004", 40);
+        linkService.addLink("ATL-004", 50, "ATL-002", 80);
+        linkService.addLink("ATL-002", 70, "ATL-001", 110);
+        linkService.addLink("ATL-001", 150, "H00:00:00:00:00:0770", 170);
+        linkService.addLink("ATL-001", 90, "ATL-004", 30);
+        linkService.addLink("ATL-001", 100, "ATL-003", 120);
+        linkService.addLink("ATL-003", 130, "ATL-005", 20);
+
+        topologyService = new MockTopologyService(linkService.createdGraph);
+
+        FlowAnalyzer flowAnalyzer = new FlowAnalyzer();
+        flowAnalyzer.flowRuleService = flowRuleService;
+        flowAnalyzer.linkService = linkService;
+        flowAnalyzer.topologyService = topologyService;
+
+        String labels = flowAnalyzer.analysisOutput();
+        String correctOutput = "Flow Rule: Device: atl-005, [IN_PORT{port=140}], [OUTPUT{port=10}]\n" +
+                "Analysis: Cleared!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-003, [IN_PORT{port=120}], [OUTPUT{port=130}]\n" +
+                "Analysis: Black Hole!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=90}]\n" +
+                "Analysis: Cycle Critical Point!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-004, [], [OUTPUT{port=50}]\n" +
+                "Analysis: Cycle!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=150}]\n" +
+                "Analysis: Cleared!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=100}]\n" +
+                "Analysis: Black Hole!\n" +
+                "\n" +
+                "Flow Rule: Device: atl-002, [IN_PORT{port=80}], [OUTPUT{port=70}]\n" +
+                "Analysis: Cycle!\n";
+        assertEquals("Wrong labels", new TreeSet(Arrays.asList(labels.replaceAll("\\s+", "").split("!"))),
+                     new TreeSet(Arrays.asList(correctOutput.replaceAll("\\s+", "").split("!"))));
+    }
+
+    public FlowRule genFlow(String d, long inPort, long outPort) {
+        DeviceId device = DeviceId.deviceId(d);
+        TrafficSelector ts = DefaultTrafficSelector.builder().matchInPort(PortNumber.portNumber(inPort)).build();
+        TrafficTreatment tt = DefaultTrafficTreatment.builder()
+                .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build();
+        return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"),
+                                   50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5]));
+    }
+    public FlowRule genFlow(String d, long outPort) {
+        DeviceId device = DeviceId.deviceId(d);
+        TrafficSelector ts = DefaultTrafficSelector.builder().build();
+        TrafficTreatment tt = DefaultTrafficTreatment.builder()
+                .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build();
+        return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"),
+                                   50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5]));
+    }
+
+}
diff --git a/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockFlowRuleService.java b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockFlowRuleService.java
new file mode 100644
index 0000000..40bb004
--- /dev/null
+++ b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockFlowRuleService.java
@@ -0,0 +1,103 @@
+package org.onosproject.flowanalyzer;
+
+import com.google.common.collect.Sets;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperations;
+import org.onosproject.net.flow.FlowRuleServiceAdapter;
+
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+
+/**
+ * Created by nikcheerla on 7/20/15.
+ */
+
+public class MockFlowRuleService extends FlowRuleServiceAdapter {
+
+    final Set<FlowRule> flows = Sets.newHashSet();
+    boolean success;
+
+    int errorFlow = -1;
+    public void setErrorFlow(int errorFlow) {
+        this.errorFlow = errorFlow;
+    }
+
+    public void setFuture(boolean success) {
+        this.success = success;
+    }
+
+    @Override
+    public void apply(FlowRuleOperations ops) {
+        AtomicBoolean thisSuccess = new AtomicBoolean(success);
+        ops.stages().forEach(stage -> stage.forEach(flow -> {
+            if (errorFlow == flow.rule().id().value()) {
+                thisSuccess.set(false);
+            } else {
+                switch (flow.type()) {
+                    case ADD:
+                    case MODIFY: //TODO is this the right behavior for modify?
+                        flows.add(flow.rule());
+                        break;
+                    case REMOVE:
+                        flows.remove(flow.rule());
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }));
+        if (thisSuccess.get()) {
+            ops.callback().onSuccess(ops);
+        } else {
+            ops.callback().onError(ops);
+        }
+    }
+
+    @Override
+    public int getFlowRuleCount() {
+        return flows.size();
+    }
+
+    @Override
+    public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
+        return flows.stream()
+                .filter(flow -> flow.deviceId().equals(deviceId))
+                .map(DefaultFlowEntry::new)
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public void applyFlowRules(FlowRule... flowRules) {
+        for (FlowRule flow : flowRules) {
+            flows.add(flow);
+        }
+    }
+
+    @Override
+    public void removeFlowRules(FlowRule... flowRules) {
+        for (FlowRule flow : flowRules) {
+            flows.remove(flow);
+        }
+    }
+
+    @Override
+    public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
+        return flows.stream()
+                .filter(flow -> flow.appId() == id.id())
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
+        return flows.stream()
+                .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
+                .collect(Collectors.toList());
+    }
+}
+
+
diff --git a/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockLinkService.java b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockLinkService.java
new file mode 100644
index 0000000..2171c6f
--- /dev/null
+++ b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockLinkService.java
@@ -0,0 +1,183 @@
+package org.onosproject.flowanalyzer;
+
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.link.LinkServiceAdapter;
+import org.onosproject.net.topology.TopologyEdge;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.ElementId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.Link;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.net.topology.TopologyVertex;
+
+import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.HashSet;
+
+import static org.onosproject.net.Link.State.ACTIVE;
+
+
+/**
+ * Created by nikcheerla on 7/21/15.
+ */
+public class MockLinkService extends LinkServiceAdapter {
+    DefaultMutableTopologyGraph createdGraph = new DefaultMutableTopologyGraph(new HashSet<>(), new HashSet<>());
+    List<Link> links = new ArrayList<>();
+
+    @Override
+    public int getLinkCount() {
+        return links.size();
+    }
+
+    @Override
+    public Iterable<Link> getLinks() {
+        return links;
+    }
+
+    @Override
+    public Set<Link> getDeviceLinks(DeviceId deviceId) {
+        Set<Link> egress = getDeviceEgressLinks(deviceId);
+        egress.addAll(getDeviceIngressLinks(deviceId));
+        return egress;
+    }
+
+    @Override
+    public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
+        Set<Link> setL = new HashSet<>();
+        for (Link l: links) {
+            if (l.src().elementId() instanceof DeviceId && l.src().deviceId().equals(deviceId)) {
+                setL.add(l);
+            }
+        }
+        return setL;
+    }
+
+    @Override
+    public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
+        Set<Link> setL = new HashSet<>();
+        for (Link l: links) {
+            if (l.dst().elementId() instanceof DeviceId && l.dst().deviceId().equals(deviceId)) {
+                setL.add(l);
+            }
+        }
+        return setL;
+    }
+
+
+    @Override
+    public Set<Link> getEgressLinks(ConnectPoint pt) {
+        Set<Link> setL = new HashSet<>();
+        for (Link l: links) {
+            if (l.src().equals(pt)) {
+                setL.add(l);
+            }
+        }
+        return setL;
+    }
+
+    @Override
+    public Set<Link> getIngressLinks(ConnectPoint pt) {
+        Set<Link> setL = new HashSet<>();
+        for (Link l: links) {
+            if (l.dst().equals(pt)) {
+                setL.add(l);
+            }
+        }
+        return setL;
+    }
+
+    @Override
+    public Set<Link> getLinks(ConnectPoint pt) {
+        Set<Link> setL = new HashSet<>();
+        for (Link l: links) {
+            if (l.src().equals(pt) || l.dst().equals(pt)) {
+                setL.add(l);
+            }
+        }
+        return setL;
+    }
+
+    public void addLink(String device, long port, String device2, long port2) {
+        ElementId d1;
+        if (device.charAt(0) == 'H') {
+            device = device.substring(1, device.length());
+            d1 = HostId.hostId(device);
+        } else {
+            d1 = DeviceId.deviceId(device);
+        }
+
+        ElementId d2;
+        if (device2.charAt(0) == 'H') {
+            d2 = HostId.hostId(device2.substring(1, device2.length()));
+        } else {
+            d2 = DeviceId.deviceId(device2);
+        }
+
+        ConnectPoint src = new ConnectPoint(d1, PortNumber.portNumber(port));
+        ConnectPoint dst = new ConnectPoint(d2, PortNumber.portNumber(port2));
+        Link curLink;
+        curLink = new Link() {
+            @Override
+            public ConnectPoint src() {
+                return src;
+            }
+
+            @Override
+            public ConnectPoint dst() {
+                return dst;
+            }
+
+            @Override
+            public boolean isDurable() {
+                return true;
+            }
+
+            @Override
+            public Annotations annotations() {
+                return null;
+            }
+
+            @Override
+            public Type type() {
+                return null;
+            }
+
+            @Override
+            public ProviderId providerId() {
+                return null;
+            }
+
+            @Override
+            public State state() {
+                return ACTIVE;
+            }
+        };
+        links.add(curLink);
+        if (d1 instanceof DeviceId && d2 instanceof DeviceId) {
+            TopologyVertex v1 = () -> (DeviceId) d1, v2 = () -> (DeviceId) d2;
+            createdGraph.addVertex(v1);
+            createdGraph.addVertex(v2);
+            createdGraph.addEdge(new TopologyEdge() {
+                @Override
+                public Link link() {
+                    return curLink;
+                }
+
+                @Override
+                public TopologyVertex src() {
+                    return v1;
+                }
+
+                @Override
+                public TopologyVertex dst() {
+                    return v2;
+                }
+            });
+        }
+    }
+
+
+}
diff --git a/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockTopologyService.java b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockTopologyService.java
new file mode 100644
index 0000000..0d25c97
--- /dev/null
+++ b/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/MockTopologyService.java
@@ -0,0 +1,21 @@
+package org.onosproject.flowanalyzer;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyGraph;
+import org.onosproject.net.topology.TopologyServiceAdapter;
+
+
+/**
+ * Created by nikcheerla on 7/20/15.
+ */
+public class MockTopologyService extends TopologyServiceAdapter {
+    TopologyGraph cur;
+
+    public MockTopologyService(TopologyGraph g) {
+        cur = g;
+    }
+
+    @Override
+    public TopologyGraph getGraph(Topology topology) {
+        return cur;
+    }
+}