blob: 232deed49b732c42c85d87ce16b3778aa1c2f517 [file] [log] [blame]
Pankaj Berde85016ab2013-06-21 11:34:53 -07001package net.onrc.onos.graph;
2
3import java.util.Set;
4
5import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
7
8import com.thinkaurelius.titan.core.TitanFactory;
9import com.thinkaurelius.titan.core.TitanGraph;
Naoki Shiota987a5722013-10-23 11:59:36 -070010import com.thinkaurelius.titan.diskstorage.StorageException;
Pankaj Berde85016ab2013-06-21 11:34:53 -070011import com.tinkerpop.blueprints.TransactionalGraph;
12import com.tinkerpop.blueprints.Vertex;
13import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
14import com.tinkerpop.frames.FramedGraph;
15
16public class GraphDBConnection implements IDBConnection {
17 public enum Transaction {
18 COMMIT, ROLLBACK
19 }
20
21 public enum GenerateEvent {
22 TRUE, FALSE
23 }
24
25 class TransactionHandle {
26 protected TransactionalGraph tr;
27
28 public void create() {
29 tr = graph.newTransaction();
30 }
31 }
32
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070033 protected final static Logger log = LoggerFactory
Pankaj Berde85016ab2013-06-21 11:34:53 -070034 .getLogger(GraphDBConnection.class);
35 private static GraphDBConnection singleton = new GraphDBConnection();
36 private static TitanGraph graph;
37 private static EventTransactionalGraph<TitanGraph> eg;
38 private static String configFile;
39
40 /*
41 * A private Constructor prevents any other class from instantiating.
42 */
43 private GraphDBConnection() {
44 }
45
46 /* Static 'instance' method */
47 /**
48 * Get the instance of GraphDBConnection class.
49 * @param conf the path to the database configuration file.
50 * @return GraphDBConnection instance.
51 */
52 public static synchronized GraphDBConnection getInstance(final String conf) {
53 if (GraphDBConnection.configFile == null
54 || GraphDBConnection.configFile.isEmpty()) {
55 GraphDBConnection.configFile = conf;
56 log.debug("GraphDBConnection::Setting Config File {}",
57 GraphDBConnection.configFile);
58 }
59 if (!GraphDBConnection.configFile.isEmpty()
60 && (graph == null || graph.isOpen() == Boolean.FALSE)) {
61 graph = TitanFactory.open(GraphDBConnection.configFile);
62 // FIXME: Creation on Indexes should be done only once
63 Set<String> s = graph.getIndexedKeys(Vertex.class);
64 if (!s.contains("dpid")) {
65 graph.createKeyIndex("dpid", Vertex.class);
66 }
Pankaj Berdebbd38612013-06-22 05:59:12 -070067 if (!s.contains("port_id")) {
68 graph.createKeyIndex("port_id", Vertex.class);
69 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070070 if (!s.contains("type")) {
71 graph.createKeyIndex("type", Vertex.class);
72 }
Pavlin Radoslavov3aed4682013-06-21 13:40:48 -070073 if (!s.contains("dl_addr")) {
74 graph.createKeyIndex("dl_addr", Vertex.class);
Pankaj Berde85016ab2013-06-21 11:34:53 -070075 }
76 if (!s.contains("flow_id")) {
77 graph.createKeyIndex("flow_id", Vertex.class);
78 }
79 if (!s.contains("flow_entry_id")) {
80 graph.createKeyIndex("flow_entry_id", Vertex.class);
81 }
82 if (!s.contains("switch_state")) {
83 graph.createKeyIndex("switch_state", Vertex.class);
84 }
85 graph.commit();
86 eg = new EventTransactionalGraph<TitanGraph>(graph);
87 }
88 return singleton;
89 }
90
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070091 /**
Pankaj Berde85016ab2013-06-21 11:34:53 -070092 * Get a FramedGraph instance of the graph.
93 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070094 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -070095 public FramedGraph<TitanGraph> getFramedGraph() {
96 if (isValid()) {
97 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
98 return fg;
99 } else {
100 log.error("new FramedGraph failed");
101 return null;
102 }
103 }
104
105 /**
106 * Get EventTransactionalGraph of the titan graph.
107 * @return EventTransactionalGraph of the titan graph
108 */
109 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
110 if (isValid()) {
111 return eg;
112 } else {
113 return null;
114 }
115 }
116
117 /**
118 * Add LocalGraphChangedLister for the graph.
119 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700120 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700121 public void addEventListener(final LocalGraphChangedListener listener) {
122 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
123 eg.addListener(listener);
124 log.debug("Registered listener {}", listener.getClass());
125 }
126
127 /**
128 * Return whether this connection is valid.
129 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700130 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700131 public Boolean isValid() {
Yuta HIGUCHI3d30d002013-10-22 11:16:49 -0700132 return (graph != null && graph.isOpen());
Pankaj Berde85016ab2013-06-21 11:34:53 -0700133 }
134
135 /**
136 * Commit changes for the graph operations.
Naoki Shiota987a5722013-10-23 11:59:36 -0700137 * @throws Exception
Pankaj Berde85016ab2013-06-21 11:34:53 -0700138 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700139 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700140 public void commit() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700141// // Should not catch exception here!
142// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700143 graph.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700144// }
145// catch (Exception e) {
146// log.error("{}", e.toString());
147// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700148 }
149
150 /**
151 * Rollback changes for the graph operations.
152 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700153 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700154 public void rollback() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700155 // Should not catch exception here!
156// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700157 graph.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700158// }
159// catch (Exception e) {
160// log.error("{}", e.toString());
161// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700162 }
163
164 /**
165 * Close this database connection.
166 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700167 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700168 public void close() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700169 try {
170 commit();
171 } catch (Exception e) {
172 log.error("{}", e.toString());
173 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700174 }
175}