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