Implement serialization/deserialization feature for each Intent classes

Change-Id: I63db34914d8df76d5a000831da1e03e03383e62f
diff --git a/src/main/java/net/onrc/onos/intent/ConstrainedShortestPathIntent.java b/src/main/java/net/onrc/onos/intent/ConstrainedShortestPathIntent.java
index b659179..8a2e101 100644
--- a/src/main/java/net/onrc/onos/intent/ConstrainedShortestPathIntent.java
+++ b/src/main/java/net/onrc/onos/intent/ConstrainedShortestPathIntent.java
@@ -4,6 +4,9 @@
 import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
 import net.onrc.onos.ofcontroller.networkgraph.Port;
 
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
 /**
  * @author Toshio Koide (t-koide@onlab.us)
  */
@@ -26,7 +29,38 @@
 		this.bandwidth = bandwidth;
 	}
 
+	public static ConstrainedShortestPathIntent fromBytes(NetworkGraph graph, byte[] bytes) {
+		Input input = new Input(bytes);
+		ConstrainedShortestPathIntent intent = new ConstrainedShortestPathIntent(graph,
+				input.readString(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readDouble());
+		input.close();
+		return intent;
+	}
+
 	public Double getBandwidth() {
 		return bandwidth;
 	}
+
+	@Override
+	public byte[] toBytes() {
+		byte[] buffer = new byte[1024];
+		Output output = new Output(buffer, -1);
+		output.writeString(id);
+		output.writeLong(srcPort.getSwitch().getDpid());
+		output.writeLong(srcPort.getNumber());
+		output.writeLong(srcMac.toLong());
+		output.writeLong(dstPort.getSwitch().getDpid());
+		output.writeLong(dstPort.getNumber());
+		output.writeLong(dstMac.toLong());
+		output.writeDouble(bandwidth);
+		output.close();
+		return output.toBytes();
+	}
 }
diff --git a/src/main/java/net/onrc/onos/intent/Intent.java b/src/main/java/net/onrc/onos/intent/Intent.java
index 823b106..070af25 100644
--- a/src/main/java/net/onrc/onos/intent/Intent.java
+++ b/src/main/java/net/onrc/onos/intent/Intent.java
@@ -13,6 +13,8 @@
 	public String getId() {
 		return id;
 	}
+	
+	abstract public byte[] toBytes();
 
 	@Override
 	public int hashCode() {
diff --git a/src/main/java/net/onrc/onos/intent/PathIntent.java b/src/main/java/net/onrc/onos/intent/PathIntent.java
index 07f53e1..3afa92e 100644
--- a/src/main/java/net/onrc/onos/intent/PathIntent.java
+++ b/src/main/java/net/onrc/onos/intent/PathIntent.java
@@ -38,4 +38,10 @@
 	public Intent getParentIntent() {
 		return parentIntent;
 	}
+
+	@Override
+	public byte[] toBytes() {
+		// TODO not implemented yet
+		return null;
+	}
 }
diff --git a/src/main/java/net/onrc/onos/intent/ShortestPathIntent.java b/src/main/java/net/onrc/onos/intent/ShortestPathIntent.java
index b1f58f5..3fdcf66 100644
--- a/src/main/java/net/onrc/onos/intent/ShortestPathIntent.java
+++ b/src/main/java/net/onrc/onos/intent/ShortestPathIntent.java
@@ -1,5 +1,8 @@
 package net.onrc.onos.intent;
 
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
 import net.floodlightcontroller.util.MACAddress;
 import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
 import net.onrc.onos.ofcontroller.networkgraph.Port;
@@ -33,6 +36,20 @@
 		this.dstMac = MACAddress.valueOf(dstMac);
 	}
 
+	public static ShortestPathIntent fromBytes(NetworkGraph graph, byte[] bytes) {
+		Input input = new Input(bytes);
+		ShortestPathIntent intent = new ShortestPathIntent(graph,
+				input.readString(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong(),
+				input.readLong());
+		input.close();
+		return intent;
+		}
+
 	public Port getSourcePort() {
 		return srcPort;
 	}
@@ -55,4 +72,19 @@
 				srcPort.toString(), srcMac.toString(),
 				dstPort.toString(), dstMac.toString());
 	}
