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