blob: afb8f9bcac64f94f8d2146938c95a0bbc57f1c5a [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
Toshio Koideeb88ff62013-06-12 16:46:40 -070015public class GraphDBConnection implements IDBConnection {
Pankaj Berdeda809572013-02-22 15:31:20 -080016 public enum Transaction {
Toshio Koideeb88ff62013-06-12 16:46:40 -070017 COMMIT, ROLLBACK
Pankaj Berdeda809572013-02-22 15:31:20 -080018 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070019
Pankaj Berde2239f0d2013-04-04 09:42:43 -070020 public enum GenerateEvent {
Toshio Koideeb88ff62013-06-12 16:46:40 -070021 TRUE, FALSE
Pankaj Berde2239f0d2013-04-04 09:42:43 -070022 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070023
Pankaj Berdea7095572013-04-05 14:42:17 -070024 class TransactionHandle {
25 protected TransactionalGraph tr;
Toshio Koideeb88ff62013-06-12 16:46:40 -070026
Pankaj Berdea7095572013-04-05 14:42:17 -070027 public void create() {
Toshio Koideeb88ff62013-06-12 16:46:40 -070028 tr = graph.newTransaction();
Pankaj Berdea7095572013-04-05 14:42:17 -070029 }
30 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070031
32 protected static Logger log = LoggerFactory
33 .getLogger(GraphDBConnection.class);
34 private static GraphDBConnection singleton = new GraphDBConnection();
Pankaj Berdeda809572013-02-22 15:31:20 -080035 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070036 private static EventTransactionalGraph<TitanGraph> eg;
Toshio Koideeb88ff62013-06-12 16:46:40 -070037 private static GraphDBOperation utils;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070038 private static String configFile;
39
Toshio Koideeb88ff62013-06-12 16:46:40 -070040 /*
41 * A private Constructor prevents any other class from instantiating.
42 */
43 private GraphDBConnection() {
44 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070045
Toshio Koideeb88ff62013-06-12 16:46:40 -070046 /* Static 'instance' method */
47 public static synchronized GraphDBConnection getInstance(final String conf) {
48 if (GraphDBConnection.configFile == null
49 || GraphDBConnection.configFile.isEmpty()) {
50 GraphDBConnection.configFile = conf;
51 log.debug("GraphDBConnection::Setting Config File {}",
52 GraphDBConnection.configFile);
53 }
54 if (!GraphDBConnection.configFile.isEmpty()
55 && (graph == null || graph.isOpen() == Boolean.FALSE)) {
56 graph = TitanFactory.open(GraphDBConnection.configFile);
57 // 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 }
68 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", Vertex.class);
73 }
74 if (!s.contains("switch_state")) {
75 graph.createKeyIndex("switch_state", Vertex.class);
76 }
77 graph.commit();
78 eg = new EventTransactionalGraph<TitanGraph>(graph);
79 }
80 return singleton;
81 }
82
83 public FramedGraph<TitanGraph> getFramedGraph() {
84
85 if (isValid()) {
86 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
87 return fg;
88 } else {
89 log.error("new FramedGraph failed");
90 return null;
91 }
92 }
93
94 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
109 public Boolean isValid() {
110
111 return (graph != null || graph.isOpen());
112 }
113
114 public void startTx() {
115
116 }
117
118 public void endTx(Transaction tx) {
119 try {
120 switch (tx) {
121 case COMMIT:
122 graph.commit();
123 case ROLLBACK:
124 graph.rollback();
125 }
126 } catch (Exception e) {
127 // TODO Auto-generated catch block
128 log.error("{}", e.toString());
129 }
130 }
131
132 public void endTx(TransactionHandle tr, Transaction tx) {
133 switch (tx) {
134 case COMMIT:
135 if (tr != null && tr.tr != null) {
136 tr.tr.commit();
137 } else {
138 graph.commit();
139 }
140 case ROLLBACK:
141 if (tr != null && tr.tr != null) {
142 tr.tr.rollback();
143 } else {
144 graph.rollback();
145 }
146 }
147 }
148
149 public void endTx(Transaction tx, GenerateEvent fire) {
150
151 try {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700152 if (fire.equals(GenerateEvent.TRUE)) {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700153 switch (tx) {
154 case COMMIT:
155 eg.commit();
156 case ROLLBACK:
157 eg.rollback();
158 }
159 } else {
160 endTx(tx);
161 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700162 } catch (Exception e) {
163 // TODO Auto-generated catch block
164 e.printStackTrace();
165 }
Toshio Koideeb88ff62013-06-12 16:46:40 -0700166 }
167
168 public void close() {
169 endTx(Transaction.COMMIT);
170 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800171}