blob: 94ad43c26e6021696d86efacf6568646a1a3022f [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001package net.onrc.onos.graph;
2
3import com.thinkaurelius.titan.core.TitanFactory;
4import com.thinkaurelius.titan.core.TitanGraph;
5import com.tinkerpop.blueprints.TransactionalGraph;
6import com.tinkerpop.blueprints.Vertex;
7import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
8import com.tinkerpop.frames.FramedGraph;
9import java.util.Set;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
yoshi0451f282013-11-22 15:48:55 -080013public 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 }
yoshi0451f282013-11-22 15:48:55 -0800114}