Add and update test codes for Intent related codes

Change-Id: Idbd1e9978c1f9df0e2d19b15d108ff1f0b8d0c8b
diff --git a/src/test/java/net/onrc/onos/intent/ConstrainedBFSTreeTest.java b/src/test/java/net/onrc/onos/intent/ConstrainedBFSTreeTest.java
new file mode 100644
index 0000000..f4413b8
--- /dev/null
+++ b/src/test/java/net/onrc/onos/intent/ConstrainedBFSTreeTest.java
@@ -0,0 +1,144 @@
+package net.onrc.onos.intent;
+
+import static org.junit.Assert.*;
+import net.onrc.onos.intent.IntentOperation.Operator;
+import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
+import net.onrc.onos.ofcontroller.networkgraph.Path;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Toshio Koide (t-koide@onlab.us)
+ */
+public class ConstrainedBFSTreeTest {
+	static long LOCAL_PORT = 0xFFFEL;
+
+	@Before
+	public void setUp() throws Exception {
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void testCreate() {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology1();
+		ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L));
+		assertNotNull(tree);
+	}
+
+	@Test
+	public void testCreateConstrained() {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology1();
+		PathIntentMap intents = new PathIntentMap();
+		ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 1000.0);
+		assertNotNull(tree);
+	}
+
+	@Test
+	public void testGetPath() {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology1();
+		ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L));
+		Path path11 = tree.getPath(graph.getSwitch(1L));
+		Path path12 = tree.getPath(graph.getSwitch(2L));
+		Path path13 = tree.getPath(graph.getSwitch(3L));
+		Path path14 = tree.getPath(graph.getSwitch(4L));
+
+		assertNotNull(path11);
+		assertEquals(0, path11.size());
+
+		assertNotNull(path12);
+		assertEquals(1, path12.size());
+		assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path12.get(0));
+
+		assertNotNull(path13);
+		assertEquals(2, path13.size());
+		if (path13.get(0).getDst().getDpid() == 2L) {
+			assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path13.get(0));
+			assertEquals(new LinkEvent(graph.getLink(2L, 23L)), path13.get(1));
+		}
+		else {
+			assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path13.get(0));
+			assertEquals(new LinkEvent(graph.getLink(4L, 43L)), path13.get(1));
+		}
+
+		assertNotNull(path14);
+		assertEquals(1, path14.size());
+		assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path14.get(0));
+	}
+
+	@Test
+	public void testGetPathNull() {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology1();
+		graph.removeLink(1L, 12L, 2L, 21L);
+		graph.removeLink(1L, 14L, 4L, 41L);
+		// now, there is no path from switch 1, but to switch1
+
+		ConstrainedBFSTree tree1 = new ConstrainedBFSTree(graph.getSwitch(1L));
+		Path path12 = tree1.getPath(graph.getSwitch(2L));
+		Path path13 = tree1.getPath(graph.getSwitch(3L));
+		Path path14 = tree1.getPath(graph.getSwitch(4L));
+
+		ConstrainedBFSTree tree2 = new ConstrainedBFSTree(graph.getSwitch(2L));
+		Path path21 = tree2.getPath(graph.getSwitch(1L));
+
+		assertNull(path12);
+		assertNull(path13);
+		assertNull(path14);
+		assertNotNull(path21);
+		assertEquals(1, path21.size());
+		assertEquals(new LinkEvent(graph.getLink(2L, 21L)), path21.get(0));
+	}
+
+	@Test
+	public void testGetConstrainedPath() {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology1();
+		PathIntentMap intents = new PathIntentMap();
+		IntentOperationList intentOps = new IntentOperationList();
+
+		// create constrained shortest path intents that have the same source destination ports
+		ConstrainedShortestPathIntent intent1 = new ConstrainedShortestPathIntent(
+				"1", 1L, LOCAL_PORT, 0x111L, 2L, LOCAL_PORT, 0x222L, 600.0);
+		ConstrainedShortestPathIntent intent2 = new ConstrainedShortestPathIntent(
+				"2", 1L, LOCAL_PORT, 0x333L, 2L, LOCAL_PORT, 0x444L, 600.0);
+
+		// calculate path of the intent1
+		ConstrainedBFSTree tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
+		Path path1 = tree.getPath(graph.getSwitch(2L));
+
+		assertNotNull(path1);
+		assertEquals(1, path1.size());
+		assertEquals(new LinkEvent(graph.getLink(1L, 12L)), path1.get(0));
+
+		PathIntent pathIntent1 = new PathIntent("pi1", path1, 600.0, intent1);
+		intentOps.add(Operator.ADD, pathIntent1);
+		intents.executeOperations(intentOps);
+
+		// calculate path of the intent2
+		tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
+		Path path2 = tree.getPath(graph.getSwitch(2L));
+
+		assertNotNull(path2);
+		assertEquals(2, path2.size());
+		assertEquals(new LinkEvent(graph.getLink(1L, 14L)), path2.get(0));
+		assertEquals(new LinkEvent(graph.getLink(4L, 42L)), path2.get(1));
+
+		PathIntent pathIntent2 = new PathIntent("pi2", path2, 600.0, intent2);
+		intentOps.add(Operator.ADD, pathIntent2);
+		intents.executeOperations(intentOps);
+
+		// calculate path of the intent3
+		tree = new ConstrainedBFSTree(graph.getSwitch(1L), intents, 600.0);
+		Path path3 = tree.getPath(graph.getSwitch(2L));
+
+		assertNull(path3);
+	}
+}
\ No newline at end of file
diff --git a/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java b/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java
index aba4c11..99fa0b3 100644
--- a/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java
+++ b/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java
@@ -1,6 +1,7 @@
 package net.onrc.onos.intent;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
 
 import org.junit.After;
 import org.junit.Before;
