first commit2
diff --git a/src/main/java/net/onrc/onos/graph/DBConnection.java b/src/main/java/net/onrc/onos/graph/DBConnection.java
new file mode 100644
index 0000000..dee458a
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/DBConnection.java
@@ -0,0 +1,19 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public abstract class DBConnection implements IDBConnection {
+ public enum Transaction {
+ COMMIT, ROLLBACK
+ }
+
+ public enum GenerateEvent {
+ TRUE, FALSE
+ }
+}
diff --git a/src/main/java/net/onrc/onos/graph/DBOperation.java b/src/main/java/net/onrc/onos/graph/DBOperation.java
new file mode 100644
index 0000000..db3dad7
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/DBOperation.java
@@ -0,0 +1,186 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.frames.FramedGraph;
+import com.tinkerpop.frames.structures.FramedVertexIterable;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
+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.core.ISwitchStorage;
+import net.onrc.onos.ofcontroller.util.FlowId;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public abstract class DBOperation implements IDBOperation {
+
+ protected DBConnection conn;
+
+ @Override
+ public ISwitchObject searchActiveSwitch(String dpid) {
+ ISwitchObject sw = searchSwitch(dpid);
+ if ((sw != null)
+ && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
+ return sw;
+ }
+ return null;
+ }
+
+ @Override
+ public ISwitchObject newSwitch(final String dpid) {
+ ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
+ if (obj != null) {
+ obj.setType("switch");
+ obj.setDPID(dpid);
+ }
+ return obj;
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getAllSwitches() {
+ Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
+ return switches;
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getInactiveSwitches() {
+ Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
+ List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
+
+ for (ISwitchObject sw : switches) {
+ if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
+ inactiveSwitches.add(sw);
+ }
+ }
+ return inactiveSwitches;
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
+ //TODO: Should use an enum for flow_switch_state
+ return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
+
+ }
+
+ @Override
+ public void removeSwitch(ISwitchObject sw) {
+ conn.getFramedGraph().removeVertex(sw.asVertex());
+ }
+
+ @Override
+ public IPortObject newPort(String dpid, Short portNum) {
+ IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
+ if (obj != null) {
+ obj.setType("port");
+ String id = dpid + portNum.toString();
+ obj.setPortId(id);
+ obj.setNumber(portNum);
+ }
+ return obj;
+ }
+
+ public IPortObject searchPort(String dpid, Short number, final FramedGraph fg) {
+ String id = dpid + number.toString();
+ return (fg != null && fg.getVertices("port_id", id).iterator().hasNext())
+ ? (IPortObject) fg.getVertices("port_id", id, IPortObject.class).iterator().next() : null;
+
+ }
+
+ @Override
+ public IDeviceObject newDevice() {
+ IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
+ if (obj != null) {
+ obj.setType("device");
+ }
+ return obj;
+ }
+
+ @Override
+ public IFlowPath newFlowPath() {
+ IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
+ if (flowPath != null) {
+ flowPath.setType("flow");
+ }
+ return flowPath;
+ }
+
+ @Override
+ public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
+ GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
+ pipe.start(flowEntry.asVertex());
+ pipe.out("flow");
+ FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
+ return r.iterator().hasNext() ? r.iterator().next() : null;
+ }
+
+
+
+ protected ISwitchObject searchSwitch(final String dpid, final FramedGraph fg) {
+ return (fg != null && fg.getVertices("dpid", dpid).iterator().hasNext())
+ ? (ISwitchObject) (fg.getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
+ }
+
+ protected Iterable<ISwitchObject> getActiveSwitches(final FramedGraph fg) {
+ Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
+ List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
+
+ for (ISwitchObject sw : switches) {
+ if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
+ activeSwitches.add(sw);
+ }
+ }
+ return activeSwitches;
+ }
+
+ protected Iterable<ISwitchObject> getAllSwitches(final FramedGraph fg) {
+ Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
+ return switches;
+ }
+
+ protected IDeviceObject searchDevice(String macAddr, final FramedGraph fg) {
+ return (fg != null && fg.getVertices("dl_addr", macAddr).iterator().hasNext())
+ ? (IDeviceObject) fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
+
+ }
+
+ protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) {
+ return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext()
+ ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(),
+ IFlowPath.class).iterator().next() : null;
+ }
+
+ protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) {
+ Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
+
+ List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
+
+ for (IFlowPath fp : flowPaths) {
+ if (fp.getFlowId() != null) {
+ nonNullFlows.add(fp);
+ }
+ }
+ return nonNullFlows;
+ }
+
+ protected IFlowEntry newFlowEntry(final FramedGraph fg) {
+ IFlowEntry flowEntry = (IFlowEntry) fg.addVertex(null, IFlowEntry.class);
+ if (flowEntry != null) {
+ flowEntry.setType("flow_entry");
+ }
+ return flowEntry;
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/graph/GraphDBManager.java b/src/main/java/net/onrc/onos/graph/GraphDBManager.java
new file mode 100644
index 0000000..6b06fd2
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/GraphDBManager.java
@@ -0,0 +1,58 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public class GraphDBManager {
+ private static ThreadLocal<HashMap<String, DBConnection>> connections = new ThreadLocal<HashMap<String, DBConnection>>();
+
+ static Map<String, DBConnection> getConnectionMap() {
+ if (connections.get() == null) {
+ connections.set(new HashMap<String, DBConnection>());
+ }
+ return connections.get();
+ }
+
+ public static DBOperation getDBOperation(final String dbStore, final String dbConfigFile) {
+ DBOperation operation = null;
+ if (dbStore.equals("ramcloud")) {
+ operation = new RamCloudDBOperation();
+ } else if (dbStore.equals("titan")) {
+ operation = new TitanDBOperation();
+ }
+ if (operation != null) {
+ operation.conn = GraphDBManager.getConnection(dbStore, dbConfigFile);
+ }
+ return null;
+ }
+
+ public static DBConnection getConnection(final String dbStore, final String dbConfigFile) {
+ DBConnection conn = getConnectionMap().get(dbStore);
+ if (conn == null) {
+ if (dbStore.equals("ramcloud")) {
+ conn = new RamCloudDBConnection(dbConfigFile);
+ } else if (dbStore.equals("titan")) {
+ conn = new TitanDBConnection(dbConfigFile);
+ }
+
+ GraphDBManager.getConnectionMap().put(dbStore, conn);
+ } else {
+ GraphDBManager.getConnectionMap().get(dbStore);
+ }
+ return conn;
+ }
+
+ static List<DBConnection> getConnections() {
+ return new ArrayList<DBConnection>(getConnectionMap().values());
+ }
+}
diff --git a/src/main/java/net/onrc/onos/graph/RamCloudDBConnection.java b/src/main/java/net/onrc/onos/graph/RamCloudDBConnection.java
new file mode 100644
index 0000000..66c3154
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/RamCloudDBConnection.java
@@ -0,0 +1,80 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph;
+import com.tinkerpop.frames.FramedGraph;
+import java.io.File;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public class RamCloudDBConnection extends DBConnection {
+ private RamCloudGraph graph;
+
+ public RamCloudDBConnection(final String dbConfigFile) {
+ System.out.println("dbconfigfile is + "+ dbConfigFile);
+ final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
+ graph = new RamCloudGraph(coordinatorURL);
+ }
+
+ @Override
+ public FramedGraph getFramedGraph() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void addEventListener(LocalGraphChangedListener listener) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Boolean isValid() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void commit() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void rollback() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void close() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ private static final Configuration getConfiguration(final File dirOrFile) {
+ if (dirOrFile == null) {
+ throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
+ }
+
+ if (!dirOrFile.isFile()) {
+ throw new IllegalArgumentException("Location of configuration must be a file");
+ }
+
+ try {
+ return new PropertiesConfiguration(dirOrFile);
+ } catch (ConfigurationException e) {
+ throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
+ }
+ }
+
+ private String open(final Configuration configuration) {
+ final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
+ if (coordinatorURL == null) {
+ throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
+ }
+ return coordinatorURL;
+ }
+}
diff --git a/src/main/java/net/onrc/onos/graph/RamCloudDBOperation.java b/src/main/java/net/onrc/onos/graph/RamCloudDBOperation.java
new file mode 100644
index 0000000..93b0054
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/RamCloudDBOperation.java
@@ -0,0 +1,170 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph;
+import com.tinkerpop.frames.FramedGraph;
+import java.io.File;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
+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.util.FlowEntryId;
+import net.onrc.onos.ofcontroller.util.FlowId;
+import org.apache.commons.configuration.Configuration;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public class RamCloudDBOperation extends DBOperation {
+
+ public RamCloudDBOperation() {
+ //Configuration configuration= getConfiguration(new File(dbConfigFile));
+ //final String coordinatorURL = configuration.getProperty("connect.coordinator");
+ }
+
+ @Override
+ public INetMapTopologyObjects.ISwitchObject searchSwitch(String dpid) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public INetMapTopologyObjects.ISwitchObject searchActiveSwitch(String dpid) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.ISwitchObject> getActiveSwitches() {
+ return getActiveSwitches(conn.getFramedGraph());
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.ISwitchObject> getAllSwitches() {
+ return getAllSwitches(conn.getFramedGraph());
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.ISwitchObject> getInactiveSwitches() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void removeSwitch(INetMapTopologyObjects.ISwitchObject sw) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public INetMapTopologyObjects.IPortObject newPort(Short portNumber) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public INetMapTopologyObjects.IPortObject newPort(String dpid, Short portNum) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public IPortObject searchPort(String dpid, Short number) {
+ final FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return searchPort(dpid, number, fg);
+ }
+
+ @Override
+ public void removePort(INetMapTopologyObjects.IPortObject port) {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ if (fg != null) {
+ fg.removeVertex(port.asVertex());
+ }
+ }
+
+
+ @Override
+ public IDeviceObject searchDevice(String macAddr) {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return searchDevice(macAddr, fg);
+ }
+
+ @Override
+ public Iterable<IDeviceObject> getDevices() {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return fg != null ? fg.getVertices("type", "device", IDeviceObject.class) : null;
+ }
+
+ @Override
+ public void removeDevice(IDeviceObject dev) {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ if (fg != null) {
+ fg.removeVertex(dev.asVertex());
+ }
+ }
+
+
+ @Override
+ public INetMapTopologyObjects.IFlowPath searchFlowPath(FlowId flowId) {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return searchFlowPath(flowId, fg);
+ }
+
+ @Override
+ public Iterable<IFlowPath> getAllFlowPaths() {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return getAllFlowPaths(fg);
+ }
+
+ @Override
+ public void removeFlowPath(INetMapTopologyObjects.IFlowPath flowPath) {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ fg.removeVertex(flowPath.asVertex());
+ }
+
+ @Override
+ public IFlowEntry newFlowEntry() {
+ FramedGraph<RamCloudGraph> fg = conn.getFramedGraph();
+ return newFlowEntry(fg);
+ }
+
+ @Override
+ public INetMapTopologyObjects.IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public Iterable<INetMapTopologyObjects.IFlowEntry> getAllFlowEntries() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void removeFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public IDBConnection getDBConnection() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void commit() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void rollback() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void close() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+}
diff --git a/src/main/java/net/onrc/onos/graph/TitanDBConnection.java b/src/main/java/net/onrc/onos/graph/TitanDBConnection.java
new file mode 100644
index 0000000..055a758
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/TitanDBConnection.java
@@ -0,0 +1,125 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import com.thinkaurelius.titan.core.TitanFactory;
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.TransactionalGraph;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
+import com.tinkerpop.frames.FramedGraph;
+import java.util.Set;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public class TitanDBConnection extends DBConnection {
+
+ private TitanGraph graph;
+ private static Logger log = LoggerFactory.getLogger(TitanDBConnection.class);
+ private EventTransactionalGraph<TitanGraph> eg;
+
+ public TitanDBConnection(final String dbConfigFile) {
+ graph = TitanFactory.open(dbConfigFile);
+ Set<String> s = graph.getIndexedKeys(Vertex.class);
+ if (!s.contains("dpid")) {
+ graph.createKeyIndex("dpid", Vertex.class);
+ }
+ if (!s.contains("port_id")) {
+ graph.createKeyIndex("port_id", Vertex.class);
+ }
+ if (!s.contains("type")) {
+ graph.createKeyIndex("type", Vertex.class);
+ }
+ if (!s.contains("dl_addr")) {
+ graph.createKeyIndex("dl_addr", Vertex.class);
+ }
+ if (!s.contains("flow_id")) {
+ graph.createKeyIndex("flow_id", Vertex.class);
+ }
+ if (!s.contains("flow_entry_id")) {
+ graph.createKeyIndex("flow_entry_id", Vertex.class);
+ }
+ if (!s.contains("switch_state")) {
+ graph.createKeyIndex("switch_state", Vertex.class);
+ }
+ graph.commit();
+ eg = new EventTransactionalGraph<TitanGraph>(graph);
+ }
+
+ class TransactionHandle {
+
+ protected TransactionalGraph tr;
+
+ public void create() {
+ tr = graph.newTransaction();
+ }
+ }
+
+ @Override
+ public FramedGraph getFramedGraph() {
+ if (isValid()) {
+ FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
+ return fg;
+ } else {
+ log.error("new FramedGraph failed");
+ return null;
+ }
+ }
+
+ @Override
+ public void addEventListener(LocalGraphChangedListener listener) {
+ EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
+ eg.addListener(listener);
+ log.debug("Registered listener {}", listener.getClass());
+ }
+
+ @Override
+ public Boolean isValid() {
+ return (graph != null || graph.isOpen());
+ }
+
+ @Override
+ public void commit() {
+ try {
+ graph.commit();
+ } catch (Exception e) {
+ log.error("{}", e.toString());
+ }
+ }
+
+ @Override
+ public void rollback() {
+ try {
+ graph.rollback();
+ } catch (Exception e) {
+ log.error("{}", e.toString());
+ }
+ }
+
+ @Override
+ public void close() {
+ commit();
+ }
+
+ /**
+ * Get EventTransactionalGraph of the titan graph.
+ *
+ * @return EventTransactionalGraph of the titan graph
+ */
+ private EventTransactionalGraph<TitanGraph> getEventGraph() {
+ if (isValid()) {
+ return eg;
+ } else {
+ return null;
+ }
+ }
+
+ private TitanDBConnection() {
+ }
+}
diff --git a/src/main/java/net/onrc/onos/graph/TitanDBOperation.java b/src/main/java/net/onrc/onos/graph/TitanDBOperation.java
new file mode 100644
index 0000000..26289bd
--- /dev/null
+++ b/src/main/java/net/onrc/onos/graph/TitanDBOperation.java
@@ -0,0 +1,169 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.onrc.onos.graph;
+
+import com.thinkaurelius.titan.core.TitanGraph;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.frames.FramedGraph;
+import com.tinkerpop.frames.structures.FramedVertexIterable;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
+import java.util.ArrayList;
+import java.util.List;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.*;
+import net.onrc.onos.ofcontroller.core.ISwitchStorage;
+import net.onrc.onos.ofcontroller.util.FlowEntryId;
+import net.onrc.onos.ofcontroller.util.FlowId;
+
+/**
+ *
+ * @author nickkaranatsios
+ */
+public class TitanDBOperation extends DBOperation {
+
+ /**
+ * Search and get a switch object with DPID.
+ *
+ * @param dpid DPID of the switch
+ */
+ @Override
+ public ISwitchObject searchSwitch(String dpid) {
+ final FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+
+ return searchSwitch(dpid, fg);
+ }
+
+ @Override
+ public IPortObject newPort(String dpid, Short portNum) {
+ return newPort(dpid, portNum);
+ }
+
+ @Override
+ public Iterable<ISwitchObject> getActiveSwitches() {
+ final FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+
+ return getActiveSwitches(fg);
+ }
+
+ @Override
+ public IPortObject searchPort(String dpid, Short number) {
+ final FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return searchPort(dpid, number, fg);
+ }
+
+
+ @Override
+ public void removePort(IPortObject port) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ if (fg != null) {
+ fg.removeVertex(port.asVertex());
+ }
+ }
+
+
+ @Override
+ public IDeviceObject searchDevice(String macAddr) {
+ // TODO Auto-generated method stub
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return searchDevice(macAddr, fg);
+ }
+
+ @Override
+ public Iterable<IDeviceObject> getDevices() {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return fg != null ? fg.getVertices("type", "device", IDeviceObject.class) : null;
+ }
+
+ @Override
+ public void removeDevice(IDeviceObject dev) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ if (fg != null) {
+ fg.removeVertex(dev.asVertex());
+ }
+ }
+
+
+ @Override
+ public IFlowPath searchFlowPath(FlowId flowId) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return searchFlowPath(flowId, fg);
+ }
+
+
+ @Override
+ public Iterable<IFlowPath> getAllFlowPaths() {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return getAllFlowPaths(fg);
+ }
+
+ @Override
+ public void removeFlowPath(IFlowPath flowPath) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ fg.removeVertex(flowPath.asVertex());
+ }
+
+ @Override
+ public IFlowEntry newFlowEntry() {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ return newFlowEntry(fg);
+ }
+
+ @Override
+ public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+
+ return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext()
+ ? fg.getVertices("flow_entry_id", flowEntryId.toString(),
+ IFlowEntry.class).iterator().next() : null;
+ }
+
+ @Override
+ public Iterable<IFlowEntry> getAllFlowEntries() {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+
+ return fg.getVertices("type", "flow_entry", IFlowEntry.class);
+ }
+
+ @Override
+ public void removeFlowEntry(IFlowEntry flowEntry) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ fg.removeVertex(flowEntry.asVertex());
+ }
+
+ @Override
+ public IDBConnection getDBConnection() {
+ return conn;
+ }
+
+ @Override
+ public void commit() {
+ conn.commit();
+ }
+
+ @Override
+ public void rollback() {
+ conn.rollback();
+ }
+
+ @Override
+ public void close() {
+ conn.close();
+ }
+
+ /**
+ * Create a port having specified port number.
+ *
+ * @param portNumber port number
+ */
+ @Deprecated
+ public IPortObject newPort(Short portNumber) {
+ FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+ IPortObject obj = fg.addVertex(null, IPortObject.class);
+ if (obj != null) {
+ obj.setType("port");
+ obj.setNumber(portNumber);
+ }
+ return obj;
+ }
+}
\ No newline at end of file