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 | |
| 5 | import com.thinkaurelius.titan.core.TitanFactory; |
| 6 | import com.thinkaurelius.titan.core.TitanGraph; |
| 7 | import com.tinkerpop.blueprints.Vertex; |
| 8 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | |
| 11 | public class GraphDBConnection { |
| 12 | public enum Transaction { |
| 13 | COMMIT, |
| 14 | ROLLBACK |
| 15 | } |
| 16 | private static GraphDBConnection singleton = new GraphDBConnection( ); |
| 17 | private static TitanGraph graph; |
| 18 | private static GraphDBUtils utils; |
| 19 | |
| 20 | /* A private Constructor prevents any other |
| 21 | * class from instantiating. |
| 22 | */ |
| 23 | private GraphDBConnection(){ } |
| 24 | |
| 25 | /* Static 'instance' method */ |
| 26 | public static GraphDBConnection getInstance(String conf) { |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 27 | if (graph == null||graph.isOpen() == Boolean.FALSE) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 28 | graph = TitanFactory.open(conf); |
| 29 | // FIXME: Creation on Indexes should be done only once |
| 30 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 31 | if (!s.contains("dpid")) { |
| 32 | graph.createKeyIndex("dpid", Vertex.class); |
| 33 | } |
| 34 | if (!s.contains("type")) { |
| 35 | graph.createKeyIndex("type", Vertex.class); |
| 36 | } |
| 37 | if (!s.contains("dl_address")) { |
| 38 | graph.createKeyIndex("dl_address", Vertex.class); |
| 39 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 40 | if (!s.contains("flow_id")) { |
| 41 | graph.createKeyIndex("flow_id", Vertex.class); |
| 42 | } |
| 43 | if (!s.contains("flow_entry_id")) { |
| 44 | graph.createKeyIndex("flow_entry_id", |
| 45 | Vertex.class); |
| 46 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 47 | } |
| 48 | graph.stopTransaction(Conclusion.SUCCESS); |
| 49 | if (utils == null) { |
| 50 | utils = new GraphDBUtils(); |
| 51 | } |
| 52 | return singleton; |
| 53 | } |
| 54 | |
| 55 | public IDBUtils utils() { |
| 56 | return utils; |
| 57 | } |
| 58 | |
| 59 | protected FramedGraph<TitanGraph> getFramedGraph() { |
| 60 | |
| 61 | if (isValid()) { |
| 62 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 63 | return fg; |
| 64 | } else { |
| 65 | return null; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public Boolean isValid() { |
| 70 | |
| 71 | return (graph != null||graph.isOpen()); |
| 72 | } |
| 73 | |
| 74 | public void startTx() { |
| 75 | |
| 76 | } |
| 77 | |
| 78 | public void endTx(Transaction tx) { |
| 79 | switch (tx) { |
| 80 | case COMMIT: |
| 81 | graph.stopTransaction(Conclusion.SUCCESS); |
| 82 | case ROLLBACK: |
| 83 | graph.stopTransaction(Conclusion.FAILURE); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void close() { |
| 88 | |
| 89 | } |
| 90 | |
| 91 | } |