blob: 7ea0ab8b1add860b3e50a549f2b3d39ff780fa0f [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 Berde86a0d412013-04-05 15:06:26 -070011import com.tinkerpop.blueprints.Vertex;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070012import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -080013import com.tinkerpop.frames.FramedGraph;
14
15public class GraphDBConnection {
16 public enum Transaction {
17 COMMIT,
18 ROLLBACK
19 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070020 public enum GenerateEvent {
21 TRUE,
22 FALSE
23 }
Pankaj Berdea7095572013-04-05 14:42:17 -070024 class TransactionHandle {
25 protected TransactionalGraph tr;
26 public void create() {
Pankaj Berde5d506412013-04-23 15:03:02 -070027 tr = graph.newTransaction();
Pankaj Berdea7095572013-04-05 14:42:17 -070028 }
29 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070030 protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080031 private static GraphDBConnection singleton = new GraphDBConnection( );
32 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070033 private static EventTransactionalGraph<TitanGraph> eg;
Pankaj Berdeda809572013-02-22 15:31:20 -080034 private static GraphDBUtils utils;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070035 private static String configFile;
36
Pankaj Berdeda809572013-02-22 15:31:20 -080037
38 /* A private Constructor prevents any other
39 * class from instantiating.
40 */
41 private GraphDBConnection(){ }
42
43 /* Static 'instance' method */
Jonathan Harte6e91872013-04-13 11:10:32 -070044 public static synchronized GraphDBConnection getInstance(final String conf) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -070045 if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) {
46 GraphDBConnection.configFile = conf;
47 log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile);
48 }
49 if (!GraphDBConnection.configFile.isEmpty() &&
50 (graph == null||graph.isOpen() == Boolean.FALSE)) {
51 graph = TitanFactory.open(GraphDBConnection.configFile);
Pankaj Berdeda809572013-02-22 15:31:20 -080052 // FIXME: Creation on Indexes should be done only once
53 Set<String> s = graph.getIndexedKeys(Vertex.class);
54 if (!s.contains("dpid")) {
55 graph.createKeyIndex("dpid", Vertex.class);
56 }
57 if (!s.contains("type")) {
58 graph.createKeyIndex("type", Vertex.class);
59 }
60 if (!s.contains("dl_address")) {
61 graph.createKeyIndex("dl_address", Vertex.class);
62 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080063 if (!s.contains("flow_id")) {
64 graph.createKeyIndex("flow_id", Vertex.class);
65 }
66 if (!s.contains("flow_entry_id")) {
67 graph.createKeyIndex("flow_entry_id",
68 Vertex.class);
69 }
Pankaj Berded1c38592013-04-10 22:46:40 -070070 if (!s.contains("switch_state")) {
71 graph.createKeyIndex("switch_state",
72 Vertex.class);
73 }
Pankaj Berde5d506412013-04-23 15:03:02 -070074 graph.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -070075 eg = new EventTransactionalGraph<TitanGraph>(graph);
76 }
Pankaj Berdeda809572013-02-22 15:31:20 -080077 if (utils == null) {
78 utils = new GraphDBUtils();
79 }
80 return singleton;
81 }
82
83 public IDBUtils utils() {
84 return utils;
85 }
86
Pankaj Berde86a0d412013-04-05 15:06:26 -070087 public FramedGraph<TitanGraph> getFramedGraph() {
Pankaj Berdeda809572013-02-22 15:31:20 -080088
89 if (isValid()) {
90 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
91 return fg;
92 } else {
Pankaj Berde62016142013-04-09 15:35:50 -070093 log.error("new FramedGraph failed");
Pankaj Berdeda809572013-02-22 15:31:20 -080094 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) {
Pankaj Berde86a0d412013-04-05 15:06:26 -0700124 try {
Pankaj Berde62016142013-04-09 15:35:50 -0700125 switch (tx) {
Pankaj Berde86a0d412013-04-05 15:06:26 -0700126 case COMMIT:
Pankaj Berde5d506412013-04-23 15:03:02 -0700127 graph.commit();
Pankaj Berde86a0d412013-04-05 15:06:26 -0700128 case ROLLBACK:
Pankaj Berde5d506412013-04-23 15:03:02 -0700129 graph.rollback();
Pankaj Berde86a0d412013-04-05 15:06:26 -0700130 }
Pankaj Berde62016142013-04-09 15:35:50 -0700131 } catch (Exception e) {
132 // TODO Auto-generated catch block
133 log.error("{}",e.toString());
134 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800135 }
136
Pankaj Berdea7095572013-04-05 14:42:17 -0700137 public void endTx(TransactionHandle tr, Transaction tx) {
138 switch (tx) {
139 case COMMIT:
140 if (tr != null && tr.tr != null) {
Pankaj Berde5d506412013-04-23 15:03:02 -0700141 tr.tr.commit();
Pankaj Berdea7095572013-04-05 14:42:17 -0700142 } else {
Pankaj Berde5d506412013-04-23 15:03:02 -0700143 graph.commit();
Pankaj Berdea7095572013-04-05 14:42:17 -0700144 }
145 case ROLLBACK:
146 if (tr != null && tr.tr != null) {
Pankaj Berde5d506412013-04-23 15:03:02 -0700147 tr.tr.rollback();
Pankaj Berdea7095572013-04-05 14:42:17 -0700148 } else {
Pankaj Berde5d506412013-04-23 15:03:02 -0700149 graph.rollback();
Pankaj Berdea7095572013-04-05 14:42:17 -0700150 }
151 }
152 }
153
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700154 public void endTx(Transaction tx, GenerateEvent fire) {
155
156 try {
157 if (fire.equals(GenerateEvent.TRUE)) {
158 switch (tx) {
159 case COMMIT:
Pankaj Berde5d506412013-04-23 15:03:02 -0700160 eg.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700161 case ROLLBACK:
Pankaj Berde5d506412013-04-23 15:03:02 -0700162 eg.rollback();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700163 }
164 } else {
165 endTx(tx);
166 }
167 } catch (Exception e) {
168 // TODO Auto-generated catch block
169 e.printStackTrace();
170 }
171 }
172
Pankaj Berdeda809572013-02-22 15:31:20 -0800173 public void close() {
Pankaj Berde62016142013-04-09 15:35:50 -0700174 endTx(Transaction.COMMIT);
Pankaj Berde5d506412013-04-23 15:03:02 -0700175 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800176}