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