blob: 53235abd1931aa7a06e990a7fb3d08229877e7bf [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
32 protected static Logger log = LoggerFactory
33 .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 }
66 if (!s.contains("type")) {
67 graph.createKeyIndex("type", Vertex.class);
68 }
69 if (!s.contains("dl_address")) {
70 graph.createKeyIndex("dl_address", Vertex.class);
71 }
72 if (!s.contains("flow_id")) {
73 graph.createKeyIndex("flow_id", Vertex.class);
74 }
75 if (!s.contains("flow_entry_id")) {
76 graph.createKeyIndex("flow_entry_id", Vertex.class);
77 }
78 if (!s.contains("switch_state")) {
79 graph.createKeyIndex("switch_state", Vertex.class);
80 }
81 graph.commit();
82 eg = new EventTransactionalGraph<TitanGraph>(graph);
83 }
84 return singleton;
85 }
86
87 /**
88 * Get a FramedGraph instance of the graph.
89 */
90 public FramedGraph<TitanGraph> getFramedGraph() {
91 if (isValid()) {
92 FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
93 return fg;
94 } else {
95 log.error("new FramedGraph failed");
96 return null;
97 }
98 }
99
100 /**
101 * Get EventTransactionalGraph of the titan graph.
102 * @return EventTransactionalGraph of the titan graph
103 */
104 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
105 if (isValid()) {
106 return eg;
107 } else {
108 return null;
109 }
110 }
111
112 /**
113 * Add LocalGraphChangedLister for the graph.
114 */
115 public void addEventListener(final LocalGraphChangedListener listener) {
116 EventTransactionalGraph<TitanGraph> eg = this.getEventGraph();
117 eg.addListener(listener);
118 log.debug("Registered listener {}", listener.getClass());
119 }
120
121 /**
122 * Return whether this connection is valid.
123 */
124 public Boolean isValid() {
125 return (graph != null || graph.isOpen());
126 }
127
128 /**
129 * Commit changes for the graph operations.
130 */
131 public void commit() {
132 try {
133 graph.commit();
134 }
135 catch (Exception e) {
136 log.error("{}", e.toString());
137 }
138 }
139
140 /**
141 * Rollback changes for the graph operations.
142 */
143 public void rollback() {
144 try {
145 graph.rollback();
146 }
147 catch (Exception e) {
148 log.error("{}", e.toString());
149 }
150 }
151
152 /**
153 * Close this database connection.
154 */
155 public void close() {
156 commit();
157 }
158}