blob: a6277cfd7ac155a51097dfda0b92bf7d58be4862 [file] [log] [blame]
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berde3200ea02013-01-04 15:48:36 -08002
Pankaj Berdebbd38612013-06-22 05:59:12 -07003import net.floodlightcontroller.core.IOFSwitch;
Pankaj Berde38646d62013-06-21 11:34:04 -07004import net.onrc.onos.graph.GraphDBConnection;
5import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07006import net.onrc.onos.ofcontroller.core.ISwitchStorage;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
8import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berdeafb20532013-01-08 15:05:24 -08009
Pankaj Berde3200ea02013-01-04 15:48:36 -080010import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080011import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
12import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080015
Teruef33dc32013-06-20 09:54:37 -070016/**
Naoki Shiota987a5722013-10-23 11:59:36 -070017 * This is the class for storing the information of switches into GraphDB
Teruef33dc32013-06-20 09:54:37 -070018 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080019public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070020 protected GraphDBOperation op;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070021 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Teruef33dc32013-06-20 09:54:37 -070022
23 /***
24 * Initialize function. Before you use this class, please call this method
25 * @param conf configuration file for Cassandra DB
26 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080027 @Override
Teruef33dc32013-06-20 09:54:37 -070028 public void init(String conf) {
29 GraphDBConnection conn = GraphDBConnection.getInstance(conf);
30 op = new GraphDBOperation(conn);
31 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080032
Teruef33dc32013-06-20 09:54:37 -070033 /***
34 * Finalize/close function. After you use this class, please call this method.
35 * It will close the DB connection.
36 */
37 public void finalize() {
38 close();
39 }
40
41 /***
42 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070043 * It will close the DB connection. This is for Java garbage collection.
Teruef33dc32013-06-20 09:54:37 -070044 */
45 @Override
46 public void close() {
47 op.close();
48 }
49
Naoki Shiota987a5722013-10-23 11:59:36 -070050 // Method designing policy:
51 // op.commit() and op.rollback() MUST called in public (first-class) methods.
52 // A first-class method MUST NOT call other first-class method.
53 // Routine process should be implemented in private method.
54 // A private method MUST NOT call commit or rollback.
55
Teruef33dc32013-06-20 09:54:37 -070056 /***
Teruef33dc32013-06-20 09:54:37 -070057 * This function is for updating the switch into the DB.
58 * @param dpid The switch dpid you want to update from the DB
59 * @param state The state of the switch like ACTIVE, INACTIVE
60 * @param dmope The DM_OPERATION of the switch
61 */
62 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070063 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
64 boolean success = false;
65 ISwitchObject sw = null;
66
67 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070068 switch(dmope) {
69 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070070 try {
71 sw = op.searchSwitch(dpid);
72 if (sw != null) {
73 setSwitchStateImpl(sw, state);
74 op.commit();
75 success = true;
76 }
77 } catch (Exception e) {
78 op.rollback();
79 e.printStackTrace();
80 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
81 }
82 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070083 case INSERT:
84 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070085 try {
86 sw = addSwitchImpl(dpid);
87 if (sw != null) {
88 if (state != SwitchState.ACTIVE) {
89 setSwitchStateImpl(sw, state);
90 }
91 op.commit();
92 success = true;
93 }
94 } catch (Exception e) {
95 op.rollback();
96 e.printStackTrace();
97 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
98 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070099 break;
100 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700101 try {
102 sw = op.searchSwitch(dpid);
103 if (sw != null) {
104 deleteSwitchImpl(sw);
105 op.commit();
106 success = true;
107 }
108 } catch (Exception e) {
109 op.rollback();
110 e.printStackTrace();
111 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
112 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700113 break;
114 default:
115 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700116
117 return success;
118 }
119
120 @Override
121 public boolean addSwitch(IOFSwitch sw) {
122 boolean success = false;
123
124 String dpid = sw.getStringId();
125 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
126
127 try {
128 ISwitchObject curr = op.searchSwitch(dpid);
129 if (curr != null) {
130 //If existing the switch. set The SW state ACTIVE.
131 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
132 setSwitchStateImpl(curr, SwitchState.ACTIVE);
133 } else {
134 curr = addSwitchImpl(dpid);
135 }
136
137 for (OFPhysicalPort port: sw.getPorts()) {
138 IPortObject p = op.searchPort(dpid, port.getPortNumber());
139 if (p != null) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700140 log.debug("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
Naoki Shiota987a5722013-10-23 11:59:36 -0700141 setPortStateImpl(p, port.getState(), port.getName());
142 p.setState("ACTIVE");
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700143 if (curr.getPort(port.getPortNumber()) == null) {
144 // The port exists but the switch has no "on" link to it
145 curr.addPort(p);
146 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700147 } else {
148 p = addPortImpl(curr, port.getPortNumber());
149 setPortStateImpl(p, port.getState(), port.getName());
150 }
151 }
152 op.commit();
153 success = true;
154 } catch (Exception e) {
155 op.rollback();
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700156 //e.printStackTrace();
157 log.error("SwitchStorage:addSwitch dpid:{} failed: {}", dpid);
158 log.error("switch write error", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700159 }
160
161 return success;
162 }
163
164 /***
165 * This function is for adding the switch into the DB.
166 * @param dpid The switch dpid you want to add into the DB.
167 */
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700168 // This method is only called by tests, so we probably don't need it.
169 // If we need both addSwitch interfaces, one should call the other
170 // rather than implementing the same logic twice.
171 @Deprecated
Naoki Shiota987a5722013-10-23 11:59:36 -0700172 @Override
173 public boolean addSwitch(String dpid) {
174 boolean success = false;
175
176 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
177 try {
178 ISwitchObject sw = op.searchSwitch(dpid);
179 if (sw != null) {
180 //If existing the switch. set The SW state ACTIVE.
181 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
182 setSwitchStateImpl(sw, SwitchState.ACTIVE);
183 } else {
184 addSwitchImpl(dpid);
185 }
186 op.commit();
187 success = true;
188 } catch (Exception e) {
189 op.rollback();
190 e.printStackTrace();
191 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
192 }
193
194 return success;
195 }
196
197 /***
198 * This function is for deleting the switch into the DB.
199 * @param dpid The switch dpid you want to delete from the DB.
200 */
201 @Override
202 public boolean deleteSwitch(String dpid) {
203 boolean success = false;
204
205 try {
206 ISwitchObject sw = op.searchSwitch(dpid);
207 if (sw != null) {
208 deleteSwitchImpl(sw);
209 op.commit();
210 }
211 success = true;
212 } catch (Exception e) {
213 op.rollback();
214 e.printStackTrace();
215 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
216 }
217
218 return success;
219 }
220
221 public boolean updatePort(String dpid, short portNum, int state, String desc) {
222 boolean success = false;
223
224 try {
225 ISwitchObject sw = op.searchSwitch(dpid);
226
227 if (sw != null) {
228 IPortObject p = sw.getPort(portNum);
229 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
230 if (p != null) {
231 setPortStateImpl(p, state, desc);
232 }
233 op.commit();
234 success = true;
235 } else {
236 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
237 }
238 } catch (Exception e) {
239 op.rollback();
240 e.printStackTrace();
241 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
242 }
243
244 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700245 }
246
247 /***
248 * This function is for adding the switch port into the DB.
249 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700250 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700251 */
252 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700253 public boolean addPort(String dpid, OFPhysicalPort phport) {
254 boolean success = false;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700255
Naoki Shiota987a5722013-10-23 11:59:36 -0700256 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
257 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
258 // just dispatch to deletePort()
259 return deletePort(dpid, phport.getPortNumber());
260 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700261
262 try {
263 ISwitchObject sw = op.searchSwitch(dpid);
264
265 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700266 IPortObject p = sw.getPort(phport.getPortNumber());
267 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700268 if (p != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700269 setPortStateImpl(p, phport.getState(), phport.getName());
270 log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700271 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700272 addPortImpl(sw, phport.getPortNumber());
273 setPortStateImpl(p, phport.getState(), phport.getName());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700274 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700275 op.commit();
276 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700277 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700278 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700279 }
280 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700281 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700282 e.printStackTrace();
283 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700284 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700285
286 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700287 }
288
289 /***
290 * This function is for deleting the switch port from the DB.
291 * @param dpid The switch dpid that has the port.
292 * @param port The port you want to delete the switch.
293 */
294 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700295 public boolean deletePort(String dpid, short port) {
296 boolean success = false;
297
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700298 try {
299 ISwitchObject sw = op.searchSwitch(dpid);
300
301 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700302 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700303 if (p != null) {
304 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700305 deletePortImpl(p);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700306 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700307 }
308 }
Pavlin Radoslavov3d99e702013-11-04 15:39:34 -0800309 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700310 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700311 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700312 e.printStackTrace();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700313 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700314 }
315
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700317 }
318
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700319 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700320 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700321 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700322 sw.setState(SwitchState.ACTIVE.toString());
323 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
324 return sw;
325 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700326 return null;
327 }
328 }
329
330 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
331 if (sw != null && state != null) {
332 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700333 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
334 sw.getDPID(), state.toString());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700335 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800336 }
Teruef33dc32013-06-20 09:54:37 -0700337
Naoki Shiota987a5722013-10-23 11:59:36 -0700338 private void deleteSwitchImpl(ISwitchObject sw) {
339 if (sw != null) {
340 op.removeSwitch(sw);
341 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
342 sw.getDPID());
343 }
344 }
345
346 private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
347 IPortObject p = op.newPort(sw.getDPID(), portNum);
348 p.setState("ACTIVE");
349 sw.addPort(p);
350 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
351 sw.getDPID(), portNum);
352
353 return p;
354 }
355
356 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
357 if (port != null) {
358 if (state != null) {
359 port.setPortState(state);
360 }
361 if (desc != null) {
362 port.setDesc(desc);
363 }
364
365 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
366 new Object[] {port.getPortId(), state, desc});
367 }
368 }
369
370 private void deletePortImpl(IPortObject port) {
371 if (port != null) {
372 op.removePort(port);
373 log.info("SwitchStorage:deletePortImpl port:{} done",
374 port.getPortId());
375 }
376 }
Teru4fd58642013-06-21 07:54:49 -0700377}