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