blob: 7672e925acf6d3e7b7fdef8a893e25d880d83ee2 [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
Pavlin Radoslavov64647d22013-11-04 19:07:03 -08003import java.util.ArrayList;
4import java.util.List;
5
Pankaj Berdebbd38612013-06-22 05:59:12 -07006import net.floodlightcontroller.core.IOFSwitch;
yoshi2fd4c7e2013-11-22 15:47:55 -08007import net.onrc.onos.graph.DBOperation;
8import net.onrc.onos.graph.GraphDBManager;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07009import net.onrc.onos.ofcontroller.core.ISwitchStorage;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080010import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070011import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080013import net.onrc.onos.ofcontroller.core.ISwitchStorage;
Pankaj Berdeafb20532013-01-08 15:05:24 -080014
Pankaj Berde3200ea02013-01-04 15:48:36 -080015import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080016import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
17import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070018import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080020
Teruef33dc32013-06-20 09:54:37 -070021/**
Naoki Shiota987a5722013-10-23 11:59:36 -070022 * This is the class for storing the information of switches into GraphDB
Teruef33dc32013-06-20 09:54:37 -070023 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080024public class SwitchStorageImpl implements ISwitchStorage {
Pankaj Berde3200ea02013-01-04 15:48:36 -080025
yoshi0fee3de2013-11-23 09:13:37 -080026 protected DBOperation op;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070027 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
yoshid76fa1f2013-12-19 14:20:34 -080028 public final long measureONOSTimeProp = Long.valueOf(System.getProperty("benchmark.measureONOS", "0"));
Teruef33dc32013-06-20 09:54:37 -070029
Pankaj Berde3200ea02013-01-04 15:48:36 -080030 /***
31 * Initialize function. Before you use this class, please call this method
32 * @param conf configuration file for Cassandra DB
33 */
34 @Override
yoshi0fee3de2013-11-23 09:13:37 -080035 public void init(final String dbStore, final String conf) {
yoshid38cd312013-12-02 19:54:44 -080036 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
37 //op = GraphDBManager.getDBOperation(dbStore, conf);
Pankaj Berde3200ea02013-01-04 15:48:36 -080038 }
yoshi2fd4c7e2013-11-22 15:47:55 -080039
yoshi2fd4c7e2013-11-22 15:47:55 -080040
Teruef33dc32013-06-20 09:54:37 -070041 /***
42 * Finalize/close function. After you use this class, please call this method.
43 * It will close the DB connection.
44 */
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080045 @Override
Teruef33dc32013-06-20 09:54:37 -070046 public void finalize() {
47 close();
48 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080049
Teruef33dc32013-06-20 09:54:37 -070050 /***
51 * Finalize/close function. After you use this class, please call this method.
52 * It will close the DB connection. This is for Java garbage collection.
53 */
54 @Override
55 public void close() {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080056 op.close();
Teruef33dc32013-06-20 09:54:37 -070057 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080058
Naoki Shiota987a5722013-10-23 11:59:36 -070059 // Method designing policy:
60 // op.commit() and op.rollback() MUST called in public (first-class) methods.
61 // A first-class method MUST NOT call other first-class method.
62 // Routine process should be implemented in private method.
63 // A private method MUST NOT call commit or rollback.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080064
Teruef33dc32013-06-20 09:54:37 -070065 /***
Teruef33dc32013-06-20 09:54:37 -070066 * This function is for updating the switch into the DB.
67 * @param dpid The switch dpid you want to update from the DB
68 * @param state The state of the switch like ACTIVE, INACTIVE
69 * @param dmope The DM_OPERATION of the switch
70 */
Jonathan Hartadc63892013-11-08 14:03:55 -080071 /*
72 * Jono, 11/8/2013
73 * We don't need this update method that demultiplexes DM_OPERATIONS,
74 * we can have clients just call the required methods directly.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080075 * We especially don't need this update method to re-implement
Jonathan Hartadc63892013-11-08 14:03:55 -080076 * the functions of other methods.
77 */
78 @Deprecated
Teruef33dc32013-06-20 09:54:37 -070079 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070080 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
81 boolean success = false;
82 ISwitchObject sw = null;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080083
Naoki Shiota987a5722013-10-23 11:59:36 -070084 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070085 switch(dmope) {
86 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070087 try {
88 sw = op.searchSwitch(dpid);
89 if (sw != null) {
90 setSwitchStateImpl(sw, state);
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 }
99 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700100 case INSERT:
101 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700102 try {
103 sw = addSwitchImpl(dpid);
104 if (sw != null) {
105 if (state != SwitchState.ACTIVE) {
106 setSwitchStateImpl(sw, state);
107 }
108 op.commit();
109 success = true;
110 }
111 } catch (Exception e) {
112 op.rollback();
113 e.printStackTrace();
114 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
115 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700116 break;
117 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700118 try {
119 sw = op.searchSwitch(dpid);
120 if (sw != null) {
121 deleteSwitchImpl(sw);
122 op.commit();
123 success = true;
124 }
125 } catch (Exception e) {
126 op.rollback();
127 e.printStackTrace();
128 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
129 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700130 break;
131 default:
132 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800133
Naoki Shiota987a5722013-10-23 11:59:36 -0700134 return success;
135 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800136
Naoki Shiota987a5722013-10-23 11:59:36 -0700137 @Override
138 public boolean addSwitch(IOFSwitch sw) {
139 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800140
Naoki Shiota987a5722013-10-23 11:59:36 -0700141 String dpid = sw.getStringId();
142 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800143 long startSwitchTime = 0, endSwitchTime = 0;
144 long startUpdSwitchTime = 0, endUpdSwitchTime=0;
145 long startPortTime = 0, endPortTime=0;
146 long totalStartTime =0, totalEndTime=0;
147
Naoki Shiota987a5722013-10-23 11:59:36 -0700148 try {
yoshid76fa1f2013-12-19 14:20:34 -0800149 if (measureONOSTimeProp == 1) {
150 totalStartTime = System.nanoTime();
151 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700152 ISwitchObject curr = op.searchSwitch(dpid);
153 if (curr != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800154 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700155 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800156 if (measureONOSTimeProp == 1) {
157 startUpdSwitchTime = System.nanoTime();
158 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700159 setSwitchStateImpl(curr, SwitchState.ACTIVE);
yoshid76fa1f2013-12-19 14:20:34 -0800160 if (measureONOSTimeProp == 1) {
161 endUpdSwitchTime = System.nanoTime();
162 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700163 } else {
yoshid76fa1f2013-12-19 14:20:34 -0800164 if (measureONOSTimeProp == 1) {
165 startSwitchTime = System.nanoTime();
166 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700167 curr = addSwitchImpl(dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800168 if (measureONOSTimeProp == 1) {
169 endSwitchTime = System.nanoTime();
170 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 }
yoshid76fa1f2013-12-19 14:20:34 -0800172 if (measureONOSTimeProp == 1) {
173 startPortTime = System.nanoTime();
174 }
175 long noOfPorts = 0;
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 for (OFPhysicalPort port: sw.getPorts()) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800177 //addPort(dpid, port);
178 addPortImpl(curr, port);
yoshid76fa1f2013-12-19 14:20:34 -0800179 noOfPorts++;
Naoki Shiota987a5722013-10-23 11:59:36 -0700180 }
yoshid76fa1f2013-12-19 14:20:34 -0800181 if (measureONOSTimeProp == 1) {
182 endPortTime = System.nanoTime();
183 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800184 // XXX for now delete devices when we change a port to prevent
185 // having stale devices.
186 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800187 deviceStorage.init("","");
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800188 for (IPortObject portObject : curr.getPorts()) {
189 for (IDeviceObject deviceObject : portObject.getDevices()) {
190 // The deviceStorage has to remove on the object gained by its own
191 // FramedGraph, it can't use our objects from here
192 deviceStorage.removeDeviceImpl(deviceStorage.getDeviceByMac(deviceObject.getMACAddress()));
193 }
194 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800195
Naoki Shiota987a5722013-10-23 11:59:36 -0700196 op.commit();
yoshid76fa1f2013-12-19 14:20:34 -0800197 if (measureONOSTimeProp == 1) {
198 totalEndTime = System.nanoTime();
199 }
200 if (startSwitchTime != 0) {
201 log.error("Performance -- switch add total time {}", endSwitchTime - startSwitchTime);
202 }
203 if (startUpdSwitchTime != 0) {
204 log.error("Performance -- switch update total time {}", endUpdSwitchTime - startUpdSwitchTime);
205 }
206 if (startPortTime != 0) {
207 log.error("Performance @@ port add total time {} no of ports written {}", endPortTime - startPortTime, noOfPorts);
208 }
209 log.error("Performance && total time for add switch {}", totalEndTime - totalStartTime);
Naoki Shiota987a5722013-10-23 11:59:36 -0700210 success = true;
211 } catch (Exception e) {
212 op.rollback();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800213 log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700214 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800215
Naoki Shiota987a5722013-10-23 11:59:36 -0700216 return success;
217 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800218
Naoki Shiota987a5722013-10-23 11:59:36 -0700219 /***
220 * This function is for adding the switch into the DB.
221 * @param dpid The switch dpid you want to add into the DB.
222 */
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700223 // This method is only called by tests, so we probably don't need it.
224 // If we need both addSwitch interfaces, one should call the other
225 // rather than implementing the same logic twice.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800226 @Deprecated
Naoki Shiota987a5722013-10-23 11:59:36 -0700227 @Override
228 public boolean addSwitch(String dpid) {
229 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800230
Naoki Shiota987a5722013-10-23 11:59:36 -0700231 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
232 try {
233 ISwitchObject sw = op.searchSwitch(dpid);
234 if (sw != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800235 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700236 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
237 setSwitchStateImpl(sw, SwitchState.ACTIVE);
238 } else {
239 addSwitchImpl(dpid);
240 }
241 op.commit();
242 success = true;
243 } catch (Exception e) {
244 op.rollback();
245 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800246 log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700247 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800248
Naoki Shiota987a5722013-10-23 11:59:36 -0700249 return success;
250 }
251
252 /***
253 * This function is for deleting the switch into the DB.
254 * @param dpid The switch dpid you want to delete from the DB.
255 */
256 @Override
257 public boolean deleteSwitch(String dpid) {
258 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800259
Naoki Shiota987a5722013-10-23 11:59:36 -0700260 try {
261 ISwitchObject sw = op.searchSwitch(dpid);
262 if (sw != null) {
263 deleteSwitchImpl(sw);
264 op.commit();
265 }
266 success = true;
267 } catch (Exception e) {
268 op.rollback();
269 e.printStackTrace();
270 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
271 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800272
Naoki Shiota987a5722013-10-23 11:59:36 -0700273 return success;
274 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800275
276 @Override
Jonathan Hartadc63892013-11-08 14:03:55 -0800277 public boolean deactivateSwitch(String dpid) {
278 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800279
Jonathan Hartadc63892013-11-08 14:03:55 -0800280 try {
281 ISwitchObject switchObject = op.searchSwitch(dpid);
282 if (switchObject != null) {
283 setSwitchStateImpl(switchObject, SwitchState.INACTIVE);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800284
Jonathan Hartadc63892013-11-08 14:03:55 -0800285 for (IPortObject portObject : switchObject.getPorts()) {
286 portObject.setState("INACTIVE");
287 }
288 op.commit();
289 success = true;
290 }
291 else {
292 log.warn("Switch {} not found when trying to deactivate", dpid);
293 }
294 } catch (Exception e) {
295 // TODO what type of exception is thrown when we can't commit?
296 op.rollback();
297 log.error("SwitchStorage:deactivateSwitch {} failed", dpid, e);
298 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800299
Jonathan Hartadc63892013-11-08 14:03:55 -0800300 return success;
301 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700302
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800303 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700304 public boolean updatePort(String dpid, short portNum, int state, String desc) {
305 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800306
Naoki Shiota987a5722013-10-23 11:59:36 -0700307 try {
308 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800309
Naoki Shiota987a5722013-10-23 11:59:36 -0700310 if (sw != null) {
311 IPortObject p = sw.getPort(portNum);
312 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
313 if (p != null) {
314 setPortStateImpl(p, state, desc);
Pavlin Radoslavove86bbfc2013-11-05 08:32:14 -0800315 op.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700317 success = true;
318 } else {
319 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
320 }
321 } catch (Exception e) {
322 op.rollback();
323 e.printStackTrace();
324 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800325 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700326
327 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700328 }
329
330 /***
331 * This function is for adding the switch port into the DB.
332 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700333 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700334 */
335 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700336 public boolean addPort(String dpid, OFPhysicalPort phport) {
337 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800338
Naoki Shiota987a5722013-10-23 11:59:36 -0700339 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
340 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
341 // just dispatch to deletePort()
Jonathan Hartadc63892013-11-08 14:03:55 -0800342 // TODO This is wrong. We need to make sure the port is in the
343 // DB with the correct info and port state.
Naoki Shiota987a5722013-10-23 11:59:36 -0700344 return deletePort(dpid, phport.getPortNumber());
345 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800346
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700347 try {
348 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800349
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700350 if (sw != null) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800351 IPortObject portObject = addPortImpl(sw, phport);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800352
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800353 // XXX for now delete devices when we change a port to prevent
354 // having stale devices.
355 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800356 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800357
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800358 for (IDeviceObject deviceObject : portObject.getDevices()) {
359 deviceStorage.removeDevice(deviceObject);
360 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800361
Naoki Shiota987a5722013-10-23 11:59:36 -0700362 op.commit();
363 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700364 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700365 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700366 }
367 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700368 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700369 e.printStackTrace();
370 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800371 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800372
Naoki Shiota987a5722013-10-23 11:59:36 -0700373 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700374 }
375
376 /***
377 * This function is for deleting the switch port from the DB.
378 * @param dpid The switch dpid that has the port.
379 * @param port The port you want to delete the switch.
380 */
381 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700382 public boolean deletePort(String dpid, short port) {
383 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800384
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800385 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800386 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800387
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700388 try {
389 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800390
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700391 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700392 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700393 if (p != null) {
394 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Jonathan Hartadc63892013-11-08 14:03:55 -0800395 p.setState("INACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800396
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800397 // XXX for now delete devices when we change a port to prevent
398 // having stale devices.
399 for (IDeviceObject d : p.getDevices()) {
400 deviceStorage.removeDevice(d);
401 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700402 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700403 }
404 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800405
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800406 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700407 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700408 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700409 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800410 log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700411 }
412
Naoki Shiota987a5722013-10-23 11:59:36 -0700413 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700414 }
415
Pavlin Radoslavov64647d22013-11-04 19:07:03 -0800416 /**
417 * Get list of all ports on the switch specified by given DPID.
418 *
419 * @param dpid DPID of desired switch.
420 * @return List of port IDs. Empty list if no port was found.
421 */
422 @Override
423 public List<Short> getPorts(String dpid) {
424 List<Short> ports = new ArrayList<Short>();
425
426 ISwitchObject srcSw = op.searchSwitch(dpid);
427 if (srcSw != null) {
428 for (IPortObject srcPort : srcSw.getPorts()) {
429 ports.add(srcPort.getNumber());
430 }
431 }
432
433 return ports;
434 }
435
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700436 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700437 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700438 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700439 sw.setState(SwitchState.ACTIVE.toString());
440 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
441 return sw;
442 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700443 return null;
444 }
445 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800446
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700447 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
448 if (sw != null && state != null) {
449 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700450 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
451 sw.getDPID(), state.toString());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700452 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800453 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800454
Naoki Shiota987a5722013-10-23 11:59:36 -0700455 private void deleteSwitchImpl(ISwitchObject sw) {
456 if (sw != null) {
457 op.removeSwitch(sw);
458 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
459 sw.getDPID());
yoshi2fd4c7e2013-11-22 15:47:55 -0800460 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700461 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800462
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800463
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800464 private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) {
465 IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800466
467 log.info("SwitchStorage:addPort dpid:{} port:{}",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800468 sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800469
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800470 if (portObject != null) {
471 setPortStateImpl(portObject, phport.getState(), phport.getName());
472 portObject.setState("ACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800473
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800474 // This a convoluted way of checking if the port is attached
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800475 // or not, but doing it this way avoids using the
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800476 // ISwitchObject.getPort method which uses GremlinGroovy query
477 // and takes forever.
478 boolean attached = false;
479 for (IPortObject portsOnSwitch : sw.getPorts()) {
Yuta HIGUCHIaa1fac72013-12-15 14:47:56 -0800480 if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800481 attached = true;
482 break;
483 }
484 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800485
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800486 if (!attached) {
487 sw.addPort(portObject);
488 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800489
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800490 /*
491 if (sw.getPort(phport.getPortNumber()) == null) {
492 // The port exists but the switch has no "on" link to it
493 sw.addPort(portObject);
494 }*/
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800495
496 log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800497 sw.getDPID(), phport.getPortNumber());
498 } else {
499 //addPortImpl(sw, phport.getPortNumber());
500 portObject = op.newPort(sw.getDPID(), phport.getPortNumber());
501 portObject.setState("ACTIVE");
502 setPortStateImpl(portObject, phport.getState(), phport.getName());
503 sw.addPort(portObject);
504 log.info("SwitchStorage:addPort dpid:{} port:{} done",
505 sw.getDPID(), phport.getPortNumber());
506 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800507
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800508 return portObject;
509 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700510 // TODO There's an issue here where a port with that ID could already
511 // exist when we try to add this one (because it's left over from an
512 // old topology). We need to remove an old port with the same ID when
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800513 // we add the new port. Also it seems that old ports like this are
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700514 // never cleaned up and will remain in the DB in the ACTIVE state forever.
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800515 /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700516 IPortObject p = op.newPort(sw.getDPID(), portNum);
517 p.setState("ACTIVE");
518 sw.addPort(p);
519 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
520 sw.getDPID(), portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800521
Naoki Shiota987a5722013-10-23 11:59:36 -0700522 return p;
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800523 }*/
yoshi2fd4c7e2013-11-22 15:47:55 -0800524
Naoki Shiota987a5722013-10-23 11:59:36 -0700525 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
526 if (port != null) {
527 if (state != null) {
528 port.setPortState(state);
529 }
530 if (desc != null) {
531 port.setDesc(desc);
532 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800533
Naoki Shiota987a5722013-10-23 11:59:36 -0700534 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
535 new Object[] {port.getPortId(), state, desc});
536 }
537 }
Yuta HIGUCHIef3828d2013-12-17 22:17:07 -0800538}