Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 1 | package net.onrc.onos.graph; |
| 2 | |
| 3 | import java.util.Set; |
| 4 | |
| 5 | import org.slf4j.Logger; |
| 6 | import org.slf4j.LoggerFactory; |
| 7 | |
| 8 | import com.thinkaurelius.titan.core.TitanFactory; |
| 9 | import com.thinkaurelius.titan.core.TitanGraph; |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 10 | import com.thinkaurelius.titan.diskstorage.StorageException; |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 11 | import com.tinkerpop.blueprints.TransactionalGraph; |
| 12 | import com.tinkerpop.blueprints.Vertex; |
| 13 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 14 | import com.tinkerpop.frames.FramedGraph; |
| 15 | |
| 16 | public class GraphDBConnection implements IDBConnection { |
| 17 | public enum Transaction { |
| 18 | COMMIT, ROLLBACK |
| 19 | } |
| 20 | |
| 21 | public enum GenerateEvent { |
| 22 | TRUE, FALSE |
| 23 | } |
| 24 | |
| 25 | class TransactionHandle { |
| 26 | protected TransactionalGraph tr; |
| 27 | |
| 28 | public void create() { |
| 29 | tr = graph.newTransaction(); |
| 30 | } |
| 31 | } |
| 32 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 33 | protected final static Logger log = LoggerFactory |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 34 | .getLogger(GraphDBConnection.class); |
| 35 | private static GraphDBConnection singleton = new GraphDBConnection(); |
| 36 | private static TitanGraph graph; |
| 37 | private static EventTransactionalGraph<TitanGraph> eg; |
| 38 | private static String configFile; |
| 39 | |
| 40 | /* |
| 41 | * A private Constructor prevents any other class from instantiating. |
| 42 | */ |
| 43 | private GraphDBConnection() { |
| 44 | } |
| 45 | |
| 46 | /* Static 'instance' method */ |
| 47 | /** |
| 48 | * Get the instance of GraphDBConnection class. |
| 49 | * @param conf the path to the database configuration file. |
| 50 | * @return GraphDBConnection instance. |
| 51 | */ |
| 52 | public static synchronized GraphDBConnection getInstance(final String conf) { |
| 53 | if (GraphDBConnection.configFile == null |
| 54 | || GraphDBConnection.configFile.isEmpty()) { |
| 55 | GraphDBConnection.configFile = conf; |
| 56 | log.debug("GraphDBConnection::Setting Config File {}", |
| 57 | GraphDBConnection.configFile); |
| 58 | } |
| 59 | if (!GraphDBConnection.configFile.isEmpty() |
| 60 | && (graph == null || graph.isOpen() == Boolean.FALSE)) { |
| 61 | graph = TitanFactory.open(GraphDBConnection.configFile); |
| 62 | // FIXME: Creation on Indexes should be done only once |
| 63 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 64 | if (!s.contains("dpid")) { |
| 65 | graph.createKeyIndex("dpid", Vertex.class); |
| 66 | } |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 67 | if (!s.contains("port_id")) { |
| 68 | graph.createKeyIndex("port_id", Vertex.class); |
| 69 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 70 | if (!s.contains("type")) { |
| 71 | graph.createKeyIndex("type", Vertex.class); |
| 72 | } |
Pavlin Radoslavov | 3aed468 | 2013-06-21 13:40:48 -0700 | [diff] [blame] | 73 | if (!s.contains("dl_addr")) { |
| 74 | graph.createKeyIndex("dl_addr", Vertex.class); |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 75 | } |
| 76 | if (!s.contains("flow_id")) { |
| 77 | graph.createKeyIndex("flow_id", Vertex.class); |
| 78 | } |
| 79 | if (!s.contains("flow_entry_id")) { |
| 80 | graph.createKeyIndex("flow_entry_id", Vertex.class); |
| 81 | } |
| 82 | if (!s.contains("switch_state")) { |
| 83 | graph.createKeyIndex("switch_state", Vertex.class); |
| 84 | } |
| 85 | graph.commit(); |
| 86 | eg = new EventTransactionalGraph<TitanGraph>(graph); |
| 87 | } |
| 88 | return singleton; |
| 89 | } |
| 90 | |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 91 | /** |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 92 | * Get a FramedGraph instance of the graph. |
| 93 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 94 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 95 | public FramedGraph<TitanGraph> getFramedGraph() { |
| 96 | if (isValid()) { |
| 97 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 98 | return fg; |
| 99 | } else { |
| 100 | log.error("new FramedGraph failed"); |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get EventTransactionalGraph of the titan graph. |
| 107 | * @return EventTransactionalGraph of the titan graph |
| 108 | */ |
| 109 | protected EventTransactionalGraph<TitanGraph> getEventGraph() { |
| 110 | if (isValid()) { |
| 111 | return eg; |
| 112 | } else { |
| 113 | return null; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add LocalGraphChangedLister for the graph. |
| 119 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 120 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 121 | public void addEventListener(final LocalGraphChangedListener listener) { |
| 122 | EventTransactionalGraph<TitanGraph> eg = this.getEventGraph(); |
| 123 | eg.addListener(listener); |
| 124 | log.debug("Registered listener {}", listener.getClass()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return whether this connection is valid. |
| 129 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 130 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 131 | public Boolean isValid() { |
Yuta HIGUCHI | 3d30d00 | 2013-10-22 11:16:49 -0700 | [diff] [blame] | 132 | return (graph != null && graph.isOpen()); |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Commit changes for the graph operations. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 137 | * @throws Exception |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 138 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 139 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 140 | public void commit() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 141 | // // Should not catch exception here! |
| 142 | // try { |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 143 | graph.commit(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 144 | // } |
| 145 | // catch (Exception e) { |
| 146 | // log.error("{}", e.toString()); |
| 147 | // } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Rollback changes for the graph operations. |
| 152 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 153 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 154 | public void rollback() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 155 | // Should not catch exception here! |
| 156 | // try { |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 157 | graph.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 158 | // } |
| 159 | // catch (Exception e) { |
| 160 | // log.error("{}", e.toString()); |
| 161 | // } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Close this database connection. |
| 166 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 167 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 168 | public void close() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 169 | try { |
| 170 | commit(); |
| 171 | } catch (Exception e) { |
| 172 | log.error("{}", e.toString()); |
| 173 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 174 | } |
| 175 | } |