blob: 78f26eac4bfd95f3a6fbb0a271aab39c16cba865 [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;
Pankaj Berdea7095572013-04-05 14:42:17 -070011import com.tinkerpop.blueprints.TransactionalGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -080012import com.tinkerpop.blueprints.Vertex;
13import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070014import com.tinkerpop.blueprints.util.wrappers.event.EventGraph;
15import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
16import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalIndexableGraph;
17import com.tinkerpop.blueprints.util.wrappers.event.listener.GraphChangedListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080018import com.tinkerpop.frames.FramedGraph;
19
20public class GraphDBConnection {
21 public enum Transaction {
22 COMMIT,
23 ROLLBACK
24 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070025 public enum GenerateEvent {
26 TRUE,
27 FALSE
28 }
Pankaj Berdea7095572013-04-05 14:42:17 -070029 class TransactionHandle {
30 protected TransactionalGraph tr;
31 public void create() {
32 tr = graph.startTransaction();
33 }
34 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070035 protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080036 private static GraphDBConnection singleton = new GraphDBConnection( );
37 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070038 private static EventTransactionalGraph<TitanGraph> eg;
Pankaj Berdeda809572013-02-22 15:31:20 -080039 private static GraphDBUtils utils;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070040 private static String configFile;
41
Pankaj Berdeda809572013-02-22 15:31:20 -080042
43 /* A private Constructor prevents any other
44 * class from instantiating.
45 */
46 private GraphDBConnection(){ }
47
48 /* Static 'instance' method */
Pankaj Berde2239f0d2013-04-04 09:42:43 -070049 public static GraphDBConnection getInstance(final String conf) {
50 if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) {
51 GraphDBConnection.configFile = conf;
52 log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile);
53 }
54 if (!GraphDBConnection.configFile.isEmpty() &&
55 (graph == null||graph.isOpen() == Boolean.FALSE)) {
56 graph = TitanFactory.open(GraphDBConnection.configFile);
Pankaj Berdeda809572013-02-22 15:31:20 -080057 // FIXME: Creation on Indexes should be done only once
58 Set<String> s = graph.getIndexedKeys(Vertex.class);
59 if (!s.contains("dpid")) {
60 graph.createKeyIndex("dpid", Vertex.class);
61 }
62 if (!s.contains("type")) {
63 graph.createKeyIndex("type", Vertex.class);
64 }
65 if (!s.contains("dl_address")) {
66 graph.createKeyIndex("dl_address", Vertex.class);
67 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080068 if (!s.contains("flow_id")) {
69 graph.createKeyIndex("flow_id", Vertex.class);
70 }
71 if (!s.contains("flow_entry_id")) {
72 graph.createKeyIndex("flow_entry_id",
73 Vertex.class);
74 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070075 graph.stopTransaction(Conclusion.SUCCESS);
76 eg = new EventTransactionalGraph<TitanGraph>(graph);
77 }
Pankaj Berdeda809572013-02-22 15:31:20 -080078 if (utils == null) {
79 utils = new GraphDBUtils();
80 }
81 return singleton;
82 }
83
84 public IDBUtils utils() {
85 return utils;
86 }
87
88 protected FramedGraph<TitanGraph> getFramedGraph() {
89
90 if (isValid()) {
91 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
92 return fg;
93 } else {
94 return null;
95 }
96 }
97
Pankaj Berde2239f0d2013-04-04 09:42:43 -070098 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
99
100 if (isValid()) {
101 return eg;
102 } else {
103 return null;
104 }
105 }
106
107 public void addEventListener(final LocalGraphChangedListener listener) {
108 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
109 eg.addListener(listener);
110 log.debug("Registered listener {}",listener.getClass());
111 }
112
Pankaj Berdeda809572013-02-22 15:31:20 -0800113 public Boolean isValid() {
114
115 return (graph != null||graph.isOpen());
116 }
117
118 public void startTx() {
119
Pankaj Berdea7095572013-04-05 14:42:17 -0700120
Pankaj Berdeda809572013-02-22 15:31:20 -0800121 }
122
123 public void endTx(Transaction tx) {
124 switch (tx) {
125 case COMMIT:
Pankaj Berdea7095572013-04-05 14:42:17 -0700126 graph.stopTransaction(Conclusion.SUCCESS);
Pankaj Berdeda809572013-02-22 15:31:20 -0800127 case ROLLBACK:
Pankaj Berdea7095572013-04-05 14:42:17 -0700128 graph.stopTransaction(Conclusion.FAILURE);
Pankaj Berdeda809572013-02-22 15:31:20 -0800129 }
130 }
131
Pankaj Berdea7095572013-04-05 14:42:17 -0700132 public void endTx(TransactionHandle tr, Transaction tx) {
133 switch (tx) {
134 case COMMIT:
135 if (tr != null && tr.tr != null) {
136 tr.tr.stopTransaction(Conclusion.SUCCESS);
137 } else {
138 graph.stopTransaction(Conclusion.SUCCESS);
139 }
140 case ROLLBACK:
141 if (tr != null && tr.tr != null) {
142 tr.tr.stopTransaction(Conclusion.FAILURE);
143 } else {
144 graph.stopTransaction(Conclusion.FAILURE);
145 }
146 }
147 }
148
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700149 public void endTx(Transaction tx, GenerateEvent fire) {
150
151 try {
152 if (fire.equals(GenerateEvent.TRUE)) {
153 switch (tx) {
154 case COMMIT:
155 eg.stopTransaction(Conclusion.SUCCESS);
156 case ROLLBACK:
157 eg.stopTransaction(Conclusion.FAILURE);
158 }
159 } else {
160 endTx(tx);
161 }
162 } catch (Exception e) {
163 // TODO Auto-generated catch block
164 e.printStackTrace();
165 }
166 }
167
Pankaj Berdeda809572013-02-22 15:31:20 -0800168 public void close() {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700169 graph.shutdown();
Pankaj Berdeda809572013-02-22 15:31:20 -0800170 }
171
172}