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