yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | package net.onrc.onos.graph; |
| 2 | |
| 3 | import com.thinkaurelius.titan.core.TitanFactory; |
| 4 | import com.thinkaurelius.titan.core.TitanGraph; |
| 5 | import com.tinkerpop.blueprints.TransactionalGraph; |
| 6 | import com.tinkerpop.blueprints.Vertex; |
| 7 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 8 | import com.tinkerpop.frames.FramedGraph; |
| 9 | import java.util.Set; |
| 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
| 12 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 13 | public class TitanDBConnection extends DBConnection { |
| 14 | |
| 15 | private TitanGraph graph; |
| 16 | private static Logger log = LoggerFactory.getLogger(TitanDBConnection.class); |
| 17 | private EventTransactionalGraph<TitanGraph> eg; |
| 18 | |
| 19 | public TitanDBConnection(final String dbConfigFile) { |
| 20 | graph = TitanFactory.open(dbConfigFile); |
| 21 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 22 | if (!s.contains("dpid")) { |
| 23 | graph.createKeyIndex("dpid", Vertex.class); |
| 24 | } |
| 25 | if (!s.contains("port_id")) { |
| 26 | graph.createKeyIndex("port_id", Vertex.class); |
| 27 | } |
| 28 | if (!s.contains("type")) { |
| 29 | graph.createKeyIndex("type", Vertex.class); |
| 30 | } |
| 31 | if (!s.contains("dl_addr")) { |
| 32 | graph.createKeyIndex("dl_addr", Vertex.class); |
| 33 | } |
| 34 | if (!s.contains("flow_id")) { |
| 35 | graph.createKeyIndex("flow_id", Vertex.class); |
| 36 | } |
| 37 | if (!s.contains("flow_entry_id")) { |
| 38 | graph.createKeyIndex("flow_entry_id", Vertex.class); |
| 39 | } |
| 40 | if (!s.contains("switch_state")) { |
| 41 | graph.createKeyIndex("switch_state", Vertex.class); |
| 42 | } |
| 43 | graph.commit(); |
| 44 | eg = new EventTransactionalGraph<TitanGraph>(graph); |
| 45 | } |
| 46 | |
| 47 | class TransactionHandle { |
| 48 | |
| 49 | protected TransactionalGraph tr; |
| 50 | |
| 51 | public void create() { |
| 52 | tr = graph.newTransaction(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public FramedGraph getFramedGraph() { |
| 58 | if (isValid()) { |
| 59 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 60 | return fg; |
| 61 | } else { |
| 62 | log.error("new FramedGraph failed"); |
| 63 | return null; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void addEventListener(LocalGraphChangedListener listener) { |
| 69 | EventTransactionalGraph<TitanGraph> eg = this.getEventGraph(); |
| 70 | eg.addListener(listener); |
| 71 | log.debug("Registered listener {}", listener.getClass()); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Boolean isValid() { |
| 76 | return (graph != null || graph.isOpen()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void commit() { |
| 81 | try { |
| 82 | graph.commit(); |
| 83 | } catch (Exception e) { |
| 84 | log.error("{}", e.toString()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void rollback() { |
| 90 | try { |
| 91 | graph.rollback(); |
| 92 | } catch (Exception e) { |
| 93 | log.error("{}", e.toString()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void close() { |
| 99 | commit(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get EventTransactionalGraph of the titan graph. |
| 104 | * |
| 105 | * @return EventTransactionalGraph of the titan graph |
| 106 | */ |
| 107 | private EventTransactionalGraph<TitanGraph> getEventGraph() { |
| 108 | if (isValid()) { |
| 109 | return eg; |
| 110 | } else { |
| 111 | return null; |
| 112 | } |
| 113 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 114 | } |