blob: 9a50f47274747b68b69b7d5052e7e8822192fc7b [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;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070037 private static String configFile;
38
Toshio Koideeb88ff62013-06-12 16:46:40 -070039 /*
40 * A private Constructor prevents any other class from instantiating.
41 */
42 private GraphDBConnection() {
43 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070044
Toshio Koideeb88ff62013-06-12 16:46:40 -070045 /* Static 'instance' method */
46 public static synchronized GraphDBConnection getInstance(final String conf) {
47 if (GraphDBConnection.configFile == null
48 || GraphDBConnection.configFile.isEmpty()) {
49 GraphDBConnection.configFile = conf;
50 log.debug("GraphDBConnection::Setting Config File {}",
51 GraphDBConnection.configFile);
52 }
53 if (!GraphDBConnection.configFile.isEmpty()
54 && (graph == null || graph.isOpen() == Boolean.FALSE)) {
55 graph = TitanFactory.open(GraphDBConnection.configFile);
56 // FIXME: Creation on Indexes should be done only once
57 Set<String> s = graph.getIndexedKeys(Vertex.class);
58 if (!s.contains("dpid")) {
59 graph.createKeyIndex("dpid", Vertex.class);
60 }
61 if (!s.contains("type")) {
62 graph.createKeyIndex("type", Vertex.class);
63 }
64 if (!s.contains("dl_address")) {
65 graph.createKeyIndex("dl_address", Vertex.class);
66 }
67 if (!s.contains("flow_id")) {
68 graph.createKeyIndex("flow_id", Vertex.class);
69 }
70 if (!s.contains("flow_entry_id")) {
71 graph.createKeyIndex("flow_entry_id", Vertex.class);
72 }
73 if (!s.contains("switch_state")) {
74 graph.createKeyIndex("switch_state", Vertex.class);
75 }
76 graph.commit();
77 eg = new EventTransactionalGraph<TitanGraph>(graph);
78 }
79 return singleton;
80 }
81
82 public FramedGraph<TitanGraph> getFramedGraph() {
Toshio Koideeb88ff62013-06-12 16:46:40 -070083 if (isValid()) {
84 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
85 return fg;
86 } else {
87 log.error("new FramedGraph failed");
88 return null;
89 }
90 }
91
92 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
Toshio Koideeb88ff62013-06-12 16:46:40 -070093 if (isValid()) {
94 return eg;
95 } else {
96 return null;
97 }
98 }
99
100 public void addEventListener(final LocalGraphChangedListener listener) {
101 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
102 eg.addListener(listener);
103 log.debug("Registered listener {}", listener.getClass());
104 }
105
106 public Boolean isValid() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700107 return (graph != null || graph.isOpen());
108 }
109
Toshio Koidedc949442013-06-18 10:35:51 -0700110 public void commit() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700111 try {
Toshio Koidedc949442013-06-18 10:35:51 -0700112 graph.commit();
113 }
114 catch (Exception e) {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700115 log.error("{}", e.toString());
116 }
117 }
118
Toshio Koidedc949442013-06-18 10:35:51 -0700119 public void rollback() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700120 try {
Toshio Koidedc949442013-06-18 10:35:51 -0700121 graph.rollback();
122 }
123 catch (Exception e) {
124 log.error("{}", e.toString());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700125 }
Toshio Koideeb88ff62013-06-12 16:46:40 -0700126 }
127
128 public void close() {
Toshio Koidedc949442013-06-18 10:35:51 -0700129 commit();
Toshio Koideeb88ff62013-06-12 16:46:40 -0700130 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800131}