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