blob: 01b22aea572f33e5e14ff6dc7a09ae30a1ddd592 [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
3import java.util.Set;
4
Pankaj Berde2239f0d2013-04-04 09:42:43 -07005import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
7
8import com.esotericsoftware.minlog.Log;
Pankaj Berdeda809572013-02-22 15:31:20 -08009import com.thinkaurelius.titan.core.TitanFactory;
10import com.thinkaurelius.titan.core.TitanGraph;
11import com.tinkerpop.blueprints.Vertex;
12import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070013import com.tinkerpop.blueprints.util.wrappers.event.EventGraph;
14import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
15import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalIndexableGraph;
16import com.tinkerpop.blueprints.util.wrappers.event.listener.GraphChangedListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080017import com.tinkerpop.frames.FramedGraph;
18
19public class GraphDBConnection {
20 public enum Transaction {
21 COMMIT,
22 ROLLBACK
23 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070024 public enum GenerateEvent {
25 TRUE,
26 FALSE
27 }
28 protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080029 private static GraphDBConnection singleton = new GraphDBConnection( );
30 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070031 private static EventTransactionalGraph<TitanGraph> eg;
Pankaj Berdeda809572013-02-22 15:31:20 -080032 private static GraphDBUtils utils;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070033 private static String configFile;
34
Pankaj Berdeda809572013-02-22 15:31:20 -080035
36 /* A private Constructor prevents any other
37 * class from instantiating.
38 */
39 private GraphDBConnection(){ }
40
41 /* Static 'instance' method */
Pankaj Berde2239f0d2013-04-04 09:42:43 -070042 public static GraphDBConnection getInstance(final String conf) {
43 if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) {
44 GraphDBConnection.configFile = conf;
45 log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile);
46 }
47 if (!GraphDBConnection.configFile.isEmpty() &&
48 (graph == null||graph.isOpen() == Boolean.FALSE)) {
49 graph = TitanFactory.open(GraphDBConnection.configFile);
Pankaj Berdeda809572013-02-22 15:31:20 -080050 // FIXME: Creation on Indexes should be done only once
51 Set<String> s = graph.getIndexedKeys(Vertex.class);
52 if (!s.contains("dpid")) {
53 graph.createKeyIndex("dpid", Vertex.class);
54 }
55 if (!s.contains("type")) {
56 graph.createKeyIndex("type", Vertex.class);
57 }
58 if (!s.contains("dl_address")) {
59 graph.createKeyIndex("dl_address", Vertex.class);
60 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080061 if (!s.contains("flow_id")) {
62 graph.createKeyIndex("flow_id", Vertex.class);
63 }
64 if (!s.contains("flow_entry_id")) {
65 graph.createKeyIndex("flow_entry_id",
66 Vertex.class);
67 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070068 graph.stopTransaction(Conclusion.SUCCESS);
69 eg = new EventTransactionalGraph<TitanGraph>(graph);
70 }
Pankaj Berdeda809572013-02-22 15:31:20 -080071 if (utils == null) {
72 utils = new GraphDBUtils();
73 }
74 return singleton;
75 }
76
77 public IDBUtils utils() {
78 return utils;
79 }
80
81 protected FramedGraph<TitanGraph> getFramedGraph() {
82
83 if (isValid()) {
84 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
85 return fg;
86 } else {
87 return null;
88 }
89 }
90
Pankaj Berde2239f0d2013-04-04 09:42:43 -070091 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
92
93 if (isValid()) {
94 return eg;
95 } else {
96 return null;
97 }
98 }
99
100 public void addEventListener(final LocalGraphChangedListener listener) {
101 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
102 eg.addListener(listener);
103 log.debug("Registered listener {}",listener.getClass());
104 }
105
Pankaj Berdeda809572013-02-22 15:31:20 -0800106 public Boolean isValid() {
107
108 return (graph != null||graph.isOpen());
109 }
110
111 public void startTx() {
112
113 }
114
115 public void endTx(Transaction tx) {
116 switch (tx) {
117 case COMMIT:
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700118 eg.stopTransaction(Conclusion.SUCCESS);
Pankaj Berdeda809572013-02-22 15:31:20 -0800119 case ROLLBACK:
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700120 eg.stopTransaction(Conclusion.FAILURE);
Pankaj Berdeda809572013-02-22 15:31:20 -0800121 }
122 }
123
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700124 public void endTx(Transaction tx, GenerateEvent fire) {
125
126 try {
127 if (fire.equals(GenerateEvent.TRUE)) {
128 switch (tx) {
129 case COMMIT:
130 eg.stopTransaction(Conclusion.SUCCESS);
131 case ROLLBACK:
132 eg.stopTransaction(Conclusion.FAILURE);
133 }
134 } else {
135 endTx(tx);
136 }
137 } catch (Exception e) {
138 // TODO Auto-generated catch block
139 e.printStackTrace();
140 }
141 }
142
Pankaj Berdeda809572013-02-22 15:31:20 -0800143 public void close() {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700144 graph.shutdown();
Pankaj Berdeda809572013-02-22 15:31:20 -0800145 }
146
147}