ONOS listens to device discovery messages
diff --git a/src/main/java/net/onrc/onos/util/GraphDBConnection.java b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
new file mode 100644
index 0000000..7c1ea2a
--- /dev/null
+++ b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
@@ -0,0 +1,84 @@
+package net.onrc.onos.util;
+
+import java.util.Set;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
+import com.tinkerpop.frames.FramedGraph;
+
+public class GraphDBConnection {
+ public enum Transaction {
+ COMMIT,
+ ROLLBACK
+ }
+ private static GraphDBConnection singleton = new GraphDBConnection( );
+ private static TitanGraph graph;
+ private static GraphDBUtils utils;
+
+ /* A private Constructor prevents any other
+ * class from instantiating.
+ */
+ private GraphDBConnection(){ }
+
+ /* Static 'instance' method */
+ public static GraphDBConnection getInstance(String conf) {
+ if (graph == null||graph.isOpen()) {
+ graph = TitanFactory.open(conf);
+ // FIXME: Creation on Indexes should be done only once
+ Set<String> s = graph.getIndexedKeys(Vertex.class);
+ if (!s.contains("dpid")) {
+ graph.createKeyIndex("dpid", Vertex.class);
+ }
+ if (!s.contains("type")) {
+ graph.createKeyIndex("type", Vertex.class);
+ }
+ if (!s.contains("dl_address")) {
+ graph.createKeyIndex("dl_address", Vertex.class);
+ }
+ }
+ graph.stopTransaction(Conclusion.SUCCESS);
+ if (utils == null) {
+ utils = new GraphDBUtils();
+ }
+ return singleton;
+ }
+
+ public IDBUtils utils() {
+ return utils;
+ }
+
+ protected FramedGraph<TitanGraph> getFramedGraph() {
+
+ if (isValid()) {
+ FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
+ return fg;
+ } else {
+ return null;
+ }
+ }
+
+ public Boolean isValid() {
+
+ return (graph != null||graph.isOpen());
+ }
+
+ public void startTx() {
+
+ }
+
+ public void endTx(Transaction tx) {
+ switch (tx) {
+ case COMMIT:
+ graph.stopTransaction(Conclusion.SUCCESS);
+ case ROLLBACK:
+ graph.stopTransaction(Conclusion.FAILURE);
+ }
+ }
+
+ public void close() {
+
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/util/GraphDBUtils.java b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
new file mode 100644
index 0000000..8bb83ea
--- /dev/null
+++ b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
@@ -0,0 +1,59 @@
+package net.onrc.onos.util;
+
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.thinkaurelius.titan.graphdb.transaction.VertexIterable;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.frames.FramedGraph;
+import com.tinkerpop.frames.FramedVertexIterable;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
+
+import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
+import net.floodlightcontroller.routing.Link;
+
+public class GraphDBUtils implements IDBUtils {
+
+ @Override
+ public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
+ // TODO Auto-generated method stub
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+
+ return fg.getVertices("dpid",dpid).iterator().hasNext() ?
+ fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
+
+ }
+
+ @Override
+ public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
+ // TODO Auto-generated method stub
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
+ IDeviceObject.class).iterator().next() : null;
+
+ }
+
+ @Override
+ public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
+ ISwitchObject sw = searchSwitch(conn, dpid);
+ GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
+ pipe.start(sw.asVertex());
+ pipe.out("on").has("number", number);
+ FramedVertexIterable<IPortObject> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IPortObject.class);
+ return r.iterator().hasNext() ? r.iterator().next() : null;
+ }
+
+ @Override
+ public IDeviceObject newDevice(GraphDBConnection conn) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
+ return obj;
+ }
+
+ @Override
+ public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ fg.removeVertex(dev.asVertex());
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/util/IDBUtils.java b/src/main/java/net/onrc/onos/util/IDBUtils.java
new file mode 100644
index 0000000..52dbc70
--- /dev/null
+++ b/src/main/java/net/onrc/onos/util/IDBUtils.java
@@ -0,0 +1,13 @@
+package net.onrc.onos.util;
+
+import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
+import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
+
+public interface IDBUtils {
+ public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid);
+ public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr);
+ public IDeviceObject newDevice(GraphDBConnection conn);
+ public void removeDevice(GraphDBConnection conn, IDeviceObject dev);
+ public IPortObject searchPort(GraphDBConnection conn, String dpid, short number);
+}