@@ -23,21 +24,36 @@
 	}
 
 	@Test
-	public void test() {
-		Kryo kryo = new Kryo();
-		Output output = new Output(1024);
-
+	public void testCreate() {
 		ConstrainedShortestPathIntent intent1 =
 				new ConstrainedShortestPathIntent("1", 2L, 3L, 4L, 5L, 6L, 7L, 1000.0);
 
+		assertEquals("1", intent1.getId());
+		assertEquals(2L, intent1.getSrcSwitchDpid());
+		assertEquals(3L, intent1.getSrcPortNumber());
+		assertEquals(4L, intent1.getSrcMac());
+		assertEquals(5L, intent1.getDstSwitchDpid());
+		assertEquals(6L, intent1.getDstPortNumber());
+		assertEquals(7L, intent1.getDstMac());
+		assertEquals(1000.0, intent1.getBandwidth(), 0.0);
+	}
+
+	@Test
+	public void testKryo() {
+		KryoFactory factory = new KryoFactory();
+		Kryo kryo = factory.newKryo();
+		Output output = new Output(1000);
+
+		ConstrainedShortestPathIntent intent1 =
+				new ConstrainedShortestPathIntent("1", 2L, 3L, 4L, 5L, 6L, 7L, 1000.0);
 		kryo.writeObject(output, intent1);
+
 		output.close();
+		byte bytes[] = output.toBytes();
 
-		Input input = new Input(output.toBytes());
-		ConstrainedShortestPathIntent intent2 =
-				kryo.readObject(input, ConstrainedShortestPathIntent.class);
+		Input input = new Input(bytes);
+		ConstrainedShortestPathIntent intent2 = kryo.readObject(input, ConstrainedShortestPathIntent.class);
 		input.close();
-
 		assertEquals("1", intent2.getId());
 		assertEquals(2L, intent2.getSrcSwitchDpid());
 		assertEquals(3L, intent2.getSrcPortNumber());
diff --git a/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java b/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java
index 5ae7b13..9d85e6f 100644
--- a/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java
+++ b/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java
@@ -9,9 +9,15 @@
 import net.onrc.onos.ofcontroller.networkgraph.SwitchImpl;
 
 /**
+ * A mock class of NetworkGraph.
+ * This class should be used only by test codes.
+ *
  * @author Toshio Koide (t-koide@onlab.us)
  */
 public class MockNetworkGraph extends NetworkGraphImpl {
+	public static Long LOCAL_PORT = 0xFFFEL;
+	public SwitchImpl sw1, sw2, sw3, sw4;
+
 	class DetachableLinkImpl extends LinkImpl {
 		public DetachableLinkImpl(NetworkGraph graph, Port srcPort, Port dstPort) {
 			super(graph, srcPort, dstPort);
@@ -25,7 +31,6 @@
 		SwitchImpl sw = new SwitchImpl(this, switchId);
 		this.putSwitch(sw);
 		return sw;
-
 	}
 
 	public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
@@ -43,34 +48,41 @@
 		return links;
 	}
 
-	public void createSampleTopology() {
-		// add 10 switches (24 ports switch)
-		for (Long dpid=1L; dpid<10L; dpid++) {
-			SwitchImpl sw = (SwitchImpl) addSwitch(dpid);
-			for (Long j=1L; j<=24L; j++) {
-				sw.addPort(j);
-			}
-		}
+	/**
+	 * create sample topology of 4 switches and 5 bidirectional links.
+	 * <pre>
+	 * [1] --- [2]
+	 *  |    /  |
+	 *  |  /    |
+	 * [4] --- [3]
+	 * </pre>
+	 */
+	public void createSampleTopology1() {
+		sw1 = (SwitchImpl) addSwitch(1L);
+		sw1.addPort(LOCAL_PORT);
+		sw2 = (SwitchImpl) addSwitch(2L);
+		sw2.addPort(LOCAL_PORT);
+		sw3 = (SwitchImpl) addSwitch(3L);
+		sw3.addPort(LOCAL_PORT);
+		sw4 = (SwitchImpl) addSwitch(4L);
+		sw4.addPort(LOCAL_PORT);
 
-		// add loop path
-		addBidirectionalLinks(1L, 1L, 2L, 2L);
-		addBidirectionalLinks(2L, 1L, 3L, 2L);
-		addBidirectionalLinks(3L, 1L, 4L, 2L);
-		addBidirectionalLinks(4L, 1L, 5L, 2L);
-		addBidirectionalLinks(5L, 1L, 6L, 2L);
-		addBidirectionalLinks(6L, 1L, 7L, 2L);
-		addBidirectionalLinks(7L, 1L, 8L, 2L);
-		addBidirectionalLinks(8L, 1L, 9L, 2L);
-		addBidirectionalLinks(9L, 1L, 1L, 2L);
+		sw1.addPort(12L); // sw1 -> sw2
+		sw1.addPort(14L); // sw1 -> sw4
+		sw2.addPort(21L); // sw2 -> sw1
+		sw2.addPort(23L); // sw2 -> sw3
+		sw2.addPort(24L); // sw2 -> sw4
+		sw3.addPort(32L); // sw3 -> sw2
+		sw3.addPort(34L); // sw3 -> sw4
+		sw4.addPort(41L); // sw4 -> sw1
+		sw4.addPort(42L); // sw4 -> sw2
+		sw4.addPort(43L); // sw4 -> sw3
 
-		// add other links
-		addBidirectionalLinks(1L, 3L, 5L, 3L);
-		addBidirectionalLinks(2L, 4L, 5L, 4L);
-		addBidirectionalLinks(2L, 5L, 7L, 5L);
-		addBidirectionalLinks(3L, 6L, 7L, 6L);
-		addBidirectionalLinks(3L, 7L, 8L, 7L);
-		addBidirectionalLinks(3L, 8L, 9L, 8L);
-		addBidirectionalLinks(4L, 9l, 9L, 9L);
+		addBidirectionalLinks(1L, 12L, 2L, 21L);
+		addBidirectionalLinks(2L, 23L, 3L, 32L);
+		addBidirectionalLinks(3L, 34L, 4L, 43L);
+		addBidirectionalLinks(4L, 41L, 1L, 14L);
+		addBidirectionalLinks(2L, 24L, 4L, 42L);
 
 		// set capacity of all links to 1000Mbps
 		for (Link link: getLinks()) {
diff --git a/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java b/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java
index 0c1746f..f24fc99 100755
--- a/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java
+++ b/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java
@@ -54,11 +54,13 @@
 	@SuppressWarnings("rawtypes")
 	private IEventChannel eventChannel;
 
+	private static Long LOCAL_PORT = 0xFFFEL;
+
 	@SuppressWarnings("unchecked")
 	@Before
 	public void setUp() throws Exception {
 		MockNetworkGraph graph = new MockNetworkGraph();
-		graph.createSampleTopology();
+		graph.createSampleTopology1();
 		g = graph;
 
 		datagridService = createMock(IDatagridService.class);
@@ -124,9 +126,9 @@
 	public void createShortestPaths() throws FloodlightModuleException {
 		// create shortest path intents
 		IntentOperationList opList = new IntentOperationList();
-		opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
-		opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
-		opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
+		opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
 
 		// compile high-level intent operations into low-level intent operations (calculate paths)
 		PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
@@ -147,11 +149,11 @@
 	public void createConstrainedShortestPaths() throws FloodlightModuleException {
 		// create constrained shortest path intents
 		IntentOperationList opList = new IntentOperationList();
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
 
 		// compile high-level intent operations into low-level intent operations (calculate paths)
 		PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
@@ -172,11 +174,11 @@
 	public void createMixedShortestPaths() throws FloodlightModuleException {
 		// create constrained & best effort shortest path intents
 		IntentOperationList opList = new IntentOperationList();
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
-		opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
-		opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
-		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
+		opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
 
 		// compile high-level intent operations into low-level intent operations (calculate paths)
 		PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
@@ -206,9 +208,9 @@
 
 		// create shortest path intents
 		IntentOperationList opList = new IntentOperationList();
-		opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
-		opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
-		opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
+		opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
+		opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
 
 		// compile high-level intent operations into low-level intent operations (calculate paths)
 		PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
@@ -237,10 +239,10 @@
 		runtime1.getPathIntents().changeStates(states);
 
 		// link down
-		((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
-		((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
-		LinkEvent linkEvent1 = new LinkEvent(1L, 2L, 9L, 1L);
-		LinkEvent linkEvent2 = new LinkEvent(9L, 1L, 1L, 2L);
+		((MockNetworkGraph)g).removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1"
+		((MockNetworkGraph)g).removeLink(2L, 21L, 1L, 12L);
+		LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L);
+		LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L);
 		removedLinkEvents.clear();
 		removedLinkEvents.add(linkEvent1);
 		removedLinkEvents.add(linkEvent2);