blob: bf30297f008fc43c200ec87887e03421a2df9f1f [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;
36 private static EventTransactionalGraph<TitanGraph> eg;
37 private static String configFile;
38
39 /*
40 * A private Constructor prevents any other class from instantiating.
41 */
42 private GraphDBConnection() {
43 }
44
45 /* Static 'instance' method */
46 /**
47 * Get the instance of GraphDBConnection class.
48 * @param conf the path to the database configuration file.
49 * @return GraphDBConnection instance.
50 */
51 public static synchronized GraphDBConnection getInstance(final String conf) {
52 if (GraphDBConnection.configFile == null
53 || GraphDBConnection.configFile.isEmpty()) {
54 GraphDBConnection.configFile = conf;
55 log.debug("GraphDBConnection::Setting Config File {}",
56 GraphDBConnection.configFile);
57 }
58 if (!GraphDBConnection.configFile.isEmpty()
59 && (graph == null || graph.isOpen() == Boolean.FALSE)) {
60 graph = TitanFactory.open(GraphDBConnection.configFile);
61 // FIXME: Creation on Indexes should be done only once
62 Set<String> s = graph.getIndexedKeys(Vertex.class);
63 if (!s.contains("dpid")) {
64 graph.createKeyIndex("dpid", Vertex.class);
65 }
Pankaj Berdebbd38612013-06-22 05:59:12 -070066 if (!s.contains("port_id")) {
67 graph.createKeyIndex("port_id", Vertex.class);
68 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070069 if (!s.contains("type")) {
70 graph.createKeyIndex("type", Vertex.class);
71 }
Pavlin Radoslavov3aed4682013-06-21 13:40:48 -070072 if (!s.contains("dl_addr")) {
73 graph.createKeyIndex("dl_addr", Vertex.class);
Pankaj Berde85016ab2013-06-21 11:34:53 -070074 }
75 if (!s.contains("flow_id")) {
76 graph.createKeyIndex("flow_id", Vertex.class);
77 }
78 if (!s.contains("flow_entry_id")) {
79 graph.createKeyIndex("flow_entry_id", Vertex.class);
80 }
81 if (!s.contains("switch_state")) {
82 graph.createKeyIndex("switch_state", Vertex.class);
83 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070084 if (!s.contains("ipv4_address")) {
85 graph.createKeyIndex("ipv4_address", Vertex.class);
86 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070087 graph.commit();
88 eg = new EventTransactionalGraph<TitanGraph>(graph);
89 }
90 return singleton;
91 }
92
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070093 /**
Pankaj Berde85016ab2013-06-21 11:34:53 -070094 * Get a FramedGraph instance of the graph.
95 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070096 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -070097 public FramedGraph<TitanGraph> getFramedGraph() {
98 if (isValid()) {
99 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
100 return fg;
101 } else {
102 log.error("new FramedGraph failed");
103 return null;
104 }
105 }
106
107 /**
108 * Get EventTransactionalGraph of the titan graph.
109 * @return EventTransactionalGraph of the titan graph
110 */
111 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
112 if (isValid()) {
113 return eg;
114 } else {
115 return null;
116 }
117 }
118
119 /**
120 * Add LocalGraphChangedLister for the graph.
121 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700122 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700123 public void addEventListener(final LocalGraphChangedListener listener) {
124 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
125 eg.addListener(listener);
126 log.debug("Registered listener {}", listener.getClass());
127 }
128
129 /**
130 * Return whether this connection is valid.
131 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700132 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700133 public Boolean isValid() {
Yuta HIGUCHI3d30d002013-10-22 11:16:49 -0700134 return (graph != null && graph.isOpen());
Pankaj Berde85016ab2013-06-21 11:34:53 -0700135 }
136
137 /**
138 * Commit changes for the graph operations.
Naoki Shiota987a5722013-10-23 11:59:36 -0700139 * @throws Exception
Pankaj Berde85016ab2013-06-21 11:34:53 -0700140 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700141 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700142 public void commit() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700143// // Should not catch exception here!
144// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700145 graph.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700146// }
147// catch (Exception e) {
148// log.error("{}", e.toString());
149// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700150 }
151
152 /**
153 * Rollback changes for the graph operations.
154 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700155 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700156 public void rollback() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700157 // Should not catch exception here!
158// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700159 graph.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700160// }
161// catch (Exception e) {
162// log.error("{}", e.toString());
163// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700164 }
165
166 /**
167 * Close this database connection.
168 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700169 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700170 public void close() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 try {
172 commit();
173 } catch (Exception e) {
174 log.error("{}", e.toString());
175 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700176 }
177}