blob: 4d23b0dbbd2394cd1f7a2c0cf7fd5980e4711757 [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 {
Pankaj Berde62016142013-04-09 15:35:50 -070090 log.error("new FramedGraph failed");
Pankaj Berdeda809572013-02-22 15:31:20 -080091 return null;
92 }
93 }
94
Pankaj Berde2239f0d2013-04-04 09:42:43 -070095 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
96
97 if (isValid()) {
98 return eg;
99 } else {
100 return null;
101 }
102 }
103
104 public void addEventListener(final LocalGraphChangedListener listener) {
105 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
106 eg.addListener(listener);
107 log.debug("Registered listener {}",listener.getClass());
108 }
109
Pankaj Berdeda809572013-02-22 15:31:20 -0800110 public Boolean isValid() {
111
112 return (graph != null||graph.isOpen());
113 }
114
115 public void startTx() {
116
Pankaj Berdea7095572013-04-05 14:42:17 -0700117
Pankaj Berdeda809572013-02-22 15:31:20 -0800118 }
119
120 public void endTx(Transaction tx) {
Pankaj Berde86a0d412013-04-05 15:06:26 -0700121 try {
Pankaj Berde62016142013-04-09 15:35:50 -0700122 switch (tx) {
Pankaj Berde86a0d412013-04-05 15:06:26 -0700123 case COMMIT:
124 graph.stopTransaction(Conclusion.SUCCESS);
125 case ROLLBACK:
126 graph.stopTransaction(Conclusion.FAILURE);
127 }
Pankaj Berde62016142013-04-09 15:35:50 -0700128 } catch (Exception e) {
129 // TODO Auto-generated catch block
130 log.error("{}",e.toString());
131 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800132 }
133
Pankaj Berdea7095572013-04-05 14:42:17 -0700134 public void endTx(TransactionHandle tr, Transaction tx) {
135 switch (tx) {
136 case COMMIT:
137 if (tr != null && tr.tr != null) {
138 tr.tr.stopTransaction(Conclusion.SUCCESS);
139 } else {
140 graph.stopTransaction(Conclusion.SUCCESS);
141 }
142 case ROLLBACK:
143 if (tr != null && tr.tr != null) {
144 tr.tr.stopTransaction(Conclusion.FAILURE);
145 } else {
146 graph.stopTransaction(Conclusion.FAILURE);
147 }
148 }
149 }
150
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700151 public void endTx(Transaction tx, GenerateEvent fire) {
152
153 try {
154 if (fire.equals(GenerateEvent.TRUE)) {
155 switch (tx) {
156 case COMMIT:
157 eg.stopTransaction(Conclusion.SUCCESS);
158 case ROLLBACK:
159 eg.stopTransaction(Conclusion.FAILURE);
160 }
161 } else {
162 endTx(tx);
163 }
164 } catch (Exception e) {
165 // TODO Auto-generated catch block
166 e.printStackTrace();
167 }
168 }
169
Pankaj Berdeda809572013-02-22 15:31:20 -0800170 public void close() {
Pankaj Berde62016142013-04-09 15:35:50 -0700171 endTx(Transaction.COMMIT);
Pankaj Berdedf871be2013-04-05 18:04:17 -0700172// graph.shutdown();
Pankaj Berdeda809572013-02-22 15:31:20 -0800173 }
174
175}