Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.onrc.onos.util; |
| 2 | |
| 3 | import java.util.Set; |
| 4 | |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 5 | import org.slf4j.Logger; |
| 6 | import org.slf4j.LoggerFactory; |
| 7 | |
| 8 | import com.esotericsoftware.minlog.Log; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 9 | import com.thinkaurelius.titan.core.TitanFactory; |
| 10 | import com.thinkaurelius.titan.core.TitanGraph; |
| 11 | import com.tinkerpop.blueprints.Vertex; |
| 12 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 13 | import com.tinkerpop.blueprints.util.wrappers.event.EventGraph; |
| 14 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 15 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalIndexableGraph; |
| 16 | import com.tinkerpop.blueprints.util.wrappers.event.listener.GraphChangedListener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 17 | import com.tinkerpop.frames.FramedGraph; |
| 18 | |
| 19 | public class GraphDBConnection { |
| 20 | public enum Transaction { |
| 21 | COMMIT, |
| 22 | ROLLBACK |
| 23 | } |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 24 | public enum GenerateEvent { |
| 25 | TRUE, |
| 26 | FALSE |
| 27 | } |
| 28 | protected static Logger log = LoggerFactory.getLogger(GraphDBConnection.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 29 | private static GraphDBConnection singleton = new GraphDBConnection( ); |
| 30 | private static TitanGraph graph; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 31 | private static EventTransactionalGraph<TitanGraph> eg; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 32 | private static GraphDBUtils utils; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 33 | private static String configFile; |
| 34 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 35 | |
| 36 | /* A private Constructor prevents any other |
| 37 | * class from instantiating. |
| 38 | */ |
| 39 | private GraphDBConnection(){ } |
| 40 | |
| 41 | /* Static 'instance' method */ |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 42 | public static GraphDBConnection getInstance(final String conf) { |
| 43 | if (GraphDBConnection.configFile == null || GraphDBConnection.configFile.isEmpty()) { |
| 44 | GraphDBConnection.configFile = conf; |
| 45 | log.debug("GraphDBConnection::Setting Config File {}", GraphDBConnection.configFile); |
| 46 | } |
| 47 | if (!GraphDBConnection.configFile.isEmpty() && |
| 48 | (graph == null||graph.isOpen() == Boolean.FALSE)) { |
| 49 | graph = TitanFactory.open(GraphDBConnection.configFile); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 50 | // FIXME: Creation on Indexes should be done only once |
| 51 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 52 | if (!s.contains("dpid")) { |
| 53 | graph.createKeyIndex("dpid", Vertex.class); |
| 54 | } |
| 55 | if (!s.contains("type")) { |
| 56 | graph.createKeyIndex("type", Vertex.class); |
| 57 | } |
| 58 | if (!s.contains("dl_address")) { |
| 59 | graph.createKeyIndex("dl_address", Vertex.class); |
| 60 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 61 | if (!s.contains("flow_id")) { |
| 62 | graph.createKeyIndex("flow_id", Vertex.class); |
| 63 | } |
| 64 | if (!s.contains("flow_entry_id")) { |
| 65 | graph.createKeyIndex("flow_entry_id", |
| 66 | Vertex.class); |
| 67 | } |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 68 | graph.stopTransaction(Conclusion.SUCCESS); |
| 69 | eg = new EventTransactionalGraph<TitanGraph>(graph); |
| 70 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 71 | if (utils == null) { |
| 72 | utils = new GraphDBUtils(); |
| 73 | } |
| 74 | return singleton; |
| 75 | } |
| 76 | |
| 77 | public IDBUtils utils() { |
| 78 | return utils; |
| 79 | } |
| 80 | |
| 81 | protected FramedGraph<TitanGraph> getFramedGraph() { |
| 82 | |
| 83 | if (isValid()) { |
| 84 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 85 | return fg; |
| 86 | } else { |
| 87 | return null; |
| 88 | } |
| 89 | } |
| 90 | |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 91 | protected EventTransactionalGraph<TitanGraph> getEventGraph() { |
| 92 | |
| 93 | if (isValid()) { |
| 94 | return eg; |
| 95 | } else { |
| 96 | return null; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void addEventListener(final LocalGraphChangedListener listener) { |
| 101 | EventTransactionalGraph<TitanGraph> eg = this.getEventGraph(); |
| 102 | eg.addListener(listener); |
| 103 | log.debug("Registered listener {}",listener.getClass()); |
| 104 | } |
| 105 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 106 | public Boolean isValid() { |
| 107 | |
| 108 | return (graph != null||graph.isOpen()); |
| 109 | } |
| 110 | |
| 111 | public void startTx() { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | public void endTx(Transaction tx) { |
| 116 | switch (tx) { |
| 117 | case COMMIT: |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 118 | eg.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 119 | case ROLLBACK: |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 120 | eg.stopTransaction(Conclusion.FAILURE); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 124 | public void endTx(Transaction tx, GenerateEvent fire) { |
| 125 | |
| 126 | try { |
| 127 | if (fire.equals(GenerateEvent.TRUE)) { |
| 128 | switch (tx) { |
| 129 | case COMMIT: |
| 130 | eg.stopTransaction(Conclusion.SUCCESS); |
| 131 | case ROLLBACK: |
| 132 | eg.stopTransaction(Conclusion.FAILURE); |
| 133 | } |
| 134 | } else { |
| 135 | endTx(tx); |
| 136 | } |
| 137 | } catch (Exception e) { |
| 138 | // TODO Auto-generated catch block |
| 139 | e.printStackTrace(); |
| 140 | } |
| 141 | } |
| 142 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 143 | public void close() { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 144 | graph.shutdown(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } |