blob: 2f30806d9c75c0fbb340f4bf80f8ad786173fe34 [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
Pankaj Berdeda809572013-02-22 15:31:20 -08008import com.thinkaurelius.titan.core.TitanFactory;
9import com.thinkaurelius.titan.core.TitanGraph;
Pankaj Berdea7095572013-04-05 14:42:17 -070010import com.tinkerpop.blueprints.TransactionalGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -080011import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
Pankaj Berde86a0d412013-04-05 15:06:26 -070012import com.tinkerpop.blueprints.Vertex;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070013import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -080014import com.tinkerpop.frames.FramedGraph;
15
16public class GraphDBConnection {
17 public enum Transaction {
18 COMMIT,
19 ROLLBACK
20 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070021 public enum GenerateEvent {
22 TRUE,
23 FALSE
24 }
Pankaj Berdea7095572013-04-05 14:42:17 -070025 class TransactionHandle {
26 protected TransactionalGraph tr;
27 public void create() {
28 tr = graph.startTransaction();
29 }
30 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070031 protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080032 private static GraphDBConnection singleton = new GraphDBConnection( );
33 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070034 private static EventTransactionalGraph<TitanGraph> eg;
Pankaj Berdeda809572013-02-22 15:31:20 -080035 private static GraphDBUtils utils;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070036 private static String configFile;
37
Pankaj Berdeda809572013-02-22 15:31:20 -080038
39 /* A private Constructor prevents any other
40 * class from instantiating.
41 */
42 private GraphDBConnection(){ }
43
44 /* Static 'instance' method */
Pankaj Berde2239f0d2013-04-04 09:42:43 -070045 public static GraphDBConnection getInstance(final String conf) {
46 if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) {
47 GraphDBConnection.configFile = conf;
48 log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile);
49 }
50 if (!GraphDBConnection.configFile.isEmpty() &&
51 (graph == null||graph.isOpen() == Boolean.FALSE)) {
52 graph = TitanFactory.open(GraphDBConnection.configFile);
Pankaj Berdeda809572013-02-22 15:31:20 -080053 // FIXME: Creation on Indexes should be done only once
54 Set<String> s = graph.getIndexedKeys(Vertex.class);
55 if (!s.contains("dpid")) {
56 graph.createKeyIndex("dpid", Vertex.class);
57 }
58 if (!s.contains("type")) {
59 graph.createKeyIndex("type", Vertex.class);
60 }
61 if (!s.contains("dl_address")) {
62 graph.createKeyIndex("dl_address", Vertex.class);
63 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080064 if (!s.contains("flow_id")) {
65 graph.createKeyIndex("flow_id", Vertex.class);
66 }
67 if (!s.contains("flow_entry_id")) {
68 graph.createKeyIndex("flow_entry_id",
69 Vertex.class);
70 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070071 graph.stopTransaction(Conclusion.SUCCESS);
72 eg = new EventTransactionalGraph<TitanGraph>(graph);
73 }
Pankaj Berdeda809572013-02-22 15:31:20 -080074 if (utils == null) {
75 utils = new GraphDBUtils();
76 }
77 return singleton;
78 }
79
80 public IDBUtils utils() {
81 return utils;
82 }
83
Pankaj Berde86a0d412013-04-05 15:06:26 -070084 public FramedGraph<TitanGraph> getFramedGraph() {
Pankaj Berdeda809572013-02-22 15:31:20 -080085
86 if (isValid()) {
87 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
88 return fg;
89 } else {
90 return null;
91 }
92 }
93
Pankaj Berde2239f0d2013-04-04 09:42:43 -070094 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
95
96 if (isValid()) {
97 return eg;
98 } else {
99 return null;
100 }
101 }
102
103 public void addEventListener(final LocalGraphChangedListener listener) {
104 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
105 eg.addListener(listener);
106 log.debug("Registered listener {}",listener.getClass());
107 }
108
Pankaj Berdeda809572013-02-22 15:31:20 -0800109 public Boolean isValid() {
110
111 return (graph != null||graph.isOpen());
112 }
113
114 public void startTx() {
115
Pankaj Berdea7095572013-04-05 14:42:17 -0700116
Pankaj Berdeda809572013-02-22 15:31:20 -0800117 }
118
119 public void endTx(Transaction tx) {
Pankaj Berde86a0d412013-04-05 15:06:26 -0700120 try {
121 switch (tx) {
122 case COMMIT:
123 graph.stopTransaction(Conclusion.SUCCESS);
124 case ROLLBACK:
125 graph.stopTransaction(Conclusion.FAILURE);
126 }
127 } catch (Exception e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800131 }
132
Pankaj Berdea7095572013-04-05 14:42:17 -0700133 public void endTx(TransactionHandle tr, Transaction tx) {
134 switch (tx) {
135 case COMMIT:
136 if (tr != null && tr.tr != null) {
137 tr.tr.stopTransaction(Conclusion.SUCCESS);
138 } else {
139 graph.stopTransaction(Conclusion.SUCCESS);
140 }
141 case ROLLBACK:
142 if (tr != null && tr.tr != null) {
143 tr.tr.stopTransaction(Conclusion.FAILURE);
144 } else {
145 graph.stopTransaction(Conclusion.FAILURE);
146 }
147 }
148 }
149
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700150 public void endTx(Transaction tx, GenerateEvent fire) {
151
152 try {
153 if (fire.equals(GenerateEvent.TRUE)) {
154 switch (tx) {
155 case COMMIT:
156 eg.stopTransaction(Conclusion.SUCCESS);
157 case ROLLBACK:
158 eg.stopTransaction(Conclusion.FAILURE);
159 }
160 } else {
161 endTx(tx);
162 }
163 } catch (Exception e) {
164 // TODO Auto-generated catch block
165 e.printStackTrace();
166 }
167 }
168
Pankaj Berdeda809572013-02-22 15:31:20 -0800169 public void close() {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700170 graph.shutdown();
Pankaj Berdeda809572013-02-22 15:31:20 -0800171 }
172
173}