+
+	@Override
+	public byte[] toBytes() {
+		byte[] buffer = new byte[1024];
+		Output output = new Output(buffer, -1);
+		output.writeString(id);
+		output.writeLong(srcPort.getSwitch().getDpid());
+		output.writeLong(srcPort.getNumber());
+		output.writeLong(srcMac.toLong());
+		output.writeLong(dstPort.getSwitch().getDpid());
+		output.writeLong(dstPort.getNumber());
+		output.writeLong(dstMac.toLong());
+		output.close();
+		return output.toBytes();
+	}
 }
diff --git a/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java b/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java
new file mode 100644
index 0000000..8160ca8
--- /dev/null
+++ b/src/test/java/net/onrc/onos/intent/ConstrainedShortestPathIntentTest.java
@@ -0,0 +1,42 @@
+package net.onrc.onos.intent;
+
+import static org.junit.Assert.*;
+import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ConstrainedShortestPathIntentTest {
+	NetworkGraph g;
+
+	@Before
+	public void setUp() throws Exception {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology();
+		g = graph;
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void test() {
+		ConstrainedShortestPathIntent intent1 =
+				new ConstrainedShortestPathIntent(g, "1", 1L, 20L, 1L, 4L, 20L, 4L, 1000.0);
+
+		byte b[] = intent1.toBytes();
+
+		ConstrainedShortestPathIntent intent2 =
+				ConstrainedShortestPathIntent.fromBytes(g, b);
+
+		assertEquals("1", intent2.getId());
+		assertEquals(Long.valueOf(1), intent2.getSourcePort().getSwitch().getDpid());
+		assertEquals(Long.valueOf(20), intent2.getSourcePort().getNumber());
+		assertEquals(1L, intent2.getSourceMac().toLong());
+		assertEquals(Long.valueOf(4), intent2.getDestinationPort().getSwitch().getDpid());
+		assertEquals(Long.valueOf(20), intent2.getDestinationPort().getNumber());
+		assertEquals(4L, intent2.getDestinationMac().toLong());
+		assertEquals(Double.valueOf(1000.0), intent2.getBandwidth());
+	}
+}
diff --git a/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java b/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java
new file mode 100644
index 0000000..ec0d54f
--- /dev/null
+++ b/src/test/java/net/onrc/onos/intent/MockNetworkGraph.java
@@ -0,0 +1,66 @@
+package net.onrc.onos.intent;
+
+import net.onrc.onos.ofcontroller.networkgraph.AbstractNetworkGraph;
+import net.onrc.onos.ofcontroller.networkgraph.Link;
+import net.onrc.onos.ofcontroller.networkgraph.LinkImpl;
+import net.onrc.onos.ofcontroller.networkgraph.Switch;
+import net.onrc.onos.ofcontroller.networkgraph.SwitchImpl;
+
+public class MockNetworkGraph extends AbstractNetworkGraph {
+	public Switch addSwitch(Long switchId) {
+		SwitchImpl sw = new SwitchImpl(this, switchId);
+		switches.put(sw.getDpid(), sw);
+		return sw;
+
+	}
+
+	public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
+		return new LinkImpl(
+				this,
+				getSwitch(srcDpid).getPort(srcPortNo),
+				getSwitch(dstDpid).getPort(dstPortNo));
+	}
+
+	public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
+		Link[] links = new Link[2];
+		links[0] = addLink(srcDpid, srcPortNo, dstDpid, dstPortNo);
+		links[1] = addLink(dstDpid, dstPortNo, srcDpid, srcPortNo);
+
+		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);
+			}
+		}
+
+		// 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);
+
+		// 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);
+
+		// set capacity of all links to 1000Mbps
+		for (Link link: getLinks()) {
+			((LinkImpl)link).setCapacity(1000.0);
+		}
+	}
+}
diff --git a/src/test/java/net/onrc/onos/intent/ShortestPathIntentTest.java b/src/test/java/net/onrc/onos/intent/ShortestPathIntentTest.java
new file mode 100644
index 0000000..a0da341
--- /dev/null
+++ b/src/test/java/net/onrc/onos/intent/ShortestPathIntentTest.java
@@ -0,0 +1,42 @@
+package net.onrc.onos.intent;
+
+import static org.junit.Assert.*;
+import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ShortestPathIntentTest {
+	NetworkGraph g;
+
+	@Before
+	public void setUp() throws Exception {
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology();
+		g = graph;
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void test() {
+		ShortestPathIntent intent1 =
+				new ShortestPathIntent(g, "1", 1L, 20L, 1L, 4L, 20L, 4L);
+
+		byte b[] = intent1.toBytes();
+
+		ShortestPathIntent intent2 =
+				ShortestPathIntent.fromBytes(g, b);
+
+		assertEquals("1", intent2.getId());
+		assertEquals(Long.valueOf(1), intent2.getSourcePort().getSwitch().getDpid());
+		assertEquals(Long.valueOf(20), intent2.getSourcePort().getNumber());
+		assertEquals(1L, intent2.getSourceMac().toLong());
+		assertEquals(Long.valueOf(4), intent2.getDestinationPort().getSwitch().getDpid());
+		assertEquals(Long.valueOf(20), intent2.getDestinationPort().getNumber());
+		assertEquals(4L, intent2.getDestinationMac().toLong());
+	}
+
+}
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 93aa8ab..73eee6b 100644
--- a/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java
+++ b/src/test/java/net/onrc/onos/intent/runtime/UseCaseTest.java
@@ -4,6 +4,7 @@
 
 import net.onrc.onos.intent.ConstrainedShortestPathIntent;
 import net.onrc.onos.intent.Intent;
+import net.onrc.onos.intent.MockNetworkGraph;
 import net.onrc.onos.intent.PathIntent;
 import net.onrc.onos.intent.PathIntents;
 import net.onrc.onos.intent.ShortestPathIntent;
@@ -17,79 +18,13 @@
  * @author Toshio Koide (t-koide@onlab.us)
  */
 public class UseCaseTest {
-	public class MutableNetworkGraph extends AbstractNetworkGraph {
-		public Switch addSwitch(Long switchId) {
-			SwitchImpl sw = new SwitchImpl(this, switchId);
-			switches.put(sw.getDpid(), sw);
-			return sw;
-
-		}
-
-		public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
-			return new LinkImpl(
-					this,
-					getSwitch(srcDpid).getPort(srcPortNo),
-					getSwitch(dstDpid).getPort(dstPortNo));
-		}
-
-		public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
-			Link[] links = new Link[2];
-			links[0] = addLink(srcDpid, srcPortNo, dstDpid, dstPortNo);
-			links[1] = addLink(dstDpid, dstPortNo, srcDpid, srcPortNo);
-
-			return links;
-		}
-	}
-
 	NetworkGraph g;
 
 	@Before
 	public void setUp() {
-		MutableNetworkGraph g = new MutableNetworkGraph();
-
-		// add 10 switches (24 ports switch)
-		for (Long dpid=1L; dpid<10L; dpid++) {
-			SwitchImpl sw = (SwitchImpl) g.addSwitch(dpid);
-			for (Long j=1L; j<=24L; j++) {
-				sw.addPort(j);
-			}
-		}
-
-		// add loop path
-		g.addBidirectionalLinks(1L, 1L, 2L, 2L);
-		g.addBidirectionalLinks(2L, 1L, 3L, 2L);
-		g.addBidirectionalLinks(3L, 1L, 4L, 2L);
-		g.addBidirectionalLinks(4L, 1L, 5L, 2L);
-		g.addBidirectionalLinks(5L, 1L, 6L, 2L);
-		g.addBidirectionalLinks(6L, 1L, 7L, 2L);
-		g.addBidirectionalLinks(7L, 1L, 8L, 2L);
-		g.addBidirectionalLinks(8L, 1L, 9L, 2L);
-		g.addBidirectionalLinks(9L, 1L, 1L, 2L);
-
-		// add other links
-		g.addBidirectionalLinks(1L, 3L, 5L, 3L);
-		g.addBidirectionalLinks(2L, 4L, 5L, 4L);
-		g.addBidirectionalLinks(2L, 5L, 7L, 5L);
-		g.addBidirectionalLinks(3L, 6L, 7L, 6L);
-		g.addBidirectionalLinks(3L, 7L, 8L, 7L);
-		g.addBidirectionalLinks(3L, 8L, 9L, 8L);
-		g.addBidirectionalLinks(4L, 9l, 9L, 9L);
-
-		// set capacity of all links to 1000Mbps
-		for (Link link: g.getLinks()) {
-			((LinkImpl)link).setCapacity(1000.0);
-		}
-
-		/*
-		// add Devices
-		for (Long l=1L; l<=9L; l++) {
-			DeviceImpl d = new DeviceImpl(g, MACAddress.valueOf(l));
-			d.addAttachmentPoint(g.getSwitch(l).getPort(20L));
-			g.addDevice(d);
-		}
-		*/
-
-		this.g = g;
+		MockNetworkGraph graph = new MockNetworkGraph();
+		graph.createSampleTopology();
+		g = graph;
 	}
 
 	@After