blob: 996388724093079247db005de4af334e94b4ad35 [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;
Pankaj Berde38646d62013-06-21 11:34:04 -07007import net.onrc.onos.graph.GraphDBConnection;
8import net.onrc.onos.graph.GraphDBOperation;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -08009import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070010import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080012import net.onrc.onos.ofcontroller.core.ISwitchStorage;
Pankaj Berdeafb20532013-01-08 15:05:24 -080013
Pankaj Berde3200ea02013-01-04 15:48:36 -080014import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080015import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
16import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070017import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080019
Teruef33dc32013-06-20 09:54:37 -070020/**
Naoki Shiota987a5722013-10-23 11:59:36 -070021 * This is the class for storing the information of switches into GraphDB
Teruef33dc32013-06-20 09:54:37 -070022 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080023public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070024 protected GraphDBOperation op;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070025 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080026
Teruef33dc32013-06-20 09:54:37 -070027 /***
28 * Initialize function. Before you use this class, please call this method
29 * @param conf configuration file for Cassandra DB
30 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080031 @Override
Teruef33dc32013-06-20 09:54:37 -070032 public void init(String conf) {
33 GraphDBConnection conn = GraphDBConnection.getInstance(conf);
34 op = new GraphDBOperation(conn);
35 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080036
Teruef33dc32013-06-20 09:54:37 -070037 /***
38 * Finalize/close function. After you use this class, please call this method.
39 * It will close the DB connection.
40 */
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080041 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080042 protected void finalize() {
Teruef33dc32013-06-20 09:54:37 -070043 close();
44 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080045
Teruef33dc32013-06-20 09:54:37 -070046 /***
47 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070048 * It will close the DB connection. This is for Java garbage collection.
Teruef33dc32013-06-20 09:54:37 -070049 */
50 @Override
51 public void close() {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080052 op.close();
Teruef33dc32013-06-20 09:54:37 -070053 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080054
Naoki Shiota987a5722013-10-23 11:59:36 -070055 // Method designing policy:
56 // op.commit() and op.rollback() MUST called in public (first-class) methods.
57 // A first-class method MUST NOT call other first-class method.
58 // Routine process should be implemented in private method.
59 // A private method MUST NOT call commit or rollback.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080060
Teruef33dc32013-06-20 09:54:37 -070061 /***
Teruef33dc32013-06-20 09:54:37 -070062 * This function is for updating the switch into the DB.
63 * @param dpid The switch dpid you want to update from the DB
64 * @param state The state of the switch like ACTIVE, INACTIVE
65 * @param dmope The DM_OPERATION of the switch
66 */
Jonathan Hartadc63892013-11-08 14:03:55 -080067 /*
68 * Jono, 11/8/2013
69 * We don't need this update method that demultiplexes DM_OPERATIONS,
70 * we can have clients just call the required methods directly.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080071 * We especially don't need this update method to re-implement
Jonathan Hartadc63892013-11-08 14:03:55 -080072 * the functions of other methods.
73 */
74 @Deprecated
Teruef33dc32013-06-20 09:54:37 -070075 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070076 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
77 boolean success = false;
78 ISwitchObject sw = null;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080079
Naoki Shiota987a5722013-10-23 11:59:36 -070080 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070081 switch(dmope) {
82 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070083 try {
84 sw = op.searchSwitch(dpid);
85 if (sw != null) {
86 setSwitchStateImpl(sw, state);
87 op.commit();
88 success = true;
89 }
90 } catch (Exception e) {
91 op.rollback();
92 e.printStackTrace();
93 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
94 }
95 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070096 case INSERT:
97 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070098 try {
99 sw = addSwitchImpl(dpid);
100 if (sw != null) {
101 if (state != SwitchState.ACTIVE) {
102 setSwitchStateImpl(sw, state);
103 }
104 op.commit();
105 success = true;
106 }
107 } catch (Exception e) {
108 op.rollback();
109 e.printStackTrace();
110 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
111 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700112 break;
113 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700114 try {
115 sw = op.searchSwitch(dpid);
116 if (sw != null) {
117 deleteSwitchImpl(sw);
118 op.commit();
119 success = true;
120 }
121 } catch (Exception e) {
122 op.rollback();
123 e.printStackTrace();
124 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
125 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700126 break;
127 default:
128 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800129
Naoki Shiota987a5722013-10-23 11:59:36 -0700130 return success;
131 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800132
Naoki Shiota987a5722013-10-23 11:59:36 -0700133 @Override
134 public boolean addSwitch(IOFSwitch sw) {
135 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800136
Naoki Shiota987a5722013-10-23 11:59:36 -0700137 String dpid = sw.getStringId();
138 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800139
Naoki Shiota987a5722013-10-23 11:59:36 -0700140 try {
141 ISwitchObject curr = op.searchSwitch(dpid);
142 if (curr != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800143 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700144 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
145 setSwitchStateImpl(curr, SwitchState.ACTIVE);
146 } else {
147 curr = addSwitchImpl(dpid);
148 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800149
Naoki Shiota987a5722013-10-23 11:59:36 -0700150 for (OFPhysicalPort port: sw.getPorts()) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800151 //addPort(dpid, port);
152 addPortImpl(curr, port);
153
Naoki Shiota987a5722013-10-23 11:59:36 -0700154 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800155
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800156 // XXX for now delete devices when we change a port to prevent
157 // having stale devices.
158 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
159 deviceStorage.init("");
160 for (IPortObject portObject : curr.getPorts()) {
161 for (IDeviceObject deviceObject : portObject.getDevices()) {
162 // The deviceStorage has to remove on the object gained by its own
163 // FramedGraph, it can't use our objects from here
164 deviceStorage.removeDeviceImpl(deviceStorage.getDeviceByMac(deviceObject.getMACAddress()));
165 }
166 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800167
Naoki Shiota987a5722013-10-23 11:59:36 -0700168 op.commit();
169 success = true;
170 } catch (Exception e) {
171 op.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800172 log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700173 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800174
Naoki Shiota987a5722013-10-23 11:59:36 -0700175 return success;
176 }
177
178 /***
179 * This function is for adding the switch into the DB.
180 * @param dpid The switch dpid you want to add into the DB.
181 */
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700182 // This method is only called by tests, so we probably don't need it.
183 // If we need both addSwitch interfaces, one should call the other
184 // rather than implementing the same logic twice.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800185 @Deprecated
Naoki Shiota987a5722013-10-23 11:59:36 -0700186 @Override
187 public boolean addSwitch(String dpid) {
188 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800189
Naoki Shiota987a5722013-10-23 11:59:36 -0700190 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
191 try {
192 ISwitchObject sw = op.searchSwitch(dpid);
193 if (sw != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800194 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700195 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
196 setSwitchStateImpl(sw, SwitchState.ACTIVE);
197 } else {
198 addSwitchImpl(dpid);
199 }
200 op.commit();
201 success = true;
202 } catch (Exception e) {
203 op.rollback();
204 e.printStackTrace();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800205 log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700206 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800207
Naoki Shiota987a5722013-10-23 11:59:36 -0700208 return success;
209 }
210
211 /***
212 * This function is for deleting the switch into the DB.
213 * @param dpid The switch dpid you want to delete from the DB.
214 */
215 @Override
216 public boolean deleteSwitch(String dpid) {
217 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800218
Naoki Shiota987a5722013-10-23 11:59:36 -0700219 try {
220 ISwitchObject sw = op.searchSwitch(dpid);
221 if (sw != null) {
222 deleteSwitchImpl(sw);
223 op.commit();
224 }
225 success = true;
226 } catch (Exception e) {
227 op.rollback();
228 e.printStackTrace();
229 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
230 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800231
Naoki Shiota987a5722013-10-23 11:59:36 -0700232 return success;
233 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800234
235 @Override
Jonathan Hartadc63892013-11-08 14:03:55 -0800236 public boolean deactivateSwitch(String dpid) {
237 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800238
Jonathan Hartadc63892013-11-08 14:03:55 -0800239 try {
240 ISwitchObject switchObject = op.searchSwitch(dpid);
241 if (switchObject != null) {
242 setSwitchStateImpl(switchObject, SwitchState.INACTIVE);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800243
Jonathan Hartadc63892013-11-08 14:03:55 -0800244 for (IPortObject portObject : switchObject.getPorts()) {
245 portObject.setState("INACTIVE");
246 }
247 op.commit();
248 success = true;
249 }
250 else {
251 log.warn("Switch {} not found when trying to deactivate", dpid);
252 }
253 } catch (Exception e) {
254 // TODO what type of exception is thrown when we can't commit?
255 op.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800256 log.error("SwitchStorage:deactivateSwitch "+dpid+" failed", e);
Jonathan Hartadc63892013-11-08 14:03:55 -0800257 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800258
Jonathan Hartadc63892013-11-08 14:03:55 -0800259 return success;
260 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700261
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800262 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700263 public boolean updatePort(String dpid, short portNum, int state, String desc) {
264 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800265
Naoki Shiota987a5722013-10-23 11:59:36 -0700266 try {
267 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800268
Naoki Shiota987a5722013-10-23 11:59:36 -0700269 if (sw != null) {
270 IPortObject p = sw.getPort(portNum);
271 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
272 if (p != null) {
273 setPortStateImpl(p, state, desc);
Pavlin Radoslavove86bbfc2013-11-05 08:32:14 -0800274 op.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700275 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700276 success = true;
277 } else {
278 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
279 }
280 } catch (Exception e) {
281 op.rollback();
282 e.printStackTrace();
283 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800284 }
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 adding the switch port into the DB.
291 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700292 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700293 */
294 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700295 public boolean addPort(String dpid, OFPhysicalPort phport) {
296 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800297
Naoki Shiota987a5722013-10-23 11:59:36 -0700298 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
299 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
300 // just dispatch to deletePort()
Jonathan Hartadc63892013-11-08 14:03:55 -0800301 // TODO This is wrong. We need to make sure the port is in the
302 // DB with the correct info and port state.
Naoki Shiota987a5722013-10-23 11:59:36 -0700303 return deletePort(dpid, phport.getPortNumber());
304 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800305
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700306 try {
307 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800308
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700309 if (sw != null) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800310 IPortObject portObject = addPortImpl(sw, phport);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800311
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800312 // XXX for now delete devices when we change a port to prevent
313 // having stale devices.
314 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
315 deviceStorage.init("");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800316
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800317 for (IDeviceObject deviceObject : portObject.getDevices()) {
318 deviceStorage.removeDevice(deviceObject);
319 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800320
Naoki Shiota987a5722013-10-23 11:59:36 -0700321 op.commit();
322 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700323 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700324 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700325 }
326 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700327 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700328 e.printStackTrace();
329 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800330 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800331
Naoki Shiota987a5722013-10-23 11:59:36 -0700332 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700333 }
334
335 /***
336 * This function is for deleting the switch port from the DB.
337 * @param dpid The switch dpid that has the port.
338 * @param port The port you want to delete the switch.
339 */
340 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700341 public boolean deletePort(String dpid, short port) {
342 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800343
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800344 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
345 deviceStorage.init("");
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) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700351 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700352 if (p != null) {
353 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Jonathan Hartadc63892013-11-08 14:03:55 -0800354 p.setState("INACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800355
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800356 // XXX for now delete devices when we change a port to prevent
357 // having stale devices.
358 for (IDeviceObject d : p.getDevices()) {
359 deviceStorage.removeDevice(d);
360 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700361 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700362 }
363 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800364
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800365 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700366 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700367 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700368 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800369 log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700370 }
371
Naoki Shiota987a5722013-10-23 11:59:36 -0700372 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700373 }
374
Pavlin Radoslavov64647d22013-11-04 19:07:03 -0800375 /**
376 * Get list of all ports on the switch specified by given DPID.
377 *
378 * @param dpid DPID of desired switch.
379 * @return List of port IDs. Empty list if no port was found.
380 */
381 @Override
382 public List<Short> getPorts(String dpid) {
383 List<Short> ports = new ArrayList<Short>();
384
385 ISwitchObject srcSw = op.searchSwitch(dpid);
386 if (srcSw != null) {
387 for (IPortObject srcPort : srcSw.getPorts()) {
388 ports.add(srcPort.getNumber());
389 }
390 }
391
392 return ports;
393 }
394
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700395 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700396 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700397 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700398 sw.setState(SwitchState.ACTIVE.toString());
399 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
400 return sw;
401 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700402 return null;
403 }
404 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800405
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700406 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
407 if (sw != null && state != null) {
408 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700409 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800410 sw.getDPID(), state);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700411 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800412 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800413
Naoki Shiota987a5722013-10-23 11:59:36 -0700414 private void deleteSwitchImpl(ISwitchObject sw) {
415 if (sw != null) {
416 op.removeSwitch(sw);
417 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
418 sw.getDPID());
419 }
420 }
421
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800422
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800423 private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) {
424 IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800425
426 log.info("SwitchStorage:addPort dpid:{} port:{}",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800427 sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800428
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800429 if (portObject != null) {
430 setPortStateImpl(portObject, phport.getState(), phport.getName());
431 portObject.setState("ACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800432
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800433 // This a convoluted way of checking if the port is attached
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800434 // or not, but doing it this way avoids using the
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800435 // ISwitchObject.getPort method which uses GremlinGroovy query
436 // and takes forever.
437 boolean attached = false;
438 for (IPortObject portsOnSwitch : sw.getPorts()) {
Yuta HIGUCHIaa1fac72013-12-15 14:47:56 -0800439 if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800440 attached = true;
441 break;
442 }
443 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800444
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800445 if (!attached) {
446 sw.addPort(portObject);
447 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800448
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800449 /*
450 if (sw.getPort(phport.getPortNumber()) == null) {
451 // The port exists but the switch has no "on" link to it
452 sw.addPort(portObject);
453 }*/
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800454
455 log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800456 sw.getDPID(), phport.getPortNumber());
457 } else {
458 //addPortImpl(sw, phport.getPortNumber());
459 portObject = op.newPort(sw.getDPID(), phport.getPortNumber());
460 portObject.setState("ACTIVE");
461 setPortStateImpl(portObject, phport.getState(), phport.getName());
462 sw.addPort(portObject);
463 log.info("SwitchStorage:addPort dpid:{} port:{} done",
464 sw.getDPID(), phport.getPortNumber());
465 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800466
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800467 return portObject;
468 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700469 // TODO There's an issue here where a port with that ID could already
470 // exist when we try to add this one (because it's left over from an
471 // old topology). We need to remove an old port with the same ID when
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800472 // we add the new port. Also it seems that old ports like this are
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700473 // never cleaned up and will remain in the DB in the ACTIVE state forever.
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800474 /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700475 IPortObject p = op.newPort(sw.getDPID(), portNum);
476 p.setState("ACTIVE");
477 sw.addPort(p);
478 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
479 sw.getDPID(), portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800480
Naoki Shiota987a5722013-10-23 11:59:36 -0700481 return p;
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800482 }*/
Naoki Shiota987a5722013-10-23 11:59:36 -0700483
484 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
485 if (port != null) {
486 if (state != null) {
487 port.setPortState(state);
488 }
489 if (desc != null) {
490 port.setDesc(desc);
491 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800492
Naoki Shiota987a5722013-10-23 11:59:36 -0700493 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
494 new Object[] {port.getPortId(), state, desc});
495 }
496 }
Teru4fd58642013-06-21 07:54:49 -0700497}