Add trial code for GraphDBConnection to pass UnitTest.
diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
index ef61611..6707a11 100644
--- a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
+++ b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
@@ -28,6 +28,9 @@
protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
protected String conf;
+ /**
+ * Update
+ */
@Override
public void update(Link link, DM_OPERATION op) {
update(link, (LinkInfo)null, op);
@@ -72,7 +75,6 @@
vportDst = conn.utils().searchPort(conn, dpid, port);
if (vportSrc != null && vportDst != null) {
-
// check if the link exists
Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
@@ -80,7 +82,7 @@
for (IPortObject V : currPorts) {
currLinks.add(V);
}
-
+
if (currLinks.contains(vportDst)) {
if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
@@ -88,9 +90,49 @@
} else if (op.equals(DM_OPERATION.UPDATE)) {
// TODO: update linkinfo
// GraphDB seems to have no KeyIndex for LinkInfo data
+
+ // BEGIN: trial code (update implementation)
+ if(linkinfo != null) {
+ vportSrc.setPortState(linkinfo.getSrcPortState());
+ vportDst.setPortState(linkinfo.getDstPortState());
+
+ Vertex vsrc = vportSrc.asVertex();
+ vsrc.setProperty("first_seen_time", linkinfo.getFirstSeenTime());
+ vsrc.setProperty("last_lldp_received_time", linkinfo.getUnicastValidTime());
+ vsrc.setProperty("last_bddp_received_time", linkinfo.getMulticastValidTime());
+
+// for(Edge e: vportSrc.asVertex().getEdges(Direction.OUT)) {
+// if(e.getVertex(Direction.OUT).equals(vportDst.asVertex())) {
+// e.setProperty("first_seen_time", linkinfo.getFirstSeenTime());
+// e.setProperty("last_lldp_received_time", linkinfo.getUnicastValidTime());
+// e.setProperty("last_bddp_received_time", linkinfo.getMulticastValidTime());
+// }
+// }
+
+ conn.endTx(Transaction.COMMIT);
+ log.debug("addOrUpdateLink(): link updated {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
+ }
+ // END: trial code
}
} else {
- vportSrc.setLinkPort(vportDst);
+ if (op.equals(DM_OPERATION.UPDATE)) {
+ log.debug("addOrUpdateLink(): failed link doesn't exist {} {} src {} dst {}",
+ new Object[]{op, lt, vportSrc, vportDst});
+ } else {
+ vportSrc.setLinkPort(vportDst);
+
+ // BEGIN: trial code (update implementation)
+ if(linkinfo != null) {
+ vportSrc.setPortState(linkinfo.getSrcPortState());
+ vportDst.setPortState(linkinfo.getDstPortState());
+
+ Vertex vsrc = vportSrc.asVertex();
+ vsrc.setProperty("first_seen_time", linkinfo.getFirstSeenTime());
+ vsrc.setProperty("last_lldp_received_time", linkinfo.getUnicastValidTime());
+ vsrc.setProperty("last_bddp_received_time", linkinfo.getMulticastValidTime());
+ }
+ // END: trial code
+ }
conn.endTx(Transaction.COMMIT);
log.debug("addOrUpdateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
@@ -148,6 +190,14 @@
}
}*/
vportSrc.removeLink(vportDst);
+
+ // BEGIN: trial code (update implementation)
+ Vertex vsrc = vportSrc.asVertex();
+ vsrc.removeProperty("first_seen_time");
+ vsrc.removeProperty("last_lldp_received_time");
+ vsrc.removeProperty("last_bddp_received_time");
+ // END: trial code
+
conn.endTx(Transaction.COMMIT);
log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
lt, vportSrc, vportDst});
diff --git a/src/main/java/net/onrc/onos/util/GraphDBConnection.java b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
index dcf8aba..7041329 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBConnection.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
@@ -7,6 +7,7 @@
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
@@ -50,27 +51,65 @@
(graph == null||graph.isOpen() == Boolean.FALSE)) {
graph = TitanFactory.open(GraphDBConnection.configFile);
// FIXME: Creation on Indexes should be done only once
- Set<String> s = graph.getIndexedKeys(Vertex.class);
- if (!s.contains("dpid")) {
+ Set<String> s_vertices = graph.getIndexedKeys(Vertex.class);
+ if (!s_vertices.contains("dpid")) {
graph.createKeyIndex("dpid", Vertex.class);
}
- if (!s.contains("type")) {
+ if (!s_vertices.contains("type")) {
graph.createKeyIndex("type", Vertex.class);
}
- if (!s.contains("dl_address")) {
+ if (!s_vertices.contains("dl_address")) {
graph.createKeyIndex("dl_address", Vertex.class);
}
- if (!s.contains("flow_id")) {
+ if (!s_vertices.contains("flow_id")) {
graph.createKeyIndex("flow_id", Vertex.class);
}
- if (!s.contains("flow_entry_id")) {
+ if (!s_vertices.contains("flow_entry_id")) {
graph.createKeyIndex("flow_entry_id",
Vertex.class);
}
- if (!s.contains("switch_state")) {
+ if (!s_vertices.contains("switch_state")) {
graph.createKeyIndex("switch_state",
Vertex.class);
}
+
+ // BEGIN: trial code
+ /*
+ * Trial to store link state information as properties of port.
+ * Currently no need to be implemented. Just reference.
+ */
+ // These keys are assigned to port vertex with "OUT" direction link
+// if (!s_vertices.contains("first_seen_time")) {
+// graph.createKeyIndex("first_seen_time", Vertex.class);
+// }
+// if (!s_vertices.contains("last_lldp_received_time")) {
+// graph.createKeyIndex("last_lldp_received_time", Vertex.class);
+// }
+// if (!s_vertices.contains("last_bddp_received_time")) {
+// graph.createKeyIndex("last_bddp_received_time", Vertex.class);
+// }
+//
+//
+// Set<String> s_edges = graph.getIndexedKeys(Edge.class);
+// if (!s_edges.contains("src_port_state")) {
+// graph.createKeyIndex("src_port_state",
+// Edge.class);
+// }
+// if (!s_edges.contains("dst_port_state")) {
+// graph.createKeyIndex("dst_port_state",
+// Edge.class);
+// }
+// if (!s_edges.contains("first_seen_time")) {
+// graph.createKeyIndex("first_seen_time", Edge.class);
+// }
+// if (!s_edges.contains("last_lldp_received_time")) {
+// graph.createKeyIndex("last_lldp_received_time", Edge.class);
+// }
+// if (!s_edges.contains("last_bddp_received_time")) {
+// graph.createKeyIndex("last_bddp_received_time", Edge.class);
+// }
+ // END: trial code
+
graph.commit();
eg = new EventTransactionalGraph<TitanGraph>(graph);
}
@@ -80,7 +119,7 @@
return singleton;
}
- public IDBUtils utils() {
+ public IDBUtils utils() {
return utils;
}
diff --git a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
index 079715a..39221ab 100644
--- a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
+++ b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
@@ -7,6 +7,9 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
+
+import junit.framework.Assert;
import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
import net.floodlightcontroller.core.internal.TestDatabaseManager;
@@ -20,6 +23,8 @@
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.openflow.protocol.OFPhysicalPort;
+import org.openflow.protocol.OFPhysicalPort.OFPortState;
import org.openflow.util.HexString;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -27,8 +32,12 @@
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Direction;
+import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
+import com.tinkerpop.pipes.PipeFunction;
+import com.tinkerpop.pipes.transform.PathPipe;
@RunWith(PowerMockRunner.class)
@PrepareForTest({TitanFactory.class})
@@ -75,19 +84,17 @@
TestDatabaseManager.deleteTestDatabase();
}
- // TODO: remove @Ignore after UPDATE method will be implemented
+ // TODO: remove @Ignore after UPDATE method is implemented
@Ignore @Test
public void testUpdate_UpdateSingleLink() {
Link linkToUpdate= createExistingLink();
- LinkInfo infoToUpdate = new LinkInfo(
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- 0, 0);
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
linkStorage.update(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
- // TODO: get LinkInfo from titanGraph and verify
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
}
@Test
@@ -141,18 +148,7 @@
linkStorage.update(linkToDelete, DM_OPERATION.DELETE);
doTestLinkIsNotInGraph(linkToVerify);
}
-
- // TODO: remove @Ignore after UPDATE method will be implemented
- @Ignore @Test
- public void testUpdate_UpdateLinks(){
- List<Link> linksToUpdate= createExistingLinks();
-
- // TODO: Who calls update method like this way? Remove this test if unneeded.
- linkStorage.update(linksToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
-
- // TODO: verification of update result
- }
-
+
@Test
public void testUpdate_CreateLinks(){
List<Link> linksToCreate = createFeasibleLinks();
@@ -247,19 +243,17 @@
doTestLinkIsNotInGraph(createExistingLink());
}
+ // TODO: remove @Ignore after UPDATE method is implemented
@Ignore @Test
public void testAddOrUpdateLink_Update() {
Link linkToUpdate= createExistingLink();
- LinkInfo infoToUpdate = new LinkInfo(
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- System.currentTimeMillis(),
- 0, 0);
+ long currentTime = System.currentTimeMillis();
+ LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime);
+ LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime);
linkStorage.addOrUpdateLink(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE);
- // TODO: get LinkInfo from titanGraph and verify
-
+ doTestLinkHasStateOf(linkToUpdate, infoToVerify);
}
@Test
@@ -421,7 +415,6 @@
short src_port = link.getSrcPort();
short dst_port = link.getDstPort();
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
// Test if just one switch is found in the graph
@@ -429,6 +422,7 @@
Vertex sw = it.next();
assertFalse(it.hasNext());
+ GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid);
// Test if just one link is found in the graph
@@ -447,7 +441,6 @@
short src_port = link.getSrcPort();
short dst_port = link.getDstPort();
- GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
// Test if just one switch is found in the graph
@@ -455,11 +448,76 @@
Vertex sw = it.next();
assertFalse(it.hasNext());
+ GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid);
// Test if no link is found in the graph
assertFalse(pipe.hasNext());
}
+
+ /**
+ * Test if titanGraph has specific Link with specific LinkInfo
+ * @param link
+ */
+ private void doTestLinkHasStateOf(Link link, LinkInfo info) {
+ String src_dpid = HexString.toHexString(link.getSrc());
+ String dst_dpid = HexString.toHexString(link.getDst());
+ short src_port = link.getSrcPort();
+ short dst_port = link.getDstPort();
+
+ Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator();
+
+ // Test if just one switch is found in the graph
+ assertTrue(it.hasNext());
+ Vertex sw = it.next();
+ assertFalse(it.hasNext());
+
+ GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>();
+ pipe.start(sw);
+ pipe.enablePath();
+ pipe.out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid)
+ .path().step(new PipeFunction<PathPipe<Vertex>, Edge>() {
+ @Override
+ public Edge compute(PathPipe<Vertex> pipepath) {
+ List<Vertex> V = pipepath.next();
+
+ Vertex port_src = V.get(1);
+ Vertex port_dst = V.get(2);
+
+ for(Edge e : port_src.getEdges(Direction.OUT)) {
+ if(e.getVertex(Direction.IN).equals(port_dst)) {
+ return e;
+ }
+ }
+
+ return null;
+ }
+ });
+
+ // Test if just one link is found in the graph
+ assertTrue(pipe.hasNext());
+ Edge edge = pipe.next();
+ assertTrue(edge != null);
+ assertFalse(pipe.hasNext());
+
+ // TODO: implement test code to check if update is correctly done.
+ int portStateSrc = edge.getVertex(Direction.OUT).getProperty("port_state");
+ int portStateDst = edge.getVertex(Direction.IN).getProperty("port_state");
+
+ assertTrue(portStateSrc == info.getSrcPortState());
+ assertTrue(portStateDst == info.getDstPortState());
+
+// long firstSeenTime = edge.getProperty("first_seen_time");
+// long lastLldpReceivedTime = edge.getProperty("last_lldp_received_time");
+// long lastBddpReceivedTime = edge.getProperty("last_bddp_received_time");
+ long firstSeenTime = edge.getVertex(Direction.OUT).getProperty("first_seen_time");
+ long lastLldpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_lldp_received_time");
+ long lastBddpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_bddp_received_time");
+ assertTrue(firstSeenTime == info.getFirstSeenTime());
+ assertTrue(lastLldpReceivedTime == info.getUnicastValidTime());
+ assertTrue(lastBddpReceivedTime == info.getMulticastValidTime());
+ }
+
//----------------- Creation of test data -----------------------
/**
@@ -511,5 +569,23 @@
links.add(new Link(Long.decode("0x0000000000000a01"), 4, Long.decode("0x0000000000000a02"), 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);
+ }
//---------------------------------------------------------------
}