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; |
| 10 | import com.tinkerpop.blueprints.TransactionalGraph; |
| 11 | import com.tinkerpop.blueprints.Vertex; |
| 12 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 13 | import com.tinkerpop.frames.FramedGraph; |
| 14 | |
| 15 | public 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 HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 32 | protected final static Logger log = LoggerFactory |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 33 | .getLogger(GraphDBConnection.class); |
| 34 | private static GraphDBConnection singleton = new GraphDBConnection(); |
| 35 | private static TitanGraph graph; |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 36 | private static FramedGraph<TitanGraph> fg; |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 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 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 85 | if (!s.contains("ipv4_address")) { |
| 86 | graph.createKeyIndex("ipv4_address", Vertex.class); |
| 87 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 88 | graph.commit(); |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 89 | fg = new FramedGraph<TitanGraph>(graph); |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 90 | eg = new EventTransactionalGraph<TitanGraph>(graph); |
| 91 | } |
| 92 | return singleton; |
| 93 | } |
| 94 | |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 95 | /** |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 96 | * Get a FramedGraph instance of the graph. |
| 97 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 98 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 99 | public FramedGraph<TitanGraph> getFramedGraph() { |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 100 | return fg; |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get EventTransactionalGraph of the titan graph. |
| 105 | * @return EventTransactionalGraph of the titan graph |
| 106 | */ |
| 107 | protected EventTransactionalGraph<TitanGraph> getEventGraph() { |
| 108 | if (isValid()) { |
| 109 | return eg; |
| 110 | } else { |
| 111 | return null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add LocalGraphChangedLister for the graph. |
| 117 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 118 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 119 | public void addEventListener(final LocalGraphChangedListener listener) { |
| 120 | EventTransactionalGraph<TitanGraph> eg = this.getEventGraph(); |
| 121 | eg.addListener(listener); |
| 122 | log.debug("Registered listener {}", listener.getClass()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return whether this connection is valid. |
| 127 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 128 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 129 | public Boolean isValid() { |
Yuta HIGUCHI | 3d30d00 | 2013-10-22 11:16:49 -0700 | [diff] [blame] | 130 | return (graph != null && graph.isOpen()); |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Commit changes for the graph operations. |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 135 | * @throws Exception |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 136 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 137 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 138 | public void commit() { |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 139 | // // Should not catch exception here! |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 140 | // try { |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 141 | graph.commit(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 142 | // } |
| 143 | // catch (Exception e) { |
| 144 | // log.error("{}", e.toString()); |
| 145 | // } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Rollback changes for the graph operations. |
| 150 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 151 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 152 | public void rollback() { |
Yuta HIGUCHI | baa20c9 | 2014-01-02 12:11:28 -0800 | [diff] [blame] | 153 | // Should not catch exception here! |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 154 | // try { |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 155 | graph.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 156 | // } |
| 157 | // catch (Exception e) { |
| 158 | // log.error("{}", e.toString()); |
| 159 | // } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Close this database connection. |
| 164 | */ |
Yuta HIGUCHI | d2fcdd9 | 2013-10-22 11:18:58 -0700 | [diff] [blame] | 165 | @Override |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 166 | public void close() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 167 | try { |
| 168 | commit(); |
| 169 | } catch (Exception e) { |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 170 | log.error("close() failed with exception", e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 171 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 172 | } |
| 173 | } |