blob: 724095b66e534b0d4e0b3f4f4f7fb1bb158a3edc [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
3import java.util.Set;
4
5import com.thinkaurelius.titan.core.TitanFactory;
6import com.thinkaurelius.titan.core.TitanGraph;
7import com.tinkerpop.blueprints.Vertex;
8import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
9import com.tinkerpop.frames.FramedGraph;
10
11public class GraphDBConnection {
12 public enum Transaction {
13 COMMIT,
14 ROLLBACK
15 }
16 private static GraphDBConnection singleton = new GraphDBConnection( );
17 private static TitanGraph graph;
18 private static GraphDBUtils utils;
19
20 /* A private Constructor prevents any other
21 * class from instantiating.
22 */
23 private GraphDBConnection(){ }
24
25 /* Static 'instance' method */
26 public static GraphDBConnection getInstance(String conf) {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -080027 if (graph == null||graph.isOpen() == Boolean.FALSE) {
Pankaj Berdeda809572013-02-22 15:31:20 -080028 graph = TitanFactory.open(conf);
29 // FIXME: Creation on Indexes should be done only once
30 Set<String> s = graph.getIndexedKeys(Vertex.class);
31 if (!s.contains("dpid")) {
32 graph.createKeyIndex("dpid", Vertex.class);
33 }
34 if (!s.contains("type")) {
35 graph.createKeyIndex("type", Vertex.class);
36 }
37 if (!s.contains("dl_address")) {
38 graph.createKeyIndex("dl_address", Vertex.class);
39 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080040 if (!s.contains("flow_id")) {
41 graph.createKeyIndex("flow_id", Vertex.class);
42 }
43 if (!s.contains("flow_entry_id")) {
44 graph.createKeyIndex("flow_entry_id",
45 Vertex.class);
46 }
Pankaj Berdeda809572013-02-22 15:31:20 -080047 }
48 graph.stopTransaction(Conclusion.SUCCESS);
49 if (utils == null) {
50 utils = new GraphDBUtils();
51 }
52 return singleton;
53 }
54
55 public IDBUtils utils() {
56 return utils;
57 }
58
59 protected FramedGraph<TitanGraph> getFramedGraph() {
60
61 if (isValid()) {
62 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
63 return fg;
64 } else {
65 return null;
66 }
67 }
68
69 public Boolean isValid() {
70
71 return (graph != null||graph.isOpen());
72 }
73
74 public void startTx() {
75
76 }
77
78 public void endTx(Transaction tx) {
79 switch (tx) {
80 case COMMIT:
81 graph.stopTransaction(Conclusion.SUCCESS);
82 case ROLLBACK:
83 graph.stopTransaction(Conclusion.FAILURE);
84 }
85 }
86
87 public void close() {
88
89 }
90
91}