blob: 789f3baaf59eb84219886d8001ed230b18e38401 [file] [log] [blame]
Pankaj Berde3200ea02013-01-04 15:48:36 -08001package net.floodlightcontroller.core.internal;
2
3import java.util.Collection;
4import java.util.List;
5
6import org.openflow.protocol.OFPhysicalPort;
7
8import com.thinkaurelius.titan.core.TitanException;
9import com.thinkaurelius.titan.core.TitanFactory;
10import com.thinkaurelius.titan.core.TitanGraph;
11import com.tinkerpop.blueprints.Direction;
12import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
13import com.tinkerpop.blueprints.Edge;
14import com.tinkerpop.blueprints.Vertex;
15import net.floodlightcontroller.core.ISwitchStorage;
16
17public class SwitchStorageImpl implements ISwitchStorage {
18 public TitanGraph graph;
19
20 @Override
21 public void update(String dpid, SwitchState state, DM_OPERATION op) {
22 // TODO Auto-generated method stub
23 switch(op) {
24
25 case UPDATE:
26 case INSERT:
27 case CREATE:
28 addSwitch(dpid);
29 if (state != SwitchState.ACTIVE) {
30 setStatus(dpid, state);
31 }
32 break;
33 case DELETE:
34 deleteSwitch(dpid);
35 break;
36 default:
37 }
38 }
39
40 private void setStatus(String dpid, SwitchState state) {
41 // TODO Auto-generated method stub
42 Vertex sw;
43 try {
44 if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) {
45 sw.setProperty("state",state);
46 graph.stopTransaction(Conclusion.SUCCESS);
47 }
48 } catch (TitanException e) {
49 // TODO: handle exceptions
50 }
51
52
53 }
54
55 @Override
56 public void addPort(String dpid, OFPhysicalPort port) {
57 // TODO Auto-generated method stub
58 Vertex sw;
59 try {
60 if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) {
61 // TODO: Check if port exists
62 Vertex p = graph.addVertex(null);
Pankaj Berde8557a462013-01-07 08:59:31 -080063 p.setProperty("type","port");
Pankaj Berde3200ea02013-01-04 15:48:36 -080064 p.setProperty("number",port.getPortNumber());
65 p.setProperty("state",port.getState());
66 p.setProperty("desc",port.getName());
67 Edge e = graph.addEdge(null, sw, p, "on");
68 e.setProperty("state","ACTIVE");
69 e.setProperty("number", port.getPortNumber());
70
71 graph.stopTransaction(Conclusion.SUCCESS);
72 }
73 } catch (TitanException e) {
74 // TODO: handle exceptions
75 }
76
77 }
78
79 @Override
80 public Collection<OFPhysicalPort> getPorts(long dpid) {
81 // TODO Auto-generated method stub
82 return null;
83 }
84
85 @Override
86 public OFPhysicalPort getPort(String dpid, short portnum) {
87 // TODO Auto-generated method stub
88 return null;
89 }
90
91 @Override
92 public OFPhysicalPort getPort(String dpid, String portName) {
93 // TODO Auto-generated method stub
94 return null;
95 }
96
97 @Override
98 public void addSwitch(String dpid) {
99
100 try {
101 if (graph.getVertices("dpid",dpid).iterator().hasNext()) {
102 /*
103 * Do nothing or throw exception?
104 */
105 } else {
106 Vertex sw = graph.addVertex(null);
107
108 sw.setProperty("type","switch");
109 sw.setProperty("dpid", dpid);
110 sw.setProperty("state",SwitchState.ACTIVE);
111 graph.stopTransaction(Conclusion.SUCCESS);
112 }
113 } catch (TitanException e) {
114 /*
115 * retry till we succeed?
116 */
117 }
118
119
120 }
121
122 @Override
123 public void deleteSwitch(String dpid) {
124 // TODO Setting inactive but we need to eventually remove data
125 setStatus(dpid, SwitchState.INACTIVE);
126
127 }
128
129 @Override
130 public void deletePort(String dpid, short port) {
131 // TODO Auto-generated method stub
132 Vertex sw;
133 try {
134 if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) {
135 // TODO: Check if port exists
136 Vertex p = sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next();
Pankaj Berde8557a462013-01-07 08:59:31 -0800137 if (p != null) {
138 graph.removeVertex(p);
139 graph.stopTransaction(Conclusion.SUCCESS);
140 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800141 }
142 } catch (TitanException e) {
143 // TODO: handle exceptions
144 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800145 }
146
147 @Override
148 public void deletePort(String dpid, String portName) {
149 // TODO Auto-generated method stub
150
151 }
152
153 @Override
154 public List<String> getActiveSwitches() {
155 // TODO Auto-generated method stub
156 return null;
157 }
158
159 @Override
160 public void init(String conf) {
Pankaj Berde8557a462013-01-07 08:59:31 -0800161 //TODO extract the DB location from conf
162 String db = "/tmp/netmap";
163 graph = TitanFactory.open(db);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800164 graph.createKeyIndex("dpid", Vertex.class);
Pankaj Berde8557a462013-01-07 08:59:31 -0800165 graph.createKeyIndex("type", Vertex.class);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800166 }
167
168}