blob: 1cc4e5af51da1efe4118718dba5bfa55208ac1ec [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
3import java.util.Set;
4
Pankaj Berde2239f0d2013-04-04 09:42:43 -07005import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
7
Pankaj Berdeda809572013-02-22 15:31:20 -08008import com.thinkaurelius.titan.core.TitanFactory;
9import com.thinkaurelius.titan.core.TitanGraph;
Pankaj Berdea7095572013-04-05 14:42:17 -070010import com.tinkerpop.blueprints.TransactionalGraph;
Pankaj Berde86a0d412013-04-05 15:06:26 -070011import com.tinkerpop.blueprints.Vertex;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070012import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -080013import com.tinkerpop.frames.FramedGraph;
14
Toshio Koideeb88ff62013-06-12 16:46:40 -070015public class GraphDBConnection implements IDBConnection {
Pankaj Berdeda809572013-02-22 15:31:20 -080016 public enum Transaction {
Toshio Koideeb88ff62013-06-12 16:46:40 -070017 COMMIT, ROLLBACK
Pankaj Berdeda809572013-02-22 15:31:20 -080018 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070019
Pankaj Berde2239f0d2013-04-04 09:42:43 -070020 public enum GenerateEvent {
Toshio Koideeb88ff62013-06-12 16:46:40 -070021 TRUE, FALSE
Pankaj Berde2239f0d2013-04-04 09:42:43 -070022 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070023
Pankaj Berdea7095572013-04-05 14:42:17 -070024 class TransactionHandle {
25 protected TransactionalGraph tr;
Toshio Koideeb88ff62013-06-12 16:46:40 -070026
Pankaj Berdea7095572013-04-05 14:42:17 -070027 public void create() {
Toshio Koideeb88ff62013-06-12 16:46:40 -070028 tr = graph.newTransaction();
Pankaj Berdea7095572013-04-05 14:42:17 -070029 }
30 }
Toshio Koideeb88ff62013-06-12 16:46:40 -070031
32 protected static Logger log = LoggerFactory
33 .getLogger(GraphDBConnection.class);
34 private static GraphDBConnection singleton = new GraphDBConnection();
Pankaj Berdeda809572013-02-22 15:31:20 -080035 private static TitanGraph graph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070036 private static EventTransactionalGraph<TitanGraph> eg;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070037 private static String configFile;
38
Toshio Koideeb88ff62013-06-12 16:46:40 -070039 /*
40 * A private Constructor prevents any other class from instantiating.
41 */
42 private GraphDBConnection() {
43 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -070044
Toshio Koideeb88ff62013-06-12 16:46:40 -070045 /* Static 'instance' method */
Toshio Koidea7cff3d2013-06-19 11:28:24 -070046 /**
47 * Get the instance of GraphDBConnection class.
48 * @param conf the path to the database configuration file.
49 * @return GraphDBConnection instance.
50 */
Toshio Koideeb88ff62013-06-12 16:46:40 -070051 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
Toshio Koidea7cff3d2013-06-19 11:28:24 -070087 /**
88 * Get a FramedGraph instance of the graph.
89 */
Toshio Koideeb88ff62013-06-12 16:46:40 -070090 public FramedGraph<TitanGraph> getFramedGraph() {
Toshio Koideeb88ff62013-06-12 16:46:40 -070091 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
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700100 /**
101 * Get EventTransactionalGraph of the titan graph.
102 * @return EventTransactionalGraph of the titan graph
103 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700104 protected EventTransactionalGraph<TitanGraph> getEventGraph() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700105 if (isValid()) {
106 return eg;
107 } else {
108 return null;
109 }
110 }
111
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700112 /**
113 * Add LocalGraphChangedLister for the graph.
114 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700115 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
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700121 /**
122 * Return whether this connection is valid.
123 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700124 public Boolean isValid() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700125 return (graph != null || graph.isOpen());
126 }
127
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700128 /**
129 * Commit changes for the graph operations.
130 */
Toshio Koidedc949442013-06-18 10:35:51 -0700131 public void commit() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700132 try {
Toshio Koidedc949442013-06-18 10:35:51 -0700133 graph.commit();
134 }
135 catch (Exception e) {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700136 log.error("{}", e.toString());
137 }
138 }
139
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700140 /**
141 * Rollback changes for the graph operations.
142 */
Toshio Koidedc949442013-06-18 10:35:51 -0700143 public void rollback() {
Toshio Koideeb88ff62013-06-12 16:46:40 -0700144 try {
Toshio Koidedc949442013-06-18 10:35:51 -0700145 graph.rollback();
146 }
147 catch (Exception e) {
148 log.error("{}", e.toString());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700149 }
Toshio Koideeb88ff62013-06-12 16:46:40 -0700150 }
151
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700152 /**
153 * Close this database connection.
154 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700155 public void close() {
Toshio Koidedc949442013-06-18 10:35:51 -0700156 commit();
Toshio Koideeb88ff62013-06-12 16:46:40 -0700157 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800158}