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