rename GraphDBUtils to GraphDBOperation and make it to have GraphDBConnection's instance.
diff --git a/src/main/java/net/onrc/onos/util/GraphDBConnection.java b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
index 7ea0ab8..afb8f9b 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBConnection.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
@@ -12,165 +12,160 @@
import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
import com.tinkerpop.frames.FramedGraph;
-public class GraphDBConnection {
+public class GraphDBConnection implements IDBConnection {
public enum Transaction {
- COMMIT,
- ROLLBACK
+ COMMIT, ROLLBACK
}
+
public enum GenerateEvent {
- TRUE,
- FALSE
+ TRUE, FALSE
}
+
class TransactionHandle {
protected TransactionalGraph tr;
+
public void create() {
- tr = graph.newTransaction();
+ tr = graph.newTransaction();
}
}
- protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class);
- private static GraphDBConnection singleton = new GraphDBConnection( );
+
+ protected static Logger log = LoggerFactory
+ .getLogger(GraphDBConnection.class);
+ private static GraphDBConnection singleton = new GraphDBConnection();
private static TitanGraph graph;
private static EventTransactionalGraph<TitanGraph> eg;
- private static GraphDBUtils utils;
+ private static GraphDBOperation utils;
private static String configFile;
-
- /* A private Constructor prevents any other
- * class from instantiating.
- */
- private GraphDBConnection(){ }
-
- /* Static 'instance' method */
- public static synchronized GraphDBConnection getInstance(final String conf) {
- if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) {
- GraphDBConnection.configFile = conf;
- log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile);
- }
- if (!GraphDBConnection.configFile.isEmpty() &&
- (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")) {
- 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);
- }
- 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);
- }
- if (utils == null) {
- utils = new GraphDBUtils();
- }
- return singleton;
- }
-
- public IDBUtils utils() {
- return utils;
- }
-
- public FramedGraph<TitanGraph> getFramedGraph() {
-
- if (isValid()) {
- FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
- return fg;
- } else {
- log.error("new FramedGraph failed");
- return null;
- }
- }
-
- protected EventTransactionalGraph<TitanGraph> getEventGraph() {
-
- if (isValid()) {
- return eg;
- } else {
- return null;
- }
- }
-
- public void addEventListener(final LocalGraphChangedListener listener) {
- EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
- eg.addListener(listener);
- log.debug("Registered listener {}",listener.getClass());
- }
-
- public Boolean isValid() {
-
- return (graph != null||graph.isOpen());
- }
-
- public void startTx() {
-
-
- }
-
- public void endTx(Transaction tx) {
- try {
- switch (tx) {
- case COMMIT:
- graph.commit();
- case ROLLBACK:
- graph.rollback();
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- log.error("{}",e.toString());
- }
- }
-
- public void endTx(TransactionHandle tr, Transaction tx) {
- switch (tx) {
- case COMMIT:
- if (tr != null && tr.tr != null) {
- tr.tr.commit();
- } else {
- graph.commit();
- }
- case ROLLBACK:
- if (tr != null && tr.tr != null) {
- tr.tr.rollback();
- } else {
- graph.rollback();
- }
- }
- }
-
- public void endTx(Transaction tx, GenerateEvent fire) {
+ /*
+ * A private Constructor prevents any other class from instantiating.
+ */
+ private GraphDBConnection() {
+ }
- try {
+ /* Static 'instance' method */
+ public static synchronized GraphDBConnection getInstance(final String conf) {
+ if (GraphDBConnection.configFile == null
+ || GraphDBConnection.configFile.isEmpty()) {
+ GraphDBConnection.configFile = conf;
+ log.debug("GraphDBConnection::Setting Config File {}",
+ GraphDBConnection.configFile);
+ }
+ if (!GraphDBConnection.configFile.isEmpty()
+ && (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")) {
+ 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);
+ }
+ 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);
+ }
+ return singleton;
+ }
+
+ public FramedGraph<TitanGraph> getFramedGraph() {
+
+ if (isValid()) {
+ FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
+ return fg;
+ } else {
+ log.error("new FramedGraph failed");
+ return null;
+ }
+ }
+
+ protected EventTransactionalGraph<TitanGraph> getEventGraph() {
+
+ if (isValid()) {
+ return eg;
+ } else {
+ return null;
+ }
+ }
+
+ public void addEventListener(final LocalGraphChangedListener listener) {
+ EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
+ eg.addListener(listener);
+ log.debug("Registered listener {}", listener.getClass());
+ }
+
+ public Boolean isValid() {
+
+ return (graph != null || graph.isOpen());
+ }
+
+ public void startTx() {
+
+ }
+
+ public void endTx(Transaction tx) {
+ try {
+ switch (tx) {
+ case COMMIT:
+ graph.commit();
+ case ROLLBACK:
+ graph.rollback();
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ log.error("{}", e.toString());
+ }
+ }
+
+ public void endTx(TransactionHandle tr, Transaction tx) {
+ switch (tx) {
+ case COMMIT:
+ if (tr != null && tr.tr != null) {
+ tr.tr.commit();
+ } else {
+ graph.commit();
+ }
+ case ROLLBACK:
+ if (tr != null && tr.tr != null) {
+ tr.tr.rollback();
+ } else {
+ graph.rollback();
+ }
+ }
+ }
+
+ public void endTx(Transaction tx, GenerateEvent fire) {
+
+ try {
if (fire.equals(GenerateEvent.TRUE)) {
- switch (tx) {
- case COMMIT:
- eg.commit();
- case ROLLBACK:
- eg.rollback();
- }
- } else {
- endTx(tx);
- }
+ switch (tx) {
+ case COMMIT:
+ eg.commit();
+ case ROLLBACK:
+ eg.rollback();
+ }
+ } else {
+ endTx(tx);
+ }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- }
-
- public void close() {
- endTx(Transaction.COMMIT);
- }
+ }
+
+ public void close() {
+ endTx(Transaction.COMMIT);
+ }
}
diff --git a/src/main/java/net/onrc/onos/util/GraphDBUtils.java b/src/main/java/net/onrc/onos/util/GraphDBOperation.java
similarity index 76%
rename from src/main/java/net/onrc/onos/util/GraphDBUtils.java
rename to src/main/java/net/onrc/onos/util/GraphDBOperation.java
index 2522765..bcaa3af 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBUtils.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBOperation.java
@@ -18,23 +18,28 @@
import com.tinkerpop.frames.structures.FramedVertexIterable;
import com.tinkerpop.gremlin.java.GremlinPipeline;
-public class GraphDBUtils implements IDBUtils {
+public class GraphDBOperation implements IDBOperation {
+ private GraphDBConnection conn;
+
+ public GraphDBOperation(GraphDBConnection dbConnection) {
+ this.conn = dbConnection;
+ }
@Override
- public ISwitchObject newSwitch(GraphDBConnection conn) {
+ public ISwitchObject newSwitch() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
return obj;
}
@Override
- public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
+ public void removeSwitch(ISwitchObject sw) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
fg.removeVertex(sw.asVertex());
}
@Override
- public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
+ public ISwitchObject searchSwitch(String dpid) {
// TODO Auto-generated method stub
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
@@ -44,7 +49,7 @@
}
@Override
- public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
+ public IDeviceObject searchDevice(String macAddr) {
// TODO Auto-generated method stub
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
@@ -53,8 +58,8 @@
}
@Override
- public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
- ISwitchObject sw = searchSwitch(conn, dpid);
+ public IPortObject searchPort(String dpid, short number) {
+ ISwitchObject sw = searchSwitch(dpid);
if (sw != null) {
IPortObject port = null;
@@ -82,41 +87,40 @@
}
@Override
- public IPortObject newPort(GraphDBConnection conn) {
+ public IPortObject newPort() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
IPortObject obj = fg.addVertex(null,IPortObject.class);
return obj;
}
@Override
- public IDeviceObject newDevice(GraphDBConnection conn) {
+ public IDeviceObject newDevice() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
return obj;
}
@Override
- public void removePort(GraphDBConnection conn, IPortObject port) {
+ public void removePort(IPortObject port) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
// EventGraph<TitanGraph> eg = conn.getEventGraph();
if (fg != null) fg.removeVertex(port.asVertex());
}
@Override
- public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
+ public void removeDevice(IDeviceObject dev) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
if (fg != null) fg.removeVertex(dev.asVertex());
}
@Override
- public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
+ public Iterable<IDeviceObject> getDevices() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
}
@Override
- public IFlowPath searchFlowPath(GraphDBConnection conn,
- FlowId flowId) {
+ public IFlowPath searchFlowPath(FlowId flowId) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
@@ -125,22 +129,20 @@
}
@Override
- public IFlowPath newFlowPath(GraphDBConnection conn) {
+ public IFlowPath newFlowPath() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
return flowPath;
}
@Override
- public void removeFlowPath(GraphDBConnection conn,
- IFlowPath flowPath) {
+ public void removeFlowPath(IFlowPath flowPath) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
fg.removeVertex(flowPath.asVertex());
}
@Override
- public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
- IFlowEntry flowEntry) {
+ public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
pipe.start(flowEntry.asVertex());
@@ -150,7 +152,7 @@
}
@Override
- public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
+ public Iterable<IFlowPath> getAllFlowPaths() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
@@ -165,8 +167,7 @@
}
@Override
- public IFlowEntry searchFlowEntry(GraphDBConnection conn,
- FlowEntryId flowEntryId) {
+ public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
@@ -175,35 +176,34 @@
}
@Override
- public IFlowEntry newFlowEntry(GraphDBConnection conn) {
+ public IFlowEntry newFlowEntry() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
return flowEntry;
}
@Override
- public void removeFlowEntry(GraphDBConnection conn,
- IFlowEntry flowEntry) {
+ public void removeFlowEntry(IFlowEntry flowEntry) {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
fg.removeVertex(flowEntry.asVertex());
}
@Override
- public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
+ public Iterable<IFlowEntry> getAllFlowEntries() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
return fg.getVertices("type", "flow_entry", IFlowEntry.class);
}
@Override
- public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries(GraphDBConnection conn) {
+ public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
//TODO: Should use an enum for flow_switch_state
return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
}
@Override
- public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
+ public Iterable<ISwitchObject> getActiveSwitches() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
@@ -217,14 +217,14 @@
}
@Override
- public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
+ public Iterable<ISwitchObject> getAllSwitches() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
return switches;
}
@Override
- public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
+ public Iterable<ISwitchObject> getInactiveSwitches() {
FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
@@ -238,9 +238,9 @@
}
@Override
- public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
+ public ISwitchObject searchActiveSwitch(String dpid) {
- ISwitchObject sw = searchSwitch(conn, dpid);
+ ISwitchObject sw = searchSwitch(dpid);
if ((sw != null) &&
sw.getState().equals(SwitchState.ACTIVE.toString())) {
return sw;
diff --git a/src/main/java/net/onrc/onos/util/IDBUtils.java b/src/main/java/net/onrc/onos/util/IDBUtils.java
deleted file mode 100644
index 35803d2..0000000
--- a/src/main/java/net/onrc/onos/util/IDBUtils.java
+++ /dev/null
@@ -1,41 +0,0 @@
-package net.onrc.onos.util;
-
-import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
-import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
-import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
-import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
-import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
-import net.floodlightcontroller.util.FlowEntryId;
-import net.floodlightcontroller.util.FlowId;
-
-public interface IDBUtils {
- public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid);
- public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid);
- public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn);
- public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn);
- public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn);
-
-
- 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);
- public Iterable<IDeviceObject> getDevices(GraphDBConnection conn);
- public IFlowPath searchFlowPath(GraphDBConnection conn, FlowId flowId);
- public IFlowPath newFlowPath(GraphDBConnection conn);
- public void removeFlowPath(GraphDBConnection conn, IFlowPath flowPath);
- public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
- IFlowEntry flowEntry);
- public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn);
- public IFlowEntry searchFlowEntry(GraphDBConnection conn,
- FlowEntryId flowEntryId);
- public IFlowEntry newFlowEntry(GraphDBConnection conn);
- public void removeFlowEntry(GraphDBConnection conn,
- IFlowEntry flowEntry);
- public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn);
- public IPortObject newPort(GraphDBConnection conn);
- ISwitchObject newSwitch(GraphDBConnection conn);
- void removePort(GraphDBConnection conn, IPortObject port);
- void removeSwitch(GraphDBConnection conn, ISwitchObject sw);
- Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries(GraphDBConnection conn);
-}