blob: 7938bb6e7e159d8d04a90cd3b031ce1d697f401c [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;
10import com.tinkerpop.blueprints.TransactionalGraph;
11import com.tinkerpop.blueprints.Vertex;
12import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
13import com.tinkerpop.frames.FramedGraph;
14
15public class GraphDBConnection implements IDBConnection {
16 public enum Transaction {
17 COMMIT, ROLLBACK
18 }
19
20 public enum GenerateEvent {
21 TRUE, FALSE
22 }
23
24 class TransactionHandle {
25 protected TransactionalGraph tr;
26
27 public void create() {
28 tr = graph.newTransaction();
29 }
30 }
31
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070032 protected final static Logger log = LoggerFactory
Pankaj Berde85016ab2013-06-21 11:34:53 -070033 .getLogger(GraphDBConnection.class);
34 private static GraphDBConnection singleton = new GraphDBConnection();
35 private static TitanGraph graph;
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -080036 private static FramedGraph<TitanGraph> fg;
Pankaj Berde85016ab2013-06-21 11:34:53 -070037 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 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070085 if (!s.contains("ipv4_address")) {
86 graph.createKeyIndex("ipv4_address", Vertex.class);
87 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070088 graph.commit();
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -080089 fg = new FramedGraph<TitanGraph>(graph);
Pankaj Berde85016ab2013-06-21 11:34:53 -070090 eg = new EventTransactionalGraph<TitanGraph>(graph);
91 }
92 return singleton;
93 }
94
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070095 /**
Pankaj Berde85016ab2013-06-21 11:34:53 -070096 * Get a FramedGraph instance of the graph.
97 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070098 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -070099 public FramedGraph<TitanGraph> getFramedGraph() {
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800100 return fg;
Pankaj Berde85016ab2013-06-21 11:34:53 -0700101 }
102
103 /**
104 * Get EventTransactionalGraph of the titan graph.
105 * @return EventTransactionalGraph of the titan graph
106 */
107 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
108 if (isValid()) {
109 return eg;
110 } else {
111 return null;
112 }
113 }
114
115 /**
116 * Add LocalGraphChangedLister for the graph.
117 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700118 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700119 public void addEventListener(final LocalGraphChangedListener listener) {
120 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
121 eg.addListener(listener);
122 log.debug("Registered listener {}", listener.getClass());
123 }
124
125 /**
126 * Return whether this connection is valid.
127 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700128 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700129 public Boolean isValid() {
Yuta HIGUCHI3d30d002013-10-22 11:16:49 -0700130 return (graph != null && graph.isOpen());
Pankaj Berde85016ab2013-06-21 11:34:53 -0700131 }
132
133 /**
134 * Commit changes for the graph operations.
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800135 * @throws Exception
Pankaj Berde85016ab2013-06-21 11:34:53 -0700136 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700137 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700138 public void commit() {
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800139// // Should not catch exception here!
Naoki Shiota987a5722013-10-23 11:59:36 -0700140// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700141 graph.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700142// }
143// catch (Exception e) {
144// log.error("{}", e.toString());
145// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700146 }
147
148 /**
149 * Rollback changes for the graph operations.
150 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700151 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700152 public void rollback() {
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800153 // Should not catch exception here!
Naoki Shiota987a5722013-10-23 11:59:36 -0700154// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700155 graph.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700156// }
157// catch (Exception e) {
158// log.error("{}", e.toString());
159// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700160 }
161
162 /**
163 * Close this database connection.
164 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700165 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700166 public void close() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700167 try {
168 commit();
169 } catch (Exception e) {
170 log.error("{}", e.toString());
171 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700172 }
173}