blob: 6d79de8217b0d3831512268e314d77c8ae60c351 [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 }
84 graph.commit();
85 eg = new EventTransactionalGraph<TitanGraph>(graph);
86 }
87 return singleton;
88 }
89
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070090 /**
Pankaj Berde85016ab2013-06-21 11:34:53 -070091 * Get a FramedGraph instance of the graph.
92 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -070093 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -070094 public FramedGraph<TitanGraph> getFramedGraph() {
95 if (isValid()) {
96 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
97 return fg;
98 } else {
99 log.error("new FramedGraph failed");
100 return null;
101 }
102 }
103
104 /**
105 * Get EventTransactionalGraph of the titan graph.
106 * @return EventTransactionalGraph of the titan graph
107 */
108 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
109 if (isValid()) {
110 return eg;
111 } else {
112 return null;
113 }
114 }
115
116 /**
117 * Add LocalGraphChangedLister for the graph.
118 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700119 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700120 public void addEventListener(final LocalGraphChangedListener listener) {
121 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
122 eg.addListener(listener);
123 log.debug("Registered listener {}", listener.getClass());
124 }
125
126 /**
127 * Return whether this connection is valid.
128 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700129 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700130 public Boolean isValid() {
Yuta HIGUCHI3d30d002013-10-22 11:16:49 -0700131 return (graph != null && graph.isOpen());
Pankaj Berde85016ab2013-06-21 11:34:53 -0700132 }
133
134 /**
135 * Commit changes for the graph operations.
136 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700137 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700138 public void commit() {
139 try {
140 graph.commit();
141 }
142 catch (Exception e) {
143 log.error("{}", e.toString());
144 }
145 }
146
147 /**
148 * Rollback changes for the graph operations.
149 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700150 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700151 public void rollback() {
152 try {
153 graph.rollback();
154 }
155 catch (Exception e) {
156 log.error("{}", e.toString());
157 }
158 }
159
160 /**
161 * Close this database connection.
162 */
Yuta HIGUCHId2fcdd92013-10-22 11:18:58 -0700163 @Override
Pankaj Berde85016ab2013-06-21 11:34:53 -0700164 public void close() {
165 commit();
166 }
167}