blob: b504c4b3c613f17a691d0db92c35695dcd672b82 [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;
Jonathan Hartebfae332014-01-15 10:37:47 -080014import com.tinkerpop.frames.FramedGraphFactory;
Jonathan Hart18d976e2014-01-15 10:54:18 -080015import com.tinkerpop.frames.modules.gremlingroovy.GremlinGroovyModule;
Pankaj Berde85016ab2013-06-21 11:34:53 -070016
17public class GraphDBConnection implements IDBConnection {
18 public enum Transaction {
19 COMMIT, ROLLBACK
20 }
21
22 public enum GenerateEvent {
23 TRUE, FALSE
24 }
25
26 class TransactionHandle {
27 protected TransactionalGraph tr;
28
29 public void create() {
30 tr = graph.newTransaction();
31 }
32 }
33
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070034 protected final static Logger log = LoggerFactory
Pankaj Berde85016ab2013-06-21 11:34:53 -070035 .getLogger(GraphDBConnection.class);
36 private static GraphDBConnection singleton = new GraphDBConnection();
37 private static TitanGraph graph;
Jonathan Hartebfae332014-01-15 10:37:47 -080038 private static FramedGraphFactory factory;
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -080039 private static FramedGraph<TitanGraph> fg;
Pankaj Berde85016ab2013-06-21 11:34:53 -070040 private static EventTransactionalGraph<TitanGraph> eg;
41 private static String configFile;
42
43 /*
44 * A private Constructor prevents any other class from instantiating.
45 */
46 private GraphDBConnection() {
47 }
48
49 /* Static 'instance' method */
50 /**
51 * Get the instance of GraphDBConnection class.
52 * @param conf the path to the database configuration file.
53 * @return GraphDBConnection instance.
54 */
55 public static synchronized GraphDBConnection getInstance(final String conf) {
56 if (GraphDBConnection.configFile == null
57 || GraphDBConnection.configFile.isEmpty()) {
58 GraphDBConnection.configFile = conf;
59 log.debug("GraphDBConnection::Setting Config File {}",
60 GraphDBConnection.configFile);
61 }
62 if (!GraphDBConnection.configFile.isEmpty()
63 && (graph == null || graph.isOpen() == Boolean.FALSE)) {
64 graph = TitanFactory.open(GraphDBConnection.configFile);
65 // FIXME: Creation on Indexes should be done only once
66 Set<String> s = graph.getIndexedKeys(Vertex.class);
67 if (!s.contains("dpid")) {
68 graph.createKeyIndex("dpid", Vertex.class);
69 }
Pankaj Berdebbd38612013-06-22 05:59:12 -070070 if (!s.contains("port_id")) {
71 graph.createKeyIndex("port_id", Vertex.class);
72 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070073 if (!s.contains("type")) {
74 graph.createKeyIndex("type", Vertex.class);
75 }
Pavlin Radoslavov3aed4682013-06-21 13:40:48 -070076 if (!s.contains("dl_addr")) {
77 graph.createKeyIndex("dl_addr", Vertex.class);
Pankaj Berde85016ab2013-06-21 11:34:53 -070078 }
79 if (!s.contains("flow_id")) {
80 graph.createKeyIndex("flow_id", Vertex.class);
81 }
82 if (!s.contains("flow_entry_id")) {
83 graph.createKeyIndex("flow_entry_id", Vertex.class);
84 }
85 if (!s.contains("switch_state")) {
86 graph.createKeyIndex("switch_state", Vertex.class);
87 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070088 if (!s.contains("ipv4_address")) {
89 graph.createKeyIndex("ipv4_address", Vertex.class);
90 }
Pankaj Berde85016ab2013-06-21 11:34:53 -070091 graph.commit();
Jonathan Hartebfae332014-01-15 10:37:47 -080092 // Make sure you reuse the factory when creating new framed graphs
Jonathan Hart18d976e2014-01-15 10:54:18 -080093 factory = new FramedGraphFactory(new GremlinGroovyModule());
Jonathan Hartebfae332014-01-15 10:37:47 -080094 fg = factory.create(graph);
Pankaj Berde85016ab2013-06-21 11:34:53 -070095 eg = new EventTransactionalGraph<TitanGraph>(graph);
96 }
97 return singleton;
98 }
99
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700100 /**
Pankaj Berde85016ab2013-06-21 11:34:53 -0700101 * Get a FramedGraph instance of the graph.
102 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700103 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700104 public FramedGraph<TitanGraph> getFramedGraph() {
Jonathan Hartebfae332014-01-15 10:37:47 -0800105 if (isValid()) {
106 return fg;
107 } else {
108 log.error("New FramedGraph failed");
109 return null;
110 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700111 }
112
113 /**
114 * Get EventTransactionalGraph of the titan graph.
115 * @return EventTransactionalGraph of the titan graph
116 */
117 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
118 if (isValid()) {
119 return eg;
120 } else {
121 return null;
122 }
123 }
124
125 /**
126 * Add LocalGraphChangedLister for the graph.
127 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700128 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700129 public void addEventListener(final LocalGraphChangedListener listener) {
130 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
131 eg.addListener(listener);
132 log.debug("Registered listener {}", listener.getClass());
133 }
134
135 /**
136 * Return whether this connection is valid.
137 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700138 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700139 public Boolean isValid() {
Yuta HIGUCHI3d30d002013-10-22 11:16:49 -0700140 return (graph != null && graph.isOpen());
Pankaj Berde85016ab2013-06-21 11:34:53 -0700141 }
142
143 /**
144 * Commit changes for the graph operations.
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800145 * @throws Exception
Pankaj Berde85016ab2013-06-21 11:34:53 -0700146 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700147 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700148 public void commit() {
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800149// // Should not catch exception here!
Naoki Shiota987a5722013-10-23 11:59:36 -0700150// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700151 graph.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700152// }
153// catch (Exception e) {
154// log.error("{}", e.toString());
155// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700156 }
157
158 /**
159 * Rollback changes for the graph operations.
160 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700161 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700162 public void rollback() {
Yuta HIGUCHIbaa20c92014-01-02 12:11:28 -0800163 // Should not catch exception here!
Naoki Shiota987a5722013-10-23 11:59:36 -0700164// try {
Pankaj Berde85016ab2013-06-21 11:34:53 -0700165 graph.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700166// }
167// catch (Exception e) {
168// log.error("{}", e.toString());
169// }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700170 }
171
172 /**
173 * Close this database connection.
174 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700175 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700176 public void close() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700177 try {
178 commit();
179 } catch (Exception e) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800180 log.error("close() failed with exception", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700181 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700182 }
183}