Merge branch 'master' of https://github.com/OPENNETWORKINGLAB/ONOS into dev2
diff --git a/README.md b/README.md
index ea1553a..bfd8d57 100644
--- a/README.md
+++ b/README.md
@@ -17,11 +17,18 @@
$ mvn clean
$ mvn compile
-
+
+
+Dependencies
+------------
+1. Zookeeper
+ Download and install apache-zookeeper-3.4.5: http://zookeeper.apache.org/releases.html
+2. Cassandra
+ Download and install apache-cassandra-1.2.2: http://cassandra.apache.org/download/
+
Running ONOS
------------
-
1. Start zookeeper
$ cd (ZOOKEEPER-INSTALL-DIR)/bin
@@ -38,8 +45,30 @@
3. Start ONOS instance
+ $ cd (ONOS-INSTALL-DIR)/
+
$ ./start-onos.sh start
4. Start ONOS rest apis
$ ./start-rest.sh start
+
+Running ONOS with Cassandra embedded (Optional)
+-----------------------------------------------
+
+1. Start Zookeeper
+
+ $ cd (ZOOKEEPER-INSTALL-DIR)/bin
+
+ $ ./zkServer.sh start
+
+2. Start ONOS and Cassandra embedded
+
+ $ cd (ONOS-INSTAL_DIR)/
+
+ $ ./start-onos-embedded.sh start
+
+3. Start ONOS rest apis
+
+ $ ./start-rest.sh start
+
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
index c89b2fa..b33591e 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
@@ -27,11 +27,21 @@
protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
protected GraphDBOperation dbop;
+ /**
+ * Update a record in the LinkStorage in a way provided by op.
+ * @param link Record of a link to be updated.
+ * @param op Operation to be done.
+ */
@Override
public void update(Link link, DM_OPERATION op) {
update(link, (LinkInfo)null, op);
}
+ /**
+ * Update multiple records in the LinkStorage in a way provided by op.
+ * @param links List of records to be updated.
+ * @param op Operation to be done.
+ */
@Override
public void update(List<Link> links, DM_OPERATION op) {
for (Link lt: links) {
@@ -39,6 +49,12 @@
}
}
+ /**
+ * Update a record of link with meta-information in the LinkStorage in a way provided by op.
+ * @param link Record of a link to update.
+ * @param linkinfo Meta-information of a link to be updated.
+ * @param op Operation to be done.
+ */
@Override
public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
switch (op) {
@@ -53,6 +69,12 @@
}
}
+ /**
+ * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
+ * @param lt Record of a link to be updated.
+ * @param linkinfo Meta-information of a link to be updated.
+ * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
+ */
public void updateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
IPortObject vportSrc = null, vportDst = null;
@@ -70,7 +92,6 @@
vportDst = dbop.searchPort(dpid, port);
if (vportSrc != null && vportDst != null) {
-
// check if the link exists
Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
@@ -78,7 +99,7 @@
for (IPortObject V : currPorts) {
currLinks.add(V);
}
-
+
if (currLinks.contains(vportDst)) {
// TODO: update linkinfo
if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
@@ -104,6 +125,10 @@
}
}
+ /**
+ * Delete multiple records in the LinkStorage.
+ * @param links List of records to be deleted.
+ */
@Override
public void deleteLinks(List<Link> links) {
@@ -112,7 +137,10 @@
}
}
-
+ /**
+ * Delete a record in the LinkStorage.
+ * @param link Record to be deleted.
+ */
@Override
public void deleteLink(Link lt) {
IPortObject vportSrc = null, vportDst = null;
@@ -162,59 +190,114 @@
}
}
+ /**
+ * Get list of all links connected to the port specified by given DPID and port number.
+ * @param dpid DPID of desired port.
+ * @param port Port number of desired port.
+ * @return List of links. Empty list if no port was found.
+ */
// TODO: Fix me
@Override
public List<Link> getLinks(Long dpid, short port) {
- IPortObject vportSrc;
+ List<Link> links = new ArrayList<Link>();
- vportSrc = dbop.searchPort(HexString.toHexString(dpid), port);
- if (vportSrc != null) {
-
- for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
- if (e.getLabel().equals("link")) {
- break;
- }
- }
- }
- return null;
+ IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
+ ISwitchObject srcSw = srcPort.getSwitch();
+
+ if(srcSw != null) {
+ for(IPortObject dstPort : srcPort.getLinkedPorts()) {
+ ISwitchObject dstSw = dstPort.getSwitch();
+ Link link = new Link(HexString.toLong(srcSw.getDPID()),
+ srcPort.getNumber(),
+ HexString.toLong(dstSw.getDPID()),
+ dstPort.getNumber());
+
+ links.add(link);
+ }
+ }
+
+ return links;
}
+ /**
+ * Initialize the object. Open LinkStorage using given configuration file.
+ * @param conf Path (absolute path for now) to configuration file.
+ */
@Override
public void init(String conf) {
//TODO extract the DB location from properties
this.dbop = new GraphDBOperation(conf);
}
+ /**
+ * Delete records of the links connected to the port specified by given DPID and port number.
+ * @param dpid DPID of desired port.
+ * @param port Port number of desired port.
+ */
+ // TODO: Fix me
@Override
public void deleteLinksOnPort(Long dpid, short port) {
- // TODO Auto-generated method stub
+ List<Link> linksToDelete = getLinks(dpid,port);
+ for(Link l : linksToDelete) {
+ deleteLink(l);
+ }
}
+ /**
+ * Get list of all links connected to the switch specified by given DPID.
+ * @param dpid DPID of desired switch.
+ * @return List of links. Empty list if no port was found.
+ */
+ // TODO: Fix me
@Override
public List<Link> getLinks(String dpid) {
- // TODO Auto-generated method stub
- return null;
+ List<Link> links = new ArrayList<Link>();
+
+ ISwitchObject srcSw = dbop.searchSwitch(dpid);
+
+ if(srcSw != null) {
+ for(IPortObject srcPort : srcSw.getPorts()) {
+ for(IPortObject dstPort : srcPort.getLinkedPorts()) {
+ ISwitchObject dstSw = dstPort.getSwitch();
+ if(dstSw != null) {
+ Link link = new Link(HexString.toLong(srcSw.getDPID()),
+ srcPort.getNumber(),
+ HexString.toLong(dstSw.getDPID()),
+ dstPort.getNumber());
+ links.add(link);
+ }
+ }
+ }
+ }
+
+ return links;
}
+ /**
+ * Get list of all links whose state is ACTIVE.
+ * @return List of active links. Empty list if no port was found.
+ */
public List<Link> getActiveLinks() {
Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
List<Link> links = new ArrayList<Link>();
- for (ISwitchObject sw : switches) {
- GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
- ExtractLink extractor = new ExtractLink();
-
- pipe.start(sw.asVertex());
- pipe.enablePath(true);
- pipe.out("on").out("link").in("on").path().step(extractor);
+
+ for (ISwitchObject srcSw : switches) {
+ for(IPortObject srcPort : srcSw.getPorts()) {
+ for(IPortObject dstPort : srcPort.getLinkedPorts()) {
+ ISwitchObject dstSw = dstPort.getSwitch();
- while (pipe.hasNext() ) {
- Link l = pipe.next();
- links.add(l);
+ if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
+ links.add(new Link(HexString.toLong(srcSw.getDPID()),
+ srcPort.getNumber(),
+ HexString.toLong(dstSw.getDPID()),
+ dstPort.getNumber()));
+ }
+ }
}
-
}
+
return links;
}
@@ -244,10 +327,16 @@
}
}
+ /**
+ * Finalize the object.
+ */
public void finalize() {
close();
}
+ /**
+ * Close LinkStorage.
+ */
@Override
public void close() {
// TODO Auto-generated method stub
diff --git a/src/test/java/net/floodlightcontroller/core/internal/TestDatabaseManager.java b/src/test/java/net/floodlightcontroller/core/internal/TestDatabaseManager.java
new file mode 100644
index 0000000..8a09139
--- /dev/null
+++ b/src/test/java/net/floodlightcontroller/core/internal/TestDatabaseManager.java
@@ -0,0 +1,76 @@
+package net.floodlightcontroller.core.internal;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.apache.commons.io.FileUtils;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader;
+
+public class TestDatabaseManager {
+ private static final String testDbLocation = "/tmp/onos-testdb";
+
+ //private static final String testDbGraphML = "<?xml version=\"1.0\" ?><graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"> <key id=\"id\" for=\"node\" attr.name=\"id\" attr.type=\"string\"></key> <key id=\"type\" for=\"node\" attr.name=\"type\" attr.type=\"string\"></key> <key id=\"dpid\" for=\"node\" attr.name=\"dpid\" attr.type=\"string\"></key> <key id=\"desc\" for=\"node\" attr.name=\"desc\" attr.type=\"string\"></key> <key id=\"number\" for=\"node\" attr.name=\"number\" attr.type=\"int\"></key> <key id=\"dl_addr\" for=\"node\" attr.name=\"dl_addr\" attr.type=\"string\"></key> <key id=\"nw_addr\" for=\"node\" attr.name=\"nw_addr\" attr.type=\"string\"></key> <key id=\"id\" for=\"edge\" attr.name=\"id\" attr.type=\"string\"></key> <key id=\"source\" for=\"edge\" attr.name=\"source\" attr.type=\"string\"></key> <key id=\"target\" for=\"edge\" attr.name=\"target\" attr.type=\"string\"></key> <key id=\"label\" for=\"edge\" attr.name=\"label\" attr.type=\"string\"></key> <graph id=\"G\" edgedefault=\"directed\"> <node id=\"1\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:01</data> <data key=\"desc\">OpenFlow Switch at SEA</data> </node> <node id=\"2\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:02</data> <data key=\"desc\">OpenFlow Switch at LAX</data> </node> <node id=\"3\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:03</data> <data key=\"desc\">OpenFlow Switch at CHI</data> </node> <node id=\"4\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:04</data> <data key=\"desc\">OpenFlow Switch at IAH</data> </node> <node id=\"5\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:05</data> <data key=\"desc\">OpenFlow Switch at NYC</data> </node> <node id=\"6\"> <data key=\"type\">switch</data> <data key=\"dpid\">00:00:00:00:00:00:0a:06</data> <data key=\"desc\">OpenFlow Switch at ATL</data> </node> <node id=\"100\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at SEA Switch</data> </node> <node id=\"101\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at SEA Switch</data> </node> <node id=\"102\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at SEA Switch</data> </node> <node id=\"103\"> <data key=\"type\">port</data> <data key=\"number\">4</data> <data key=\"desc\">port 4 at SEA Switch</data> </node> <node id=\"104\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at LAX Switch</data> </node> <node id=\"105\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at LAX Switch</data> </node> <node id=\"106\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at LAX Switch</data> </node> <node id=\"107\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at CHI Switch</data> </node> <node id=\"108\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at CHI Switch</data> </node> <node id=\"109\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at CHI Switch</data> </node> <node id=\"110\"> <data key=\"type\">port</data> <data key=\"number\">4</data> <data key=\"desc\">port 4 at CHI Switch</data> </node> <node id=\"111\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at IAH Switch</data> </node> <node id=\"112\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at IAH Switch</data> </node> <node id=\"113\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at IAH Switch</data> </node> <node id=\"114\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at NYC Switch</data> </node> <node id=\"115\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at NYC Switch</data> </node> <node id=\"116\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at NYC Switch</data> </node> <node id=\"117\"> <data key=\"type\">port</data> <data key=\"number\">1</data> <data key=\"desc\">port 1 at ATL Switch</data> </node> <node id=\"118\"> <data key=\"type\">port</data> <data key=\"number\">2</data> <data key=\"desc\">port 2 at ATL Switch</data> </node> <node id=\"119\"> <data key=\"type\">port</data> <data key=\"number\">3</data> <data key=\"desc\">port 3 at ATL Switch</data> </node> <node id=\"1000\"> <data key=\"type\">device</data> <data key=\"dl_addr\">20:c9:d0:4a:e1:73</data> <data key=\"nw_addr\">192.168.10.101</data> </node> <node id=\"1001\"> <data key=\"type\">device</data> <data key=\"dl_addr\">20:c9:d0:4a:e1:62</data> <data key=\"nw_addr\">192.168.20.101</data> </node> <node id=\"1002\"> <data key=\"type\">device</data> <data key=\"dl_addr\">10:40:f3:e6:8d:55</data> <data key=\"nw_addr\">192.168.10.1</data> </node> <node id=\"1003\"> <data key=\"type\">device</data> <data key=\"dl_addr\">a0:b3:cc:9c:c6:88</data> <data key=\"nw_addr\">192.168.20.1</data> </node> <node id=\"1004\"> <data key=\"type\">device</data> <data key=\"dl_addr\">00:04:20:e2:50:a2</data> <data key=\"nw_addr\">192.168.30.1</data> </node> <node id=\"1005\"> <data key=\"type\">device</data> <data key=\"dl_addr\">58:55:ca:c4:1b:a0</data> <data key=\"nw_addr\">192.168.40.1</data> </node> <edge id=\"10000\" source=\"1\" target=\"101\" label=\"on\"></edge> <edge id=\"10001\" source=\"1\" target=\"102\" label=\"on\"></edge> <edge id=\"10002\" source=\"1\" target=\"103\" label=\"on\"></edge> <edge id=\"10003\" source=\"2\" target=\"104\" label=\"on\"></edge> <edge id=\"10004\" source=\"2\" target=\"105\" label=\"on\"></edge> <edge id=\"10005\" source=\"2\" target=\"106\" label=\"on\"></edge> <edge id=\"10006\" source=\"3\" target=\"107\" label=\"on\"></edge> <edge id=\"10007\" source=\"3\" target=\"108\" label=\"on\"></edge> <edge id=\"10008\" source=\"3\" target=\"109\" label=\"on\"></edge> <edge id=\"10009\" source=\"3\" target=\"110\" label=\"on\"></edge> <edge id=\"10010\" source=\"4\" target=\"111\" label=\"on\"></edge> <edge id=\"10011\" source=\"4\" target=\"112\" label=\"on\"></edge> <edge id=\"10012\" source=\"4\" target=\"113\" label=\"on\"></edge> <edge id=\"10013\" source=\"5\" target=\"114\" label=\"on\"></edge> <edge id=\"10014\" source=\"5\" target=\"115\" label=\"on\"></edge> <edge id=\"10015\" source=\"5\" target=\"116\" label=\"on\"></edge> <edge id=\"10016\" source=\"6\" target=\"117\" label=\"on\"></edge> <edge id=\"10017\" source=\"6\" target=\"118\" label=\"on\"></edge> <edge id=\"10018\" source=\"6\" target=\"119\" label=\"on\"></edge> <edge id=\"11000\" source=\"101\" target=\"107\" label=\"link\"></edge> <edge id=\"11003\" source=\"105\" target=\"111\" label=\"link\"></edge> <edge id=\"11004\" source=\"107\" target=\"101\" label=\"link\"></edge> <edge id=\"11005\" source=\"108\" target=\"112\" label=\"link\"></edge> <edge id=\"11006\" source=\"109\" target=\"114\" label=\"link\"></edge> <edge id=\"11007\" source=\"111\" target=\"105\" label=\"link\"></edge> <edge id=\"11008\" source=\"112\" target=\"108\" label=\"link\"></edge> <edge id=\"11009\" source=\"113\" target=\"117\" label=\"link\"></edge> <edge id=\"11010\" source=\"114\" target=\"109\" label=\"link\"></edge> <edge id=\"11011\" source=\"115\" target=\"118\" label=\"link\"></edge> <edge id=\"11012\" source=\"117\" target=\"113\" label=\"link\"></edge> <edge id=\"11013\" source=\"118\" target=\"115\" label=\"link\"></edge> <edge id=\"12000\" source=\"103\" target=\"1000\" label=\"host\"></edge> <edge id=\"12001\" source=\"103\" target=\"1001\" label=\"host\"></edge> <edge id=\"12002\" source=\"110\" target=\"1002\" label=\"host\"></edge> <edge id=\"12003\" source=\"116\" target=\"1003\" label=\"host\"></edge> <edge id=\"12004\" source=\"106\" target=\"1004\" label=\"host\"></edge> <edge id=\"12005\" source=\"119\" target=\"1005\" label=\"host\"></edge> </graph> </graphml>";
+
+ private static final String testDataLocation = "titan/schema/test-network.xml";
+
+ public static TitanGraph getTestDatabase(){
+ //return TitanFactory.open(testDbLocation);
+// return TitanFactory.openInMemoryGraph();
+ return TitanFactory.open(testDbLocation);
+ }
+
+ public static void populateTestData(TitanGraph titanGraph){
+ Set<String> s = titanGraph.getIndexedKeys(Vertex.class);
+ if (!s.contains("dpid")) {
+ titanGraph.createKeyIndex("dpid", Vertex.class);
+ titanGraph.stopTransaction(Conclusion.SUCCESS);
+ }
+ if (!s.contains("type")) {
+ titanGraph.createKeyIndex("type", Vertex.class);
+ titanGraph.stopTransaction(Conclusion.SUCCESS);
+ }
+
+ //InputStream graphMLStream = new ByteArrayInputStream(testDbGraphML.getBytes());
+
+ try {
+ InputStream graphMLStream = new FileInputStream(testDataLocation);
+ GraphMLReader.inputGraph(titanGraph, graphMLStream);
+ } catch (FileNotFoundException e) {
+ Assert.fail("Test data file not found: " + testDataLocation);
+ } catch (IOException e) {
+ e.printStackTrace();
+ Assert.fail("IOException thrown");
+ }
+
+ //Change the type of all port numbers to short in the database
+ Iterator<Vertex> it = titanGraph.getVertices("type", "port").iterator();
+ while (it.hasNext()){
+ Vertex port = it.next();
+ Integer portNum = (Integer) port.getProperty("number");
+ port.setProperty("number", portNum.shortValue());
+ }
+ titanGraph.stopTransaction(Conclusion.SUCCESS);
+ }
+
+ public static void deleteTestDatabase(){
+ try {
+ FileUtils.deleteDirectory(new File(testDbLocation));
+ } catch (IOException e) {
+ System.out.println("delete failed");
+ e.printStackTrace();
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImplTest.java b/src/test/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImplTest.java
index b03af4e..7bf2b1a 100644
--- a/src/test/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImplTest.java
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImplTest.java
@@ -4,125 +4,533 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import java.util.Iterator;
+import java.util.ArrayList;
import java.util.List;
import net.floodlightcontroller.routing.Link;
import net.onrc.onos.ofcontroller.core.ILinkStorage;
import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
+import net.onrc.onos.ofcontroller.core.internal.TestableGraphDBOperation.TestPortObject;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
+import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openflow.protocol.OFPhysicalPort;
+import org.openflow.util.HexString;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-import com.thinkaurelius.titan.core.TitanGraph;
-import com.tinkerpop.blueprints.Vertex;
-import com.tinkerpop.gremlin.java.GremlinPipeline;
-
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({LinkStorageImpl.class, GraphDBConnection.class, GraphDBOperation.class})
public class LinkStorageImplTest {
+ protected static Logger log = LoggerFactory.getLogger(LinkStorageImplTest.class);
+
private static ILinkStorage linkStorage;
- private static TitanGraph titanGraph;
- //TODO Future ideas:
- //Test add links with CREATE and UPDATE
- //Test adding existing link again
+ // Mock GraphDBConnection (do nothing)
+ private static GraphDBConnection conn;
+ // Mock GraphDBOperation (mocks port-related methods only)
+ private static TestableGraphDBOperation ope;
+
+ /**
+ * Setup code called before each tests.
+ * Read test graph data and replace DB access by test graph data.
+ * @throws Exception
+ */
@Before
public void setUp() throws Exception{
- titanGraph = TestDatabaseManager.getTestDatabase();
- TestDatabaseManager.populateTestData(titanGraph);
+ PowerMock.mockStatic(GraphDBConnection.class);
+ PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
+ conn = PowerMock.createNiceMock(GraphDBConnection.class);
+ EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(conn).anyTimes();
+ PowerMock.replay(GraphDBConnection.class);
- linkStorage = new TestableLinkStorageImpl(titanGraph);
+ ope = new TestableGraphDBOperation();
+ PowerMock.expectNew(GraphDBOperation.class, (GraphDBConnection)EasyMock.anyObject()).andReturn(ope).anyTimes();
+ PowerMock.replay(GraphDBOperation.class);
+
+ linkStorage = new LinkStorageImpl();
+ linkStorage.init("/dummy/path/to/conf");
+
+ initLinks();
}
+ /**
+ * Closing code called after each tests.
+ * Discard test graph data.
+ * @throws Exception
+ */
@After
- public void tearDown() throws Exception {
- titanGraph.shutdown();
+ public void tearDown() throws Exception {
+ // finish code
+ linkStorage.close();
+ ope.close();
}
- /*
- * Add a link between port 1.102 and 2.104
- * i.e SEA switch port 3 to LAX switch port 1
+ // TODO: remove @Ignore after UPDATE method is implemented
+ /**
+ * Test if update() can correctly updates LinkInfo for a Link.
+ */
+ @Ignore @Test
+ public void testUpdate_UpdateSingleLink() {
+ Link linkToUpdate= createExistingLink();
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
+
+ linkStorage.update(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
+
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
+ }
+
+ /**
+ * Test if update() can correctly creates a Link.
*/
@Test
- public void testAddSingleLink(){
- Link linkToAdd = new Link(Long.decode("0x0000000000000a01"), 3, Long.decode("0x0000000000000a02"), 1);
+ public void testUpdate_CreateSingleLink() {
+ Link linkToCreate = createFeasibleLink();
+ Link linkToVerify = createFeasibleLink();
//Use the link storage API to add the link
- linkStorage.update(linkToAdd, ILinkStorage.DM_OPERATION.INSERT);
+ linkStorage.update(linkToCreate, ILinkStorage.DM_OPERATION.CREATE);
+ doTestLinkExist(linkToVerify);
+ }
+
+ /**
+ * Test if update() can correctly inserts a Link.
+ */
+ @Test
+ public void testUpdate_InsertSingleLink(){
+ Link linkToInsert = createFeasibleLink();
+ Link linkToVerify = createFeasibleLink();
- //Test if it was added correctly with the Gremlin API
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
- Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
-
- assertTrue(it.hasNext());
- Vertex sw1 = it.next();
- assertFalse(it.hasNext());
-
- pipe.start(sw1).out("on").has("number", (short)3).out("link").in("on");
-
- assertTrue(pipe.hasNext());
- Vertex sw2 = pipe.next();
- assertFalse(pipe.hasNext());
-
- //Check we ended up at the right vertex
- assertEquals((String)sw2.getProperty("dpid"), "00:00:00:00:00:00:0a:02");
+ //Use the link storage API to add the link
+ linkStorage.update(linkToInsert, ILinkStorage.DM_OPERATION.INSERT);
+ doTestLinkExist(linkToVerify);
}
- //TODO enable once method is written
+ /**
+ * Test if update() can correctly deletes a Link.
+ */
+ @Test
+ public void testUpdate_DeleteSingleLink(){
+ Link linkToDelete = createExistingLink();
+ Link linkToVerify = createExistingLink();
+
+ // Test deletion of existing link
+ linkStorage.update(linkToDelete, DM_OPERATION.DELETE);
+ doTestLinkNotExist(linkToVerify);
+ }
+
+ /**
+ * Test if update() can correctly creates multiple Links.
+ */
+ @Test
+ public void testUpdate_CreateLinks(){
+ List<Link> linksToCreate = createFeasibleLinks();
+ List<Link> linksToVerify = createFeasibleLinks();
+
+ // Test creation of new links
+ linkStorage.update(linksToCreate, ILinkStorage.DM_OPERATION.CREATE);
+ for(Link l : linksToVerify) {
+ doTestLinkExist(l);
+ }
+ }
+
+ /**
+ * Test if update() can handle mixture of normal/abnormal input for creation of Links.
+ * Deprecated: DBOperation is responsible.
+ */
@Ignore @Test
- public void testGetLinks(){
- //TODO Make sure this works when the implementation is written
- List<Link> list = linkStorage.getLinks(Long.decode("0x0000000000000a01"), (short)2);
+ public void testUpdate_CreateLinks_Mixuture(){
+ List<Link> linksToCreate = new ArrayList<Link>();
+ linksToCreate.add(createFeasibleLink());
+ linksToCreate.add(createExistingLink());
+
+ // Test creation of mixture of new/existing links
+ linkStorage.update(linksToCreate, ILinkStorage.DM_OPERATION.CREATE);
+ doTestLinkExist(createFeasibleLink());
+ doTestLinkExist(createExistingLink());
+ }
+
+ /**
+ * Test if update() can correctly inserts multiple Links.
+ */
+ @Test
+ public void testUpdate_InsertLinks(){
+ List<Link> linksToInsert = createFeasibleLinks();
+ List<Link> linksToVerify = createFeasibleLinks();
+
+ // Test insertion of new links
+ linkStorage.update(linksToInsert, ILinkStorage.DM_OPERATION.INSERT);
+ for(Link l : linksToVerify) {
+ doTestLinkExist(l);
+ }
+ }
+
+ /**
+ * Test if update() can handle mixture of normal/abnormal input for creation of Links.
+ */
+ @Ignore @Test
+ public void testUpdate_InsertLinks_Mixuture(){
+ List<Link> linksToInsert = new ArrayList<Link>();
+ linksToInsert.add(createFeasibleLink());
+ linksToInsert.add(createExistingLink());
+
+ // Test insertion of mixture of new/existing links
+ linkStorage.update(linksToInsert, ILinkStorage.DM_OPERATION.INSERT);
+ doTestLinkExist(createFeasibleLink());
+ doTestLinkExist(createExistingLink());
+ }
+
+ /**
+ * Test if update() can correctly deletes multiple Links.
+ */
+ @Test
+ public void testUpdate_DeleteLinks(){
+ List<Link> linksToDelete = createExistingLinks();
+ List<Link> linksToVerify = createExistingLinks();
+
+ // Test deletion of existing links
+ linkStorage.update(linksToDelete, ILinkStorage.DM_OPERATION.DELETE);
+ for(Link l : linksToVerify) {
+ doTestLinkNotExist(l);
+ }
+ }
+
+ /**
+ * Test if update() can handle mixture of normal/abnormal input for deletion of Links.
+ */
+ @Ignore @Test
+ public void testUpdate_DeleteLinks_Mixuture(){
+ List<Link> linksToDelete = new ArrayList<Link>();
+ linksToDelete.add(createFeasibleLink());
+ linksToDelete.add(createExistingLink());
+
+ // Test deletion of mixture of new/existing links
+ linkStorage.update(linksToDelete, ILinkStorage.DM_OPERATION.DELETE);
+ doTestLinkNotExist(createFeasibleLink());
+ doTestLinkNotExist(createExistingLink());
+ }
+
+ // TODO: remove @Ignore after UPDATE method is implemented
+ /**
+ * Test if updateLink() can correctly updates LinkInfo for a Link.
+ */
+ @Ignore @Test
+ public void testUpdateLink_Update() {
+ Link linkToUpdate= createExistingLink();
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
+
+ linkStorage.updateLink(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
+
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
+ }
+
+ /**
+ * Test if updateLink() can correctly creates a Link.
+ */
+ @Test
+ public void testUpdateLink_Create() {
+ Link linkToCreate = createFeasibleLink();
+ Link linkToVerify = createFeasibleLink();
+
+ //Use the link storage API to add the link
+ linkStorage.updateLink(linkToCreate, null, ILinkStorage.DM_OPERATION.CREATE);
+ doTestLinkExist(linkToVerify);
+ }
+
+ /**
+ * Test if updateLink() can correctly inserts a Link.
+ */
+ @Test
+ public void testUpdateLink_Insert() {
+ Link linkToInsert = createFeasibleLink();
+ Link linkToVerify = createFeasibleLink();
+
+ //Use the link storage API to add the link
+ linkStorage.updateLink(linkToInsert, null, ILinkStorage.DM_OPERATION.INSERT);
+
+ doTestLinkExist(linkToVerify);
+ }
+
+ // TODO: Check if addOrUpdateLink() should accept DELETE operation. If not, remove this test.
+ /**
+ * Test if updateLink() can correctly deletes a Link.
+ */
+ @Ignore @Test
+ public void testUpdateLink_Delete() {
+ Link linkToDelete = createExistingLink();
+ Link linkToVerify = createExistingLink();
+
+ // Test deletion of existing link
+ linkStorage.updateLink(linkToDelete, null, DM_OPERATION.DELETE);
+ doTestLinkNotExist(linkToVerify);
+
+ linkToDelete = createFeasibleLink();
+ linkToVerify = createFeasibleLink();
+
+ // Test deletion of not-existing link
+ linkStorage.updateLink(linkToDelete, null, DM_OPERATION.DELETE);
+ doTestLinkNotExist(linkToVerify);
+ }
+
+ /**
+ * Test if getLinks() can correctly return Links connected to specific DPID and port.
+ */
+ @Test
+ public void testGetLinks_ByDpidPort(){
+ Link linkToVerify = createExistingLink();
+ Long dpid = linkToVerify.getSrc();
+ short port = (short)linkToVerify.getSrcPort();
+
+ List<Link> list = linkStorage.getLinks(dpid, port);
assertEquals(list.size(), 1);
Link l = list.get(0);
- assertEquals(l.getSrc(), 2561L);
- assertEquals(l.getSrcPort(), (short)2);
- assertEquals(l.getDst(), 2563L);
- assertEquals(l.getDstPort(), (short)1);
+ assertEquals(l.getSrc(), linkToVerify.getSrc());
+ assertEquals(l.getSrcPort(), linkToVerify.getSrcPort());
+ assertEquals(l.getDst(), linkToVerify.getDst());
+ assertEquals(l.getDstPort(), linkToVerify.getDstPort());
+
+ Link linkToVerifyNot = createFeasibleLink();
+
+ List<Link> list2 = linkStorage.getLinks(linkToVerifyNot.getSrc(), (short)linkToVerifyNot.getSrcPort());
+
+ assertEquals(list2.size(), 0);
}
- //TODO enable once method is written
- @Ignore @Test
- public void testUpdateDelete(){
- Link linkToDelete = new Link(Long.decode("0x0000000000000a01"), 2, Long.decode("0x0000000000000a03"), 1);
+ /**
+ * Test if getLinks() can correctly return Links connected to specific MAC address.
+ */
+ @Test
+ public void testGetLinks_ByString() {
+ Link linkToVeryfy = createExistingLink();
+ String dpid = HexString.toHexString(linkToVeryfy.getSrc());
- linkStorage.update(linkToDelete, DM_OPERATION.DELETE);
-
- //Test if it was deleted correctly with the Gremlin API
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
- Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
-
- assertTrue(it.hasNext());
- Vertex sw1 = it.next();
- assertFalse(it.hasNext());
-
- pipe.start(sw1).out("on").has("number", 2).out("link");
-
- assertFalse(pipe.hasNext());
+ List<Link> links = linkStorage.getLinks(dpid);
+ assertTrue(links.contains(linkToVeryfy));
+
+ Link linkToVerifyNot = createFeasibleLink();
+ assertFalse(links.contains(linkToVerifyNot));
}
- //TODO enable once method is written
- @Ignore @Test
+ /**
+ * Test if deleteLink() can correctly delete a Link.
+ */
+ @Test
+ public void testDeleteLink() {
+ // Deletion of existing link
+ Link linkToDelete = createExistingLink();
+ Link linkToVerify = createExistingLink();
+
+ linkStorage.deleteLink(linkToDelete);
+ doTestLinkNotExist(linkToVerify);
+ }
+
+ /**
+ * Test if deleteLinks() can correctly delete Links.
+ */
+ @Test
public void testDeleteLinks(){
- //TODO Make sure this works when the implementation is written
+ List<Link> linksToDelete = createExistingLinks();
+ List<Link> linksToVerify = createExistingLinks();
- linkStorage.deleteLinksOnPort(Long.decode("0x0000000000000a01"), (short)2);
-
- //Test if it was deleted correctly with the Gremlin API
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
- Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
-
- assertTrue(it.hasNext());
- Vertex sw1 = it.next();
- assertFalse(it.hasNext());
-
- pipe.start(sw1).out("on").has("number", 2).out("link");
-
- assertFalse(pipe.hasNext());
+ linkStorage.deleteLinks(linksToDelete);
+ for(Link l : linksToVerify) {
+ doTestLinkNotExist(l);
+ }
}
+ /**
+ * Test if deleteLinks() can handle mixture of normal/abnormal input.
+ */
+ @Ignore @Test
+ public void testDeleteLinks_Mixture(){
+ List<Link> linksToDelete = new ArrayList<Link>();
+ linksToDelete.add(createFeasibleLink());
+ linksToDelete.add(createExistingLink());
+
+ // Test deletion of mixture of new/existing links
+ linkStorage.deleteLinks(linksToDelete);
+ doTestLinkNotExist(createFeasibleLink());
+ doTestLinkNotExist(createExistingLink());
+ }
+
+ /**
+ * Test if getActiveLinks() can correctly return active Links.
+ */
+ @Test
+ public void testGetActiveLinks() {
+ Link existingLink = createExistingLink();
+ Link notExistingLink = createFeasibleLink();
+
+ List<Link> links = linkStorage.getActiveLinks();
+
+ assertTrue(links.contains(existingLink));
+ assertFalse(links.contains(notExistingLink));
+ }
+
+ /**
+ * Test if deleteLinksOnPort() can delete Links.
+ */
+ @Test
+ public void testDeleteLinksOnPort() {
+ Link linkToDelete = createExistingLink();
+ Link linkToVerify = createExistingLink();
+
+ linkStorage.deleteLinksOnPort(linkToDelete.getSrc(), linkToDelete.getSrcPort());
+
+ doTestLinkNotExist(linkToVerify);
+ }
+
+ /**
+ * Test if specific link is existing
+ * @param link
+ */
+ private void doTestLinkExist(Link link) {
+ assertTrue(ope.hasLinkBetween(HexString.toHexString(link.getSrc()),
+ link.getSrcPort(),
+ HexString.toHexString(link.getDst()),
+ link.getDstPort()));
+ }
+
+ /**
+ * Test if titanGraph doesn't have specific link
+ * @param link
+ */
+ private void doTestLinkNotExist(Link link) {
+ assertFalse(ope.hasLinkBetween(HexString.toHexString(link.getSrc()),
+ link.getSrcPort(),
+ HexString.toHexString(link.getDst()),
+ link.getDstPort()));
+ }
+
+ /**
+ * Test if titanGraph has specific Link with specific LinkInfo
+ * @param link
+ */
+ private void doTestLinkHasStateOf(Link link, LinkInfo info) {
+ }
+
+ //----------------- Creation of test data -----------------------
+ // Assume a network shown below.
+ //
+ // [dpid1]--+--[port:1]----[port:1]--+--[dpid2]
+ // | |
+ // +--[port:2] [port:2]--+
+ // |
+ // +--[port:3] [port:1]--+--[dpid3]
+ // | |
+ // +--[port:4]----[port:2]--+
+ //
+ // dpid1 : 00:00:00:00:0a:01
+ // dpid2 : 00:00:00:00:0a:02
+ // dpid3 : 00:00:00:00:0a:03
+
+ private void initLinks() {
+ final String dpid1 = "00:00:00:00:0a:01";
+ final String dpid2 = "00:00:00:00:0a:02";
+ final String dpid3 = "00:00:00:00:0a:03";
+
+ ope.createNewSwitchForTest(dpid1).setStateForTest("ACTIVE");
+ ope.createNewSwitchForTest(dpid2).setStateForTest("ACTIVE");
+ ope.createNewSwitchForTest(dpid3).setStateForTest("ACTIVE");
+
+ TestPortObject ports1 [] = {
+ ope.createNewPortForTest(dpid1, (short)1),
+ ope.createNewPortForTest(dpid1, (short)2),
+ ope.createNewPortForTest(dpid1, (short)3),
+ ope.createNewPortForTest(dpid1, (short)4),
+ };
+
+ TestPortObject ports2 [] = {
+ ope.createNewPortForTest(dpid2, (short)1),
+ ope.createNewPortForTest(dpid2, (short)2),
+ };
+
+ TestPortObject ports3 [] = {
+ ope.createNewPortForTest(dpid3, (short)1),
+ ope.createNewPortForTest(dpid3, (short)2),
+ };
+
+ ope.setLinkBetweenPortsForTest(ports1[0], ports2[0]);
+ ope.setLinkBetweenPortsForTest(ports1[3], ports3[1]);
+ }
+
+ /**
+ * Returns new Link object of existing link
+ * @return new Link object
+ */
+ private Link createExistingLink() {
+ return new Link(Long.decode("0x0000000000000a01"), 1, Long.decode("0x0000000000000a02"), 1);
+ }
+
+ /**
+ * Returns new Link object of not-existing but feasible link
+ * @return new Link object
+ */
+ private Link createFeasibleLink() {
+ return new Link(Long.decode("0x0000000000000a01"), 3, Long.decode("0x0000000000000a03"), 1);
+ }
+
+ // make NO sense while test-network data doesn't define physical network (i.e. any link is feasible)
+ @SuppressWarnings("unused")
+ private Link createInfeasibleLink() {
+ return new Link(Long.decode("0x0000000000000a01"), 1, Long.decode("0x0000000000000a03"), 3);
+ }
+
+ /**
+ * Returns list of Link objects which all has information of existing link in titanGraph
+ * @return ArrayList of new Link objects
+ */
+ private List<Link> createExistingLinks() {
+ List<Link> links = new ArrayList<Link>();
+ links.add(new Link(Long.decode("0x0000000000000a01"), 1, Long.decode("0x0000000000000a02"), 1));
+ links.add(new Link(Long.decode("0x0000000000000a01"), 4, Long.decode("0x0000000000000a03"), 2));
+ return links;
+ }
+
+ /**
+ * Returns list of Link objects which all has information of not-existing but feasible link
+ * @return ArrayList of new Link objects
+ */
+ private List<Link> createFeasibleLinks() {
+ List<Link> links = new ArrayList<Link>();
+ links.add(new Link(Long.decode("0x0000000000000a01"), 2, Long.decode("0x0000000000000a02"), 2));
+ links.add(new Link(Long.decode("0x0000000000000a01"), 3, Long.decode("0x0000000000000a03"), 1));
+ return links;
+ }
+
+ /**
+ * Returns new LinkInfo object with convenient values.
+ * @return LinkInfo object
+ */
+ private LinkInfo createFeasibleLinkInfo(long time) {
+ long time_first = time;
+ long time_last_lldp = time + 50;
+ long time_last_bddp = time + 100;
+ int state_src = OFPhysicalPort.OFPortState.OFPPS_STP_FORWARD.getValue();
+ int state_dst = OFPhysicalPort.OFPortState.OFPPS_STP_LISTEN.getValue();
+
+ return new LinkInfo(time_first,
+ time_last_lldp,
+ time_last_bddp,
+ state_src,
+ state_dst);
+ }
+ //---------------------------------------------------------------
}
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestDatabaseManager.java b/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestDatabaseManager.java
index eea586b..b4cfc31 100644
--- a/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestDatabaseManager.java
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestDatabaseManager.java
@@ -73,4 +73,4 @@
}
}
-}
+}
\ No newline at end of file
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestableGraphDBOperation.java b/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestableGraphDBOperation.java
new file mode 100644
index 0000000..31a4bb0
--- /dev/null
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/internal/TestableGraphDBOperation.java
@@ -0,0 +1,1331 @@
+package net.onrc.onos.ofcontroller.core.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.easymock.EasyMock;
+import org.openflow.util.HexString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.frames.annotations.gremlin.GremlinParam;
+
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
+import net.onrc.onos.ofcontroller.util.FlowEntryId;
+import net.onrc.onos.ofcontroller.util.FlowId;
+import net.onrc.onos.util.GraphDBConnection;
+import net.onrc.onos.util.GraphDBOperation;
+import net.onrc.onos.util.IDBConnection;
+
+/**
+ * Mock class of GraphDBOperation which provides additional setter to construct a graph for test.
+ * This object simply caches parameters set up by override interfaces and reflect them when commit().
+ * *ForTest() methods are exempt from cache, parameters through those methods are reflected soon.
+ * @author Naoki Shiota
+ *
+ */
+public class TestableGraphDBOperation extends GraphDBOperation {
+ protected static Logger log = LoggerFactory.getLogger(TestableGraphDBOperation.class);
+
+ protected List<TestSwitchObject> switches;
+ protected List<TestPortObject> ports;
+ protected List<TestDeviceObject> devices;
+ protected List<TestFlowPath> paths;
+ protected List<TestFlowEntry> entries;
+
+ protected List<TestSwitchObject> switchesToAdd;
+ protected List<TestPortObject> portsToAdd;
+ protected List<TestDeviceObject> devicesToAdd;
+ protected List<TestFlowPath> pathsToAdd;
+ protected List<TestFlowEntry> entriesToAdd;
+
+ protected List<TestSwitchObject> switchesToRemove;
+ protected List<TestPortObject> portsToRemove;
+ protected List<TestDeviceObject> devicesToRemove;
+ protected List<TestFlowPath> pathsToRemove;
+ protected List<TestFlowEntry> entriesToRemove;
+
+ // Testable implementations of INetMapTopologyObject interfaces
+
+ public static class TestSwitchObject implements ISwitchObject {
+ private String state,type,dpid;
+ private List<IPortObject> ports;
+ private List<IDeviceObject> devices;
+ private List<IFlowEntry> entries;
+
+ private String stateToUpdate, typeToUpdate, dpidToUpdate;
+ private List<IPortObject> portsToAdd;
+ private List<IPortObject> portsToRemove;
+
+ public TestSwitchObject() {
+ type = "switch";
+
+ ports = new ArrayList<IPortObject>();
+ portsToAdd = new ArrayList<IPortObject>();
+ portsToRemove = new ArrayList<IPortObject>();
+ devices = new ArrayList<IDeviceObject>();
+ entries = new ArrayList<IFlowEntry>();
+
+ clearUncommitedData();
+ }
+
+ public void commit() {
+ for(IPortObject port : portsToAdd) {
+ ports.add(port);
+ }
+ for(IPortObject port : portsToRemove) {
+ ports.remove(port);
+ }
+ if(stateToUpdate != null) { state = stateToUpdate; }
+ if(typeToUpdate != null) { type = typeToUpdate; }
+ if(dpidToUpdate != null) { dpid = dpidToUpdate; }
+
+ clearUncommitedData();
+ }
+
+ public void rollback() {
+ clearUncommitedData();
+ }
+
+ public void clearUncommitedData() {
+ portsToAdd.clear();
+ portsToRemove.clear();
+ stateToUpdate = typeToUpdate = dpidToUpdate = null;
+ }
+
+ public void setStateForTest(String state) { this.state = state; }
+ public void setTypeForTest(String type) { this.type = type; }
+ public void setDpidForTest(String dpid) { this.dpid = dpid; }
+ public void addPortForTest(TestPortObject port) { ports.add(port); }
+ public void addDeviceForTest(TestDeviceObject dev) { devices.add(dev); }
+ public void addEntryForTest(TestFlowEntry entry) { entries.add(entry); }
+
+ @Override
+ public String getState() { return state; }
+
+ @Override
+ public void setState(String state) { this.stateToUpdate = state; }
+
+ @Override
+ public String getType() { return type ; }
+
+ @Override
+ public void setType(String type) { this.typeToUpdate = type; }
+
+ // Not support for test
+ @Override
+ public Vertex asVertex() { return null; }
+
+ @Override
+ public String getDPID() { return dpid; }
+
+ @Override
+ public void setDPID(String dpid) { this.dpidToUpdate = dpid; }
+
+ @Override
+ public Iterable<IPortObject> getPorts() { return ports; }
+
+ @Override
+ public IPortObject getPort(@GremlinParam("port_num") short port_num) {
+ for(IPortObject port : ports) {
+ if(port.getNumber() == port_num) {
+ return port;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public void addPort(IPortObject port) { portsToAdd.add(port); }
+
+ @Override
+ public void removePort(IPortObject port) { portsToRemove.add(port); }
+
+ @Override
+ public Iterable<IDeviceObject> getDevices() { return devices; }
+
+ @Override
+ public Iterable<IFlowEntry> getFlowEntries() { return entries; }
+ }
+
+ public static class TestPortObject implements IPortObject {
+ private String state,type,desc;
+ private Short number;
+ private Integer port_state;
+ private ISwitchObject sw;
+
+ private List<IPortObject> linkedPorts;
+ private List<IDeviceObject> devices;
+ private List<IFlowEntry> inflows,outflows;
+
+ private String stateToUpdate,typeToUpdate,descToUpdate;
+ private Short numberToUpdate;
+ private Integer port_stateToUpdate;
+
+ private List<IPortObject> linkedPortsToAdd;
+ private List<IPortObject> linkedPortsToRemove;
+ private List<IDeviceObject> devicesToAdd;
+ private List<IDeviceObject> devicesToRemove;
+
+
+ public TestPortObject() {
+ type = "port";
+
+ linkedPorts = new ArrayList<IPortObject>();
+ linkedPortsToAdd = new ArrayList<IPortObject>();
+ linkedPortsToRemove = new ArrayList<IPortObject>();
+ devices = new ArrayList<IDeviceObject>();
+ devicesToAdd = new ArrayList<IDeviceObject>();
+ devicesToRemove = new ArrayList<IDeviceObject>();
+ inflows = new ArrayList<IFlowEntry>();
+ outflows = new ArrayList<IFlowEntry>();
+
+ clearUncommitedData();
+ }
+
+ public void commit() {
+ for(IPortObject port : linkedPortsToAdd) { linkedPorts.add(port); }
+ for(IPortObject port : linkedPortsToRemove) { linkedPorts.remove(port); }
+ for(IDeviceObject dev : devicesToAdd) { devices.add(dev); }
+ for(IDeviceObject dev : devicesToRemove) { devices.remove(dev); }
+
+ if(stateToUpdate != null) { state = stateToUpdate; }
+ if(typeToUpdate != null) { type = typeToUpdate; }
+ if(descToUpdate != null) { desc = descToUpdate; }
+ if(numberToUpdate != null) { number = numberToUpdate; }
+ if(port_stateToUpdate != null) { port_state = port_stateToUpdate; }
+
+ clearUncommitedData();
+ }
+
+ public void rollback() {
+ clearUncommitedData();
+ }
+
+ public void clearUncommitedData() {
+ linkedPortsToAdd.clear();
+ linkedPortsToRemove.clear();
+ devicesToAdd.clear();
+ devicesToRemove.clear();
+ stateToUpdate = typeToUpdate = descToUpdate = null;
+ port_stateToUpdate = null;
+ numberToUpdate = null;
+ }
+
+ // Setter methods for test
+ public void setStateForTest(String state) { this.state = state; }
+ public void setTypeForTest(String type) { this.type = type; }
+ public void setDescForTest(String desc) { this.desc = desc; }
+ public void setNumberForTest(Short number) { this.number = number; }
+ public void setPortStateForTest(Integer state) { this.port_state = state; }
+ public void setSwitchForTest(ISwitchObject sw) { this.sw = sw; }
+ public void addLinkedPortForTest(TestPortObject port) { this.linkedPorts.add(port); }
+ public void addInflowForTest(TestFlowEntry entry) { inflows.add(entry); }
+ public void addOutflowForTest(TestFlowEntry entry) { outflows.add(entry); }
+
+ // Override methods for mock IPortObject
+ @Override
+ public String getState() { return state; }
+
+ @Override
+ public void setState(String state) { this.stateToUpdate = state; }
+
+ @Override
+ public String getType() { return type; }
+
+ @Override
+ public void setType(String type) { this.typeToUpdate = type; }
+
+ // not support for test
+ @Override
+ public Vertex asVertex() {
+ return null;
+ }
+
+ @Override
+ public Short getNumber() { return number; }
+
+ @Override
+ public void setNumber(Short n) { this.numberToUpdate = n; }
+
+ @Override
+ public String getDesc() { return desc; }
+
+ @Override
+ public void setDesc(String s) { this.descToUpdate = s; }
+
+ @Override
+ public Integer getPortState() { return port_state; }
+
+ @Override
+ public void setPortState(Integer s) { this.port_stateToUpdate = s; }
+
+ @Override
+ public ISwitchObject getSwitch() { return sw; }
+
+ @Override
+ public Iterable<IDeviceObject> getDevices() { return devices; }
+
+ @Override
+ public void setDevice(IDeviceObject device) { devicesToAdd.add(device); }
+
+ @Override
+ public void removeDevice(IDeviceObject device) { devicesToRemove.add(device); }
+
+ @Override
+ public Iterable<IFlowEntry> getInFlowEntries() { return inflows; }
+
+ @Override
+ public Iterable<IFlowEntry> getOutFlowEntries() { return outflows; }
+
+ @Override
+ public Iterable<IPortObject> getLinkedPorts() { return linkedPorts; }
+
+ @Override
+ public void removeLink(IPortObject dest_port) { linkedPortsToRemove.add(dest_port); }
+
+ @Override
+ public void setLinkPort(IPortObject dest_port) { linkedPortsToAdd.add(dest_port); }
+ }
+
+ public static class TestDeviceObject implements IDeviceObject {
+ private String state,type,mac,ipaddr;
+ private List<IPortObject> ports;
+ private List<ISwitchObject> switches;
+
+ private String stateToUpdate,typeToUpdate,macToUpdate,ipaddrToUpdate;
+ private List<IPortObject> portsToAdd;
+ private List<IPortObject> portsToRemove;
+
+ public TestDeviceObject() {
+ type = "device";
+
+ ports = new ArrayList<IPortObject>();
+ portsToAdd = new ArrayList<IPortObject>();
+ portsToRemove = new ArrayList<IPortObject>();
+ switches = new ArrayList<ISwitchObject>();
+
+ clearUncommitedData();
+ }
+
+ public void commit() {
+ for(IPortObject port : portsToAdd) {
+ ports.add(port);
+ }
+ for(IPortObject port : portsToRemove) {
+ ports.remove(port);
+ }
+
+ if(stateToUpdate != null) { state = stateToUpdate; }
+ if(typeToUpdate != null) { type = typeToUpdate; }
+ if(macToUpdate != null) { mac = macToUpdate; }
+ if(ipaddrToUpdate != null) { ipaddr = ipaddrToUpdate; }
+
+ clearUncommitedData();
+ }
+
+ public void rollback() {
+ clearUncommitedData();
+ }
+
+ public void clearUncommitedData() {
+ ports.clear();
+ portsToAdd.clear();
+ portsToRemove.clear();
+
+ stateToUpdate = typeToUpdate = macToUpdate = ipaddrToUpdate = null;
+ }
+
+ // Setter methods for test
+ public void setStateForTest(String state) { this.state = state; }
+ public void setTypeForTest(String type) { this.type = type; }
+ public void setMacForTest(String mac) { this.mac = mac; }
+ public void setIpaddrForTest(String ipaddr) { this.ipaddr = ipaddr; }
+ public void addSwitchForTest(ISwitchObject sw) { switches.add(sw); }
+ public void addPortForTest(IPortObject port) { ports.add(port); }
+
+
+ // Override methods
+ @Override
+ public String getState() { return state; }
+
+ @Override
+ public void setState(String state) { stateToUpdate = state; }
+
+ @Override
+ public String getType() { return type; }
+
+ @Override
+ public void setType(String type) { typeToUpdate = type; }
+
+ @Override
+ public Vertex asVertex() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getMACAddress() { return mac; }
+
+ @Override
+ public void setMACAddress(String macaddr) { macToUpdate = macaddr; }
+
+ @Override
+ public String getIPAddress() { return ipaddr; }
+
+ @Override
+ public void setIPAddress(String ipaddr) { ipaddrToUpdate = ipaddr; }
+
+ @Override
+ public Iterable<IPortObject> getAttachedPorts() {
+ return ports; }
+
+ @Override
+ public void setHostPort(IPortObject port) { portsToAdd.add(port); }
+
+ @Override
+ public void removeHostPort(IPortObject port) { portsToRemove.add(port); }
+
+ @Override
+ public Iterable<ISwitchObject> getSwitch() { return switches; }
+ }
+
+ public static class TestFlowPath implements IFlowPath {
+ private String state,type,flowId,installerId,srcSw,dstSw;
+ private String dataPathSummary,userState;
+ private String matchSrcMac,matchDstMac,matchSrcIpaddr,matchDstIpaddr;
+ private Short srcPort,dstPort,matchEthernetFrameType;
+
+ private List<IFlowEntry> entries;
+ private List<ISwitchObject> switches;
+
+ private String stateToUpdate,typeToUpdate,flowIdToUpdate,installerIdToUpdate,srcSwToUpdate,dstSwToUpdate;
+ private String dataPathSummaryToUpdate,userStateToUpdate;
+ private String matchSrcMacToUpdate,matchDstMacToUpdate,matchSrcIpaddrToUpdate,matchDstIpaddrToUpdate;
+ private Short srcPortToUpdate,dstPortToUpdate,matchEthernetFrameTypeToUpdate;
+
+ private List<IFlowEntry> flowsToAdd;
+ private List<IFlowEntry> flowsToRemove;
+
+ public TestFlowPath() {
+ type = "flow";
+
+ entries = new ArrayList<IFlowEntry>();
+ flowsToAdd = new ArrayList<IFlowEntry>();
+ flowsToRemove = new ArrayList<IFlowEntry>();
+
+ switches = new ArrayList<ISwitchObject>();
+
+ clear();
+ }
+
+ public void commit() {
+ for(IFlowEntry flow : flowsToAdd) {
+ entries.add(flow);
+ }
+ for(IFlowEntry flow : flowsToRemove) {
+ entries.remove(flow);
+ }
+ if(stateToUpdate != null) { state = stateToUpdate; }
+ if(typeToUpdate != null) { type = typeToUpdate; }
+ if(flowIdToUpdate != null) { flowId = flowIdToUpdate; }
+ if(installerIdToUpdate != null) { installerId = installerIdToUpdate; }
+ if(srcSwToUpdate != null) { srcSw = srcSwToUpdate; }
+ if(dstSwToUpdate != null) { dstSw = dstSwToUpdate; }
+ if(dataPathSummaryToUpdate != null) { dataPathSummary = dataPathSummaryToUpdate; }
+ if(userStateToUpdate != null) { userState = userStateToUpdate; }
+ if(matchSrcMacToUpdate != null) { matchSrcMac = matchSrcMacToUpdate; }
+ if(matchDstMacToUpdate != null) { matchDstMac = matchDstMacToUpdate; }
+ if(matchSrcIpaddrToUpdate != null) { matchSrcIpaddr = matchSrcIpaddrToUpdate; }
+ if(matchDstIpaddrToUpdate != null) { matchDstIpaddr = matchDstIpaddrToUpdate; }
+ if(srcPortToUpdate != null) { srcPort = srcPortToUpdate; }
+ if(dstPortToUpdate != null) { dstPort = dstPortToUpdate; }
+ if(matchEthernetFrameTypeToUpdate != null) { matchEthernetFrameType = matchEthernetFrameTypeToUpdate; }
+ }
+
+ public void rollback() {
+ clear();
+ }
+
+ public void clear() {
+ flowsToAdd.clear();
+ flowsToRemove.clear();
+
+ stateToUpdate = typeToUpdate = flowIdToUpdate = installerIdToUpdate = null;
+ srcSwToUpdate = dstSwToUpdate = dataPathSummaryToUpdate = userStateToUpdate = null;
+ matchSrcMacToUpdate = matchDstMacToUpdate = matchSrcIpaddrToUpdate = matchDstIpaddrToUpdate = null;
+ srcPortToUpdate = dstPortToUpdate = matchEthernetFrameTypeToUpdate = null;
+ }
+
+ // Setter methods for test
+ public void setStateForTest(String state) { this.state = state; }
+ public void setTypeForTest(String type) { this.type = type; }
+ public void setFlowIdForTest(String flowId) { this.flowId = flowId; }
+ public void setInstallerIdForTest(String installerId) { this.installerId = installerId; }
+ public void setSrcSwForTest(String srcSw) { this.srcSw = srcSw; }
+ public void setDstSwForTest(String dstSw) { this.dstSw = dstSw; }
+ public void setDataPathSummaryForTest(String dataPathSummary) { this.dataPathSummary = dataPathSummary; }
+ public void setUserStateForTest(String userState) { this.userState = userState; }
+ public void setMatchSrcMacForTest(String matchSrcMac) { this.matchSrcMac = matchSrcMac; }
+ public void setMatchDstMacForTest(String matchDstMac) { this.matchDstMac = matchDstMac; }
+ public void setMatchSrcIpaddrForTest(String matchSrcIpaddr) { this.matchSrcIpaddr = matchSrcIpaddr; }
+ public void setMatchDstIpaddrForTest(String matchDstIpaddr) { this.matchDstIpaddr = matchDstIpaddr; }
+ public void setSrcPortForTest(Short srcPort) { this.srcPort = srcPort; }
+ public void setDstPortForTest(Short dstPort) { this.dstPort = dstPort; }
+ public void setMatchEthernetFrameTypeForTest(Short matchEthernetFrameType) { this.matchEthernetFrameType = matchEthernetFrameType; }
+ public void addFlowEntryForTest(IFlowEntry entry) { entries.add(entry); }
+ public void addSwitchForTest(ISwitchObject sw) { switches.add(sw); }
+
+ @Override
+ public String getState() { return state; }
+
+ @Override
+ public void setState(String state) { stateToUpdate = state; }
+
+ @Override
+ public String getType() { return type; }
+
+ @Override
+ public void setType(String type) { typeToUpdate = type; }
+
+ @Override
+ public Vertex asVertex() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getFlowId() { return flowId; }
+
+ @Override
+ public void setFlowId(String flowId) { flowIdToUpdate = flowId; }
+
+ @Override
+ public String getInstallerId() { return installerId; }
+
+ @Override
+ public void setInstallerId(String installerId) { installerIdToUpdate = installerId; }
+
+ @Override
+ public String getSrcSwitch() { return srcSw; }
+
+ @Override
+ public void setSrcSwitch(String srcSwitch) { srcSwToUpdate = srcSwitch; }
+
+ @Override
+ public Short getSrcPort() { return srcPort; }
+
+ @Override
+ public void setSrcPort(Short srcPort) { srcPortToUpdate = srcPort; }
+
+ @Override
+ public String getDstSwitch() { return dstSw; }
+
+ @Override
+ public void setDstSwitch(String dstSwitch) { dstSwToUpdate = dstSwitch; }
+
+ @Override
+ public Short getDstPort() { return dstPort; }
+
+ @Override
+ public void setDstPort(Short dstPort) { dstPortToUpdate = dstPort; }
+
+ @Override
+ public String getDataPathSummary() { return dataPathSummary; }
+
+ @Override
+ public void setDataPathSummary(String dataPathSummary) { dataPathSummaryToUpdate = dataPathSummary; }
+
+ @Override
+ public Iterable<IFlowEntry> getFlowEntries() { return entries; }
+
+ @Override
+ public void addFlowEntry(IFlowEntry flowEntry) {
+ if(! entries.contains(flowEntry)) {
+ flowsToAdd.add(flowEntry);
+ }
+ }
+
+ @Override
+ public void removeFlowEntry(IFlowEntry flowEntry) {
+ if(entries.contains(flowEntry)) {
+ flowsToAdd.add(flowEntry);
+ }
+ }
+
+ @Override
+ public Short getMatchEthernetFrameType() { return matchEthernetFrameType; }
+
+ @Override
+ public void setMatchEthernetFrameType(Short matchEthernetFrameType) {
+ matchEthernetFrameTypeToUpdate = matchEthernetFrameType; }
+
+ @Override
+ public String getMatchSrcMac() { return matchSrcMac; }
+
+ @Override
+ public void setMatchSrcMac(String matchSrcMac) { matchSrcMacToUpdate = matchSrcMac; }
+
+ @Override
+ public String getMatchDstMac() { return matchDstMac; }
+
+ @Override
+ public void setMatchDstMac(String matchDstMac) { matchDstMacToUpdate = matchDstMac; }
+
+ @Override
+ public String getMatchSrcIPv4Net() { return matchSrcIpaddr; }
+
+ @Override
+ public void setMatchSrcIPv4Net(String matchSrcIPv4Net) {
+ matchSrcIpaddrToUpdate = matchSrcIPv4Net; }
+
+ @Override
+ public String getMatchDstIPv4Net() { return matchDstIpaddr; }
+
+ @Override
+ public void setMatchDstIPv4Net(String matchDstIPv4Net) {
+ matchDstIpaddrToUpdate = matchDstIPv4Net; }
+
+ @Override
+ public Iterable<ISwitchObject> getSwitches() { return switches; }
+
+ @Override
+ public String getUserState() { return userState; }
+
+ @Override
+ public void setUserState(String userState) { userStateToUpdate = userState; }
+ }
+
+ public static class TestFlowEntry implements IFlowEntry {
+ private String state,type,entryId,dpid,userState,switchState,errorStateType,errorStateCode;
+ private String matchSrcMac,matchDstMac,matchSrcIpaddr,matchDstIpaddr;
+ private Short matchInPort,matchEtherFrameType,actionOutput;
+
+ private IFlowPath flowPath;
+ private ISwitchObject sw;
+ private IPortObject inport,outport;
+
+ private String stateToUpdate,typeToUpdate,entryIdToUpdate,dpidToUpdate,
+ userStateToUpdate,switchStateToUpdate,errorStateTypeToUpdate,errorStateCodeToUpdate;
+ private String matchSrcMacToUpdate,matchDstMacToUpdate,matchSrcIpaddrToUpdate,matchDstIpaddrToUpdate;
+
+ private Short matchInPortToUpdate,matchEtherFrameTypeToUpdate,actionOutputToUpdate;
+
+ private IFlowPath flowPathToUpdate;
+ private ISwitchObject swToUpdate;
+ private IPortObject inportToUpdate,outportToUpdate;
+
+ public TestFlowEntry() {
+ type = "flow_entry";
+
+ clearUncommitedData();
+ }
+
+ public void commit() {
+ if(stateToUpdate != null) { state = stateToUpdate; }
+ if(typeToUpdate != null) { type = typeToUpdate; }
+ if(entryIdToUpdate != null) { entryId = entryIdToUpdate; }
+ if(dpidToUpdate != null) { dpid = dpidToUpdate; }
+ if(userStateToUpdate != null) { userState = userStateToUpdate; }
+ if(switchStateToUpdate != null) { switchState = switchStateToUpdate; }
+ if(errorStateTypeToUpdate != null) { errorStateType = errorStateTypeToUpdate; }
+ if(errorStateCodeToUpdate != null) { errorStateCode = errorStateCodeToUpdate; }
+ if(matchSrcMacToUpdate != null) { matchSrcMac = matchSrcMacToUpdate; }
+ if(matchDstMacToUpdate != null) { matchDstMac = matchDstMacToUpdate; }
+ if(matchSrcIpaddrToUpdate != null) { matchSrcIpaddr = matchSrcIpaddrToUpdate; }
+ if(matchDstIpaddrToUpdate != null) { matchDstIpaddr = matchDstIpaddrToUpdate; }
+ if(matchInPortToUpdate != null) { matchInPort = matchInPortToUpdate; }
+ if(matchEtherFrameTypeToUpdate != null) { matchEtherFrameType = matchEtherFrameTypeToUpdate; }
+ if(actionOutputToUpdate != null) { actionOutput = actionOutputToUpdate; }
+
+ if(flowPathToUpdate != null) { flowPath = flowPathToUpdate; }
+ if(swToUpdate != null) { sw = swToUpdate; }
+ if(inportToUpdate != null) { inport = inportToUpdate; }
+ if(outportToUpdate != null) { outport = outportToUpdate; }
+
+ clearUncommitedData();
+ }
+
+ public void rollback() {
+ clearUncommitedData();
+ }
+
+ public void clearUncommitedData() {
+ stateToUpdate = typeToUpdate = entryIdToUpdate = dpidToUpdate = null;
+ userStateToUpdate = switchStateToUpdate = errorStateTypeToUpdate = errorStateCodeToUpdate = null;
+ matchSrcMacToUpdate = matchDstMacToUpdate = matchSrcIpaddrToUpdate = matchDstIpaddrToUpdate = null;
+ matchInPortToUpdate = matchEtherFrameTypeToUpdate = actionOutputToUpdate = null;
+ flowPathToUpdate = null;
+ swToUpdate = null;
+ inportToUpdate = outportToUpdate = null;
+ }
+
+ // Setter methods for test
+ public void setStateForTest(String state) { this.state = state; }
+ public void setTypeForTest(String type) { this.type = type; }
+ public void setEntryIdForTest(String entryId) { this.entryId = entryId; }
+ public void setDpidForTest(String dpid) { this.dpid = dpid; }
+ public void setUserStateForTest(String userState) { this.userState = userState; }
+ public void setSwitchStateForTest(String switchState) { this.switchState = switchState; }
+ public void setErrorStateTypeForTest(String errorStateType) { this.errorStateType = errorStateType; }
+ public void setErrorStateCodeForTest(String errorStateCode) { this.errorStateCode = errorStateCode; }
+ public void setMatchSrcMacForTest(String matchSrcMac) { this.matchSrcMac = matchSrcMac; }
+ public void setMatchDstMacForTest(String matchDstMac) { this.matchDstMac = matchDstMac; }
+ public void setMatchSrcIpaddrForTest(String matchSrcIpaddr) { this.matchSrcIpaddr = matchSrcIpaddr; }
+ public void setMatchDstIpaddrForTest(String matchDstIpaddr) { this.matchDstIpaddr = matchDstIpaddr; }
+ public void setMatchInPortForTest(Short matchInPort) { this.matchInPort = matchInPort; }
+ public void setMatchEtherFrameTypeForTest(Short matchEtherFrameType) { this.matchEtherFrameType = matchEtherFrameType; }
+ public void setActionOutputForTest(Short actionOutput) { this.actionOutput = actionOutput; }
+ public void setFlowPathForTest(IFlowPath flowPath) { this.flowPath = flowPath; }
+ public void setSwitchForTest(ISwitchObject sw) { this.sw = sw; }
+ public void setInportForTest(IPortObject inport) { this.inport = inport; }
+ public void setOutportForTest(IPortObject outport) { this.outport = outport; }
+
+ @Override
+ public String getState() { return state; }
+
+ @Override
+ public void setState(String state) { stateToUpdate = state; }
+
+ @Override
+ public String getType() { return type; }
+
+ @Override
+ public void setType(String type) { typeToUpdate = type; }
+
+ @Override
+ public Vertex asVertex() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getFlowEntryId() { return entryId; }
+
+ @Override
+ public void setFlowEntryId(String flowEntryId) { entryIdToUpdate = flowEntryId; }
+
+ @Override
+ public String getSwitchDpid() { return dpid; }
+
+ @Override
+ public void setSwitchDpid(String switchDpid) { dpidToUpdate = switchDpid; }
+
+ @Override
+ public String getUserState() { return userState; }
+
+ @Override
+ public void setUserState(String userState) { userStateToUpdate = userState; }
+
+ @Override
+ public String getSwitchState() { return switchState; }
+
+ @Override
+ public void setSwitchState(String switchState) { switchStateToUpdate = switchState; }
+
+ @Override
+ public String getErrorStateType() { return errorStateType; }
+
+ @Override
+ public void setErrorStateType(String errorStateType) { errorStateTypeToUpdate = errorStateType; }
+
+ @Override
+ public String getErrorStateCode() { return errorStateCode; }
+
+ @Override
+ public void setErrorStateCode(String errorStateCode) { errorStateCodeToUpdate = errorStateCode; }
+
+ @Override
+ public Short getMatchInPort() { return matchInPort; }
+
+ @Override
+ public void setMatchInPort(Short matchInPort) { matchInPortToUpdate = matchInPort; }
+
+ @Override
+ public Short getMatchEthernetFrameType() {return matchEtherFrameType; }
+
+ @Override
+ public void setMatchEthernetFrameType(Short matchEthernetFrameType) { matchEtherFrameTypeToUpdate = matchEthernetFrameType; }
+
+ @Override
+ public String getMatchSrcMac() { return matchSrcMac; }
+
+ @Override
+ public void setMatchSrcMac(String matchSrcMac) { matchSrcMacToUpdate = matchSrcMac; }
+
+ @Override
+ public String getMatchDstMac() { return matchDstMac; }
+
+ @Override
+ public void setMatchDstMac(String matchDstMac) { matchDstMacToUpdate = matchDstMac; }
+
+ @Override
+ public String getMatchSrcIPv4Net() { return matchSrcIpaddr; }
+
+ @Override
+ public void setMatchSrcIPv4Net(String matchSrcIPv4Net) { matchSrcIpaddrToUpdate = matchSrcIPv4Net; }
+
+ @Override
+ public String getMatchDstIPv4Net() { return matchDstIpaddr; }
+
+ @Override
+ public void setMatchDstIPv4Net(String matchDstIPv4Net) { matchDstIpaddrToUpdate = matchDstIPv4Net; }
+
+ @Override
+ public Short getActionOutput() { return actionOutput; }
+
+ @Override
+ public void setActionOutput(Short actionOutput) { actionOutputToUpdate = actionOutput; }
+
+ @Override
+ public IFlowPath getFlow() { return flowPath; }
+
+ @Override
+ public void setFlow(IFlowPath flow) { flowPathToUpdate = flow; }
+
+ @Override
+ public ISwitchObject getSwitch() { return sw; }
+
+ @Override
+ public void setSwitch(ISwitchObject sw) { swToUpdate = sw; }
+
+ @Override
+ public IPortObject getInPort() { return inport; }
+
+ @Override
+ public void setInPort(IPortObject port) { inportToUpdate = port; }
+
+ @Override
+ public IPortObject getOutPort() { return outport; }
+
+ @Override
+ public void setOutPort(IPortObject port) { outportToUpdate = port; }
+ }
+
+
+ public TestableGraphDBOperation() {
+ super(EasyMock.createNiceMock(GraphDBConnection.class));
+
+ switches = new ArrayList<TestSwitchObject>();
+ ports = new ArrayList<TestPortObject>();
+ devices = new ArrayList<TestDeviceObject>();
+ paths = new ArrayList<TestFlowPath>();
+ entries = new ArrayList<TestFlowEntry>();
+
+ switchesToAdd = new ArrayList<TestSwitchObject>();
+ portsToAdd = new ArrayList<TestPortObject>();
+ devicesToAdd = new ArrayList<TestDeviceObject>();
+ pathsToAdd = new ArrayList<TestFlowPath>();
+ entriesToAdd = new ArrayList<TestFlowEntry>();
+
+ switchesToRemove = new ArrayList<TestSwitchObject>();
+ portsToRemove = new ArrayList<TestPortObject>();
+ devicesToRemove = new ArrayList<TestDeviceObject>();
+ pathsToRemove = new ArrayList<TestFlowPath>();
+ entriesToRemove = new ArrayList<TestFlowEntry>();
+
+ clearUncommitedData();
+ }
+
+ private void clearUncommitedData() {
+ for(TestFlowEntry flow : entries) {
+ flow.clearUncommitedData();
+ }
+ for(TestFlowEntry flow : entriesToAdd) {
+ flow.clearUncommitedData();
+ }
+
+ for(TestDeviceObject dev : devices) {
+ dev.clearUncommitedData();
+ }
+ for(TestDeviceObject dev : devicesToAdd) {
+ dev.clearUncommitedData();
+ }
+
+ for(TestSwitchObject sw : switches) {
+ sw.clearUncommitedData();
+ }
+ for(TestSwitchObject sw : switchesToAdd) {
+ sw.clearUncommitedData();
+ }
+
+ for(TestPortObject port : ports) {
+ port.clearUncommitedData();
+ }
+ for(TestPortObject port : portsToAdd) {
+ port.clearUncommitedData();
+ }
+
+ entriesToAdd.clear();
+ entriesToRemove.clear();
+ devicesToAdd.clear();
+ devicesToRemove.clear();
+ switchesToAdd.clear();
+ switchesToRemove.clear();
+ portsToAdd.clear();
+ portsToRemove.clear();
+ }
+
+
+ // this.*ForTest() methods below are supposed to be used for creation of test topology.
+ /**
+ * Create new empty TestSwitchObject.
+ * @return New TestSwitchObject
+ */
+ public TestSwitchObject createNewSwitchForTest() {
+ TestSwitchObject sw = new TestSwitchObject();
+ switches.add(sw);
+ return sw;
+ }
+
+ /**
+ * Create new TestSwitchObject with specific DPID.
+ * @param dpid DPID to be set
+ * @return New TestSwitchObject
+ */
+ public TestSwitchObject createNewSwitchForTest(String dpid) {
+ for(TestSwitchObject sw_loop : switches) {
+ if(sw_loop.getDPID().equals(dpid)) {
+ // Already created
+ log.error("switch already exists : " + dpid);
+ return sw_loop;
+ }
+ }
+
+ TestSwitchObject sw = new TestSwitchObject();
+
+ sw.setDpidForTest(dpid);
+ switches.add(sw);
+
+ return sw;
+ }
+
+ /**
+ * Create new empty TestPortObject.
+ * @return New TestPortObject
+ */
+ public TestPortObject createNewPortForTest() {
+ TestPortObject port = new TestPortObject();
+ ports.add(port);
+ return port;
+ }
+
+ /**
+ * Create new TestPortObject with specific DPID and port number.
+ * @param dpid DPID to be set
+ * @param number Port number to be set
+ * @return New TestPortObject
+ */
+ public TestPortObject createNewPortForTest(String dpid, Short number) {
+ TestSwitchObject sw = null;
+
+ for(TestSwitchObject sw_loop : switches) {
+ if(sw_loop.getDPID().equals(dpid)) {
+ sw = sw_loop;
+ }
+ }
+
+ if(sw != null) {
+ TestPortObject port = new TestPortObject();
+ port.setNumberForTest(number);
+ port.setSwitchForTest(sw);
+ sw.addPortForTest(port);
+
+ ports.add(port);
+
+ return port;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Link a TestPortObject to other TestPortObject.
+ * @param src TestPortObjecgt of source port.
+ * @param dst TestPortObjecgt of destination port.
+ */
+ public void setLinkBetweenPortsForTest(TestPortObject src, TestPortObject dst) {
+ src.addLinkedPortForTest(dst);
+ }
+
+ /**
+ * Create new empty TestDeviceObject.
+ * @return New TestDeviceObject
+ */
+ public TestDeviceObject createNewDeviceForTest() {
+ TestDeviceObject dev = new TestDeviceObject();
+
+ return dev;
+ }
+
+ /**
+ * Create new empty TestFlowPathObject.
+ * @return New TestFlowPathObject
+ */
+ public TestFlowPath createNewFlowPathForTest() {
+ TestFlowPath path = new TestFlowPath();
+ paths.add(path);
+ return path;
+ }
+
+ /**
+ * Create new empty TestFlowEntryObject.
+ * @return New TestFlowEntryObject
+ */
+ public TestFlowEntry createNewFlowEntryForTest() {
+ TestFlowEntry entry = new TestFlowEntry();
+ entries.add(entry);
+ return entry;
+ }
+
+
+ public boolean hasLinkBetween(String srcSw_str, Short srcNumber, String dstSw_str, Short dstNumber) {
+ IPortObject srcPort = null, dstPort = null;
+ long srcSw = HexString.toLong(srcSw_str);
+ long dstSw = HexString.toLong(dstSw_str);
+
+ for(TestSwitchObject sw : switches) {
+ long swLong = HexString.toLong(sw.getDPID());
+ if(swLong == srcSw) {
+ for(IPortObject port : sw.getPorts()) {
+ if(port.getNumber().equals(srcNumber)) {
+ srcPort = port;
+ }
+ }
+ } else if(swLong == dstSw) {
+ for(IPortObject port : sw.getPorts()) {
+ if(port.getNumber().equals(dstNumber)) {
+ dstPort = port;
+ }
+ }
+ }
+ }
+
+ if(srcPort != null && dstPort != null) {
+ for(IPortObject port : srcPort.getLinkedPorts()) {
+ if(port.equals(dstPort)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ // Overriding methods below are to mock GraphDBOperation class.
+ @Override
+ public ISwitchObject newSwitch(String dpid) {
+ TestSwitchObject sw = new TestSwitchObject();
+ sw.setDPID(dpid);
+ switchesToAdd.add(sw);
+
+ return sw;
+ }
+
+ @Override
+ public ISwitchObject searchSwitch(String dpid_str) {
+ Long dpid = HexString.toLong(dpid_str);
+
+ for(ISwitchObject sw : switches) {
+ if(HexString.toLong(sw.getDPID()) == dpid) {
+ return sw;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public ISwitchObject searchActiveSwitch(String dpid_str) {
+ Long dpid = HexString.toLong(dpid_str);
+
+ for(ISwitchObject sw : switches) {
+ if(HexString.toLong(sw.getDPID()) == dpid && sw.getState().equals("ACTIVE")) {
+ return sw;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getActiveSwitches() {
+ List<ISwitchObject> list = new ArrayList<ISwitchObject>();
+
+ for(ISwitchObject sw : switches) {
+ if(sw.getState() != null && sw.getState().equals("ACTIVE")) {
+ list.add(sw);
+ }
+ }
+ return list.isEmpty() ? null : list;
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getAllSwitches() {
+ List<ISwitchObject> list = new ArrayList<ISwitchObject>();
+
+ for(ISwitchObject sw : switches) {
+ list.add(sw);
+ }
+
+ return list.isEmpty() ? null : list;
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getInactiveSwitches() {
+ List<ISwitchObject> list = new ArrayList<ISwitchObject>();
+
+ for(ISwitchObject sw : switches) {
+ if(! sw.getState().equals("ACTIVE")) {
+ list.add(sw);
+ }
+ }
+ return list.isEmpty() ? null : list;
+ }
+
+ @Override
+ public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
+ List<IFlowEntry> list = new ArrayList<IFlowEntry>();
+
+ for(TestFlowEntry entry : entries) {
+ if(entry.getSwitchState().equals("FE_SWITCH_NOT_UPDATED")) {
+ list.add(entry);
+ }
+ }
+ return list;
+ }
+
+ @Override
+ public void removeSwitch(ISwitchObject sw) {
+ if(switches.contains(sw)) {
+ switchesToRemove.add((TestSwitchObject)sw);
+ }
+ }
+
+ @Override
+ public IPortObject newPort(Short portNumber) {
+ TestPortObject port = new TestPortObject();
+ port.setNumber(portNumber);
+
+ return port;
+ }
+
+ public IPortObject newPort(Long dpid, Short portNumber) {
+ TestPortObject port = null;
+ TestSwitchObject sw = (TestSwitchObject)searchSwitch(HexString.toHexString(dpid));
+
+ if(sw != null) {
+ port = (TestPortObject)newPort(portNumber);
+ portsToAdd.add(port);
+ sw.addPort(port);
+ }
+
+ return port;
+ }
+
+ @Override
+ public IPortObject searchPort(String dpid_str, short number) {
+ long dpid = HexString.toLong(dpid_str);
+
+ for(TestSwitchObject sw : switches) {
+ if(HexString.toLong(sw.getDPID()) == dpid) {
+ for(IPortObject port : sw.getPorts()) {
+ if(port.getNumber().equals(number)) {
+ return port;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public void removePort(IPortObject port) {
+ for(TestSwitchObject sw : switches) {
+ for(IPortObject pt : sw.getPorts()) {
+ if(pt.equals(port)) {
+ sw.removePort(port);
+ }
+ }
+ }
+ portsToRemove.add((TestPortObject)port);
+ }
+
+ @Override
+ public IDeviceObject newDevice() {
+ TestDeviceObject dev = new TestDeviceObject();
+ devicesToAdd.add(dev);
+
+ return dev;
+ }
+
+ @Override
+ public IDeviceObject searchDevice(String macAddr) {
+ for(IDeviceObject dev : devices) {
+ if(dev.getMACAddress().equals(macAddr)) {
+ return dev;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Iterable<IDeviceObject> getDevices() {
+ List<IDeviceObject> list = new ArrayList<IDeviceObject>();
+
+ for(TestDeviceObject dev : devices) {
+ list.add(dev);
+ }
+
+ return list;
+ }
+
+ @Override
+ public void removeDevice(IDeviceObject dev) {
+ if(devices.contains((TestDeviceObject)dev)) {
+ devicesToRemove.add((TestDeviceObject)dev);
+ }
+ }
+
+ @Override
+ public IFlowPath newFlowPath() {
+ TestFlowPath path = new TestFlowPath();
+ pathsToAdd.add(path);
+
+ return path;
+ }
+
+ @Override
+ public IFlowPath searchFlowPath(FlowId flowId) {
+ for(IFlowPath path : paths) {
+ if(path.getFlowId().equals(flowId)) {
+ return path;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
+ for(IFlowPath path : paths) {
+ for(IFlowEntry entry : path.getFlowEntries()) {
+ if(entry.equals(flowEntry)) {
+ return path;
+ }
+ }
+
+ }
+ return null;
+ }
+
+ @Override
+ public Iterable<IFlowPath> getAllFlowPaths() {
+ List<IFlowPath> list = new ArrayList<IFlowPath>();
+
+ for(IFlowPath path : paths) {
+ list.add(path);
+ }
+
+ return list;
+ }
+
+ @Override
+ public void removeFlowPath(IFlowPath flowPath) {
+ if(paths.contains((TestFlowPath)flowPath)) {
+ pathsToRemove.add((TestFlowPath)flowPath);
+ }
+ }
+
+ @Override
+ public IFlowEntry newFlowEntry() {
+ TestFlowEntry entry = new TestFlowEntry();
+ entriesToAdd.add(entry);
+ return entry;
+ }
+
+ @Override
+ public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
+ for(TestFlowEntry entry : entries) {
+ // TODO check if this matching works
+ if(entry.getFlowEntryId().equals(flowEntryId)) {
+ return entry;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Iterable<IFlowEntry> getAllFlowEntries() {
+ List<IFlowEntry> list = new ArrayList<IFlowEntry>();
+
+ for(TestFlowEntry entry : entries) {
+ list.add(entry);
+ }
+
+ return list;
+ }
+
+ @Override
+ public void removeFlowEntry(IFlowEntry flowEntry) {
+ if(entries.contains((TestFlowEntry)flowEntry)) {
+ entriesToRemove.add((TestFlowEntry)flowEntry);
+ }
+ }
+
+ @Override
+ public IDBConnection getDBConnection() {
+ return super.getDBConnection();
+ }
+
+ @Override
+ public void commit() {
+ for(TestSwitchObject sw : switchesToAdd) {
+ switches.add(sw);
+ }
+ for(TestSwitchObject sw : switchesToRemove) {
+ sw.commit();
+ switches.remove(sw);
+ }
+ for(TestSwitchObject sw : switches) {
+ sw.commit();
+ }
+
+ for(TestPortObject port : portsToAdd) {
+ ports.add(port);
+ }
+ for(TestPortObject port : portsToRemove) {
+ port.commit();
+ ports.remove(port);
+ }
+ for(TestPortObject port : ports) {
+ port.commit();
+ }
+
+ for(TestDeviceObject dev : devicesToAdd) {
+ devices.add(dev);
+ }
+ for(TestDeviceObject dev : devicesToRemove) {
+ dev.commit();
+ devices.remove(dev);
+ }
+ for(TestDeviceObject dev : devices) {
+ dev.commit();
+ }
+
+ clearUncommitedData();
+ }
+
+ @Override
+ public void rollback() {
+ clearUncommitedData();
+ }
+
+ @Override
+ public void close() {
+ // TODO Auto-generated method stub
+
+ }
+}