blob: cb535b2088a07e5c200b3e37d8e8be9444e3f797 [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);
Teruef33dc32013-06-20 09:54:37 -070028
Pankaj Berde3200ea02013-01-04 15:48:36 -080029 /***
30 * Initialize function. Before you use this class, please call this method
31 * @param conf configuration file for Cassandra DB
32 */
33 @Override
yoshi0fee3de2013-11-23 09:13:37 -080034 public void init(final String dbStore, final String conf) {
yoshid38cd312013-12-02 19:54:44 -080035 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
36 //op = GraphDBManager.getDBOperation(dbStore, conf);
Pankaj Berde3200ea02013-01-04 15:48:36 -080037 }
yoshi2fd4c7e2013-11-22 15:47:55 -080038
yoshi2fd4c7e2013-11-22 15:47:55 -080039
Teruef33dc32013-06-20 09:54:37 -070040 /***
41 * Finalize/close function. After you use this class, please call this method.
42 * It will close the DB connection.
43 */
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080044 @Override
Teruef33dc32013-06-20 09:54:37 -070045 public void finalize() {
46 close();
47 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080048
Teruef33dc32013-06-20 09:54:37 -070049 /***
50 * Finalize/close function. After you use this class, please call this method.
51 * It will close the DB connection. This is for Java garbage collection.
52 */
53 @Override
54 public void close() {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080055 op.close();
Teruef33dc32013-06-20 09:54:37 -070056 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080057
Naoki Shiota987a5722013-10-23 11:59:36 -070058 // Method designing policy:
59 // op.commit() and op.rollback() MUST called in public (first-class) methods.
60 // A first-class method MUST NOT call other first-class method.
61 // Routine process should be implemented in private method.
62 // A private method MUST NOT call commit or rollback.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080063
Teruef33dc32013-06-20 09:54:37 -070064 /***
Teruef33dc32013-06-20 09:54:37 -070065 * This function is for updating the switch into the DB.
66 * @param dpid The switch dpid you want to update from the DB
67 * @param state The state of the switch like ACTIVE, INACTIVE
68 * @param dmope The DM_OPERATION of the switch
69 */
Jonathan Hartadc63892013-11-08 14:03:55 -080070 /*
71 * Jono, 11/8/2013
72 * We don't need this update method that demultiplexes DM_OPERATIONS,
73 * we can have clients just call the required methods directly.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080074 * We especially don't need this update method to re-implement
Jonathan Hartadc63892013-11-08 14:03:55 -080075 * the functions of other methods.
76 */
77 @Deprecated
Teruef33dc32013-06-20 09:54:37 -070078 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070079 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
80 boolean success = false;
81 ISwitchObject sw = null;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080082
Naoki Shiota987a5722013-10-23 11:59:36 -070083 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070084 switch(dmope) {
85 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070086 try {
87 sw = op.searchSwitch(dpid);
88 if (sw != null) {
89 setSwitchStateImpl(sw, state);
90 op.commit();
91 success = true;
92 }
93 } catch (Exception e) {
94 op.rollback();
95 e.printStackTrace();
96 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
97 }
98 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070099 case INSERT:
100 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700101 try {
102 sw = addSwitchImpl(dpid);
103 if (sw != null) {
104 if (state != SwitchState.ACTIVE) {
105 setSwitchStateImpl(sw, state);
106 }
107 op.commit();
108 success = true;
109 }
110 } catch (Exception e) {
111 op.rollback();
112 e.printStackTrace();
113 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
114 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700115 break;
116 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700117 try {
118 sw = op.searchSwitch(dpid);
119 if (sw != null) {
120 deleteSwitchImpl(sw);
121 op.commit();
122 success = true;
123 }
124 } catch (Exception e) {
125 op.rollback();
126 e.printStackTrace();
127 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
128 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700129 break;
130 default:
131 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800132
Naoki Shiota987a5722013-10-23 11:59:36 -0700133 return success;
134 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800135
Naoki Shiota987a5722013-10-23 11:59:36 -0700136 @Override
137 public boolean addSwitch(IOFSwitch sw) {
138 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800139
Naoki Shiota987a5722013-10-23 11:59:36 -0700140 String dpid = sw.getStringId();
141 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800142
Naoki Shiota987a5722013-10-23 11:59:36 -0700143 try {
144 ISwitchObject curr = op.searchSwitch(dpid);
145 if (curr != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800146 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700147 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
148 setSwitchStateImpl(curr, SwitchState.ACTIVE);
149 } else {
150 curr = addSwitchImpl(dpid);
151 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800152
Naoki Shiota987a5722013-10-23 11:59:36 -0700153 for (OFPhysicalPort port: sw.getPorts()) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800154 //addPort(dpid, port);
155 addPortImpl(curr, port);
yoshi2fd4c7e2013-11-22 15:47:55 -0800156
Naoki Shiota987a5722013-10-23 11:59:36 -0700157 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800158
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800159 // XXX for now delete devices when we change a port to prevent
160 // having stale devices.
161 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800162 deviceStorage.init("","");
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800163 for (IPortObject portObject : curr.getPorts()) {
164 for (IDeviceObject deviceObject : portObject.getDevices()) {
165 // The deviceStorage has to remove on the object gained by its own
166 // FramedGraph, it can't use our objects from here
167 deviceStorage.removeDeviceImpl(deviceStorage.getDeviceByMac(deviceObject.getMACAddress()));
168 }
169 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800170
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 op.commit();
172 success = true;
173 } catch (Exception e) {
174 op.rollback();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800175 log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800177
Naoki Shiota987a5722013-10-23 11:59:36 -0700178 return success;
179 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800180
Naoki Shiota987a5722013-10-23 11:59:36 -0700181 /***
182 * This function is for adding the switch into the DB.
183 * @param dpid The switch dpid you want to add into the DB.
184 */
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700185 // This method is only called by tests, so we probably don't need it.
186 // If we need both addSwitch interfaces, one should call the other
187 // rather than implementing the same logic twice.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800188 @Deprecated
Naoki Shiota987a5722013-10-23 11:59:36 -0700189 @Override
190 public boolean addSwitch(String dpid) {
191 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800192
Naoki Shiota987a5722013-10-23 11:59:36 -0700193 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
194 try {
195 ISwitchObject sw = op.searchSwitch(dpid);
196 if (sw != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800197 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700198 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
199 setSwitchStateImpl(sw, SwitchState.ACTIVE);
200 } else {
201 addSwitchImpl(dpid);
202 }
203 op.commit();
204 success = true;
205 } catch (Exception e) {
206 op.rollback();
207 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800208 log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700209 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800210
Naoki Shiota987a5722013-10-23 11:59:36 -0700211 return success;
212 }
213
214 /***
215 * This function is for deleting the switch into the DB.
216 * @param dpid The switch dpid you want to delete from the DB.
217 */
218 @Override
219 public boolean deleteSwitch(String dpid) {
220 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800221
Naoki Shiota987a5722013-10-23 11:59:36 -0700222 try {
223 ISwitchObject sw = op.searchSwitch(dpid);
224 if (sw != null) {
225 deleteSwitchImpl(sw);
226 op.commit();
227 }
228 success = true;
229 } catch (Exception e) {
230 op.rollback();
231 e.printStackTrace();
232 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
233 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800234
Naoki Shiota987a5722013-10-23 11:59:36 -0700235 return success;
236 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800237
238 @Override
Jonathan Hartadc63892013-11-08 14:03:55 -0800239 public boolean deactivateSwitch(String dpid) {
240 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800241
Jonathan Hartadc63892013-11-08 14:03:55 -0800242 try {
243 ISwitchObject switchObject = op.searchSwitch(dpid);
244 if (switchObject != null) {
245 setSwitchStateImpl(switchObject, SwitchState.INACTIVE);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800246
Jonathan Hartadc63892013-11-08 14:03:55 -0800247 for (IPortObject portObject : switchObject.getPorts()) {
248 portObject.setState("INACTIVE");
249 }
250 op.commit();
251 success = true;
252 }
253 else {
254 log.warn("Switch {} not found when trying to deactivate", dpid);
255 }
256 } catch (Exception e) {
257 // TODO what type of exception is thrown when we can't commit?
258 op.rollback();
259 log.error("SwitchStorage:deactivateSwitch {} failed", dpid, e);
260 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800261
Jonathan Hartadc63892013-11-08 14:03:55 -0800262 return success;
263 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700264
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800265 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700266 public boolean updatePort(String dpid, short portNum, int state, String desc) {
267 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800268
Naoki Shiota987a5722013-10-23 11:59:36 -0700269 try {
270 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800271
Naoki Shiota987a5722013-10-23 11:59:36 -0700272 if (sw != null) {
273 IPortObject p = sw.getPort(portNum);
274 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
275 if (p != null) {
276 setPortStateImpl(p, state, desc);
Pavlin Radoslavove86bbfc2013-11-05 08:32:14 -0800277 op.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700278 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700279 success = true;
280 } else {
281 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
282 }
283 } catch (Exception e) {
284 op.rollback();
285 e.printStackTrace();
286 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800287 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700288
289 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700290 }
291
292 /***
293 * This function is for adding the switch port into the DB.
294 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700295 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700296 */
297 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700298 public boolean addPort(String dpid, OFPhysicalPort phport) {
299 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800300
Naoki Shiota987a5722013-10-23 11:59:36 -0700301 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
302 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
303 // just dispatch to deletePort()
Jonathan Hartadc63892013-11-08 14:03:55 -0800304 // TODO This is wrong. We need to make sure the port is in the
305 // DB with the correct info and port state.
Naoki Shiota987a5722013-10-23 11:59:36 -0700306 return deletePort(dpid, phport.getPortNumber());
307 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800308
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700309 try {
310 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800311
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700312 if (sw != null) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800313 IPortObject portObject = addPortImpl(sw, phport);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800314
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800315 // XXX for now delete devices when we change a port to prevent
316 // having stale devices.
317 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800318 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800319
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800320 for (IDeviceObject deviceObject : portObject.getDevices()) {
321 deviceStorage.removeDevice(deviceObject);
322 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800323
Naoki Shiota987a5722013-10-23 11:59:36 -0700324 op.commit();
325 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700326 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700328 }
329 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700330 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700331 e.printStackTrace();
332 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800333 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800334
Naoki Shiota987a5722013-10-23 11:59:36 -0700335 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700336 }
337
338 /***
339 * This function is for deleting the switch port from the DB.
340 * @param dpid The switch dpid that has the port.
341 * @param port The port you want to delete the switch.
342 */
343 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700344 public boolean deletePort(String dpid, short port) {
345 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800346
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800347 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800348 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800349
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700350 try {
351 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800352
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700353 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700354 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700355 if (p != null) {
356 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Jonathan Hartadc63892013-11-08 14:03:55 -0800357 p.setState("INACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800358
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800359 // XXX for now delete devices when we change a port to prevent
360 // having stale devices.
361 for (IDeviceObject d : p.getDevices()) {
362 deviceStorage.removeDevice(d);
363 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700364 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700365 }
366 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800367
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800368 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700369 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700370 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700371 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800372 log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700373 }
374
Naoki Shiota987a5722013-10-23 11:59:36 -0700375 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700376 }
377
Pavlin Radoslavov64647d22013-11-04 19:07:03 -0800378 /**
379 * Get list of all ports on the switch specified by given DPID.
380 *
381 * @param dpid DPID of desired switch.
382 * @return List of port IDs. Empty list if no port was found.
383 */
384 @Override
385 public List<Short> getPorts(String dpid) {
386 List<Short> ports = new ArrayList<Short>();
387
388 ISwitchObject srcSw = op.searchSwitch(dpid);
389 if (srcSw != null) {
390 for (IPortObject srcPort : srcSw.getPorts()) {
391 ports.add(srcPort.getNumber());
392 }
393 }
394
395 return ports;
396 }
397
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700398 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700399 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700400 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700401 sw.setState(SwitchState.ACTIVE.toString());
402 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
403 return sw;
404 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700405 return null;
406 }
407 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800408
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700409 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
410 if (sw != null && state != null) {
411 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700412 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
413 sw.getDPID(), state.toString());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700414 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800415 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800416
Naoki Shiota987a5722013-10-23 11:59:36 -0700417 private void deleteSwitchImpl(ISwitchObject sw) {
418 if (sw != null) {
419 op.removeSwitch(sw);
420 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
421 sw.getDPID());
yoshi2fd4c7e2013-11-22 15:47:55 -0800422 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700423 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800424
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800425
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800426 private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) {
427 IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800428
429 log.info("SwitchStorage:addPort dpid:{} port:{}",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800430 sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800431
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800432 if (portObject != null) {
433 setPortStateImpl(portObject, phport.getState(), phport.getName());
434 portObject.setState("ACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800435
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800436 // This a convoluted way of checking if the port is attached
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800437 // or not, but doing it this way avoids using the
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800438 // ISwitchObject.getPort method which uses GremlinGroovy query
439 // and takes forever.
440 boolean attached = false;
441 for (IPortObject portsOnSwitch : sw.getPorts()) {
Yuta HIGUCHIaa1fac72013-12-15 14:47:56 -0800442 if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800443 attached = true;
444 break;
445 }
446 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800447
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800448 if (!attached) {
449 sw.addPort(portObject);
450 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800451
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800452 /*
453 if (sw.getPort(phport.getPortNumber()) == null) {
454 // The port exists but the switch has no "on" link to it
455 sw.addPort(portObject);
456 }*/
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800457
458 log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800459 sw.getDPID(), phport.getPortNumber());
460 } else {
461 //addPortImpl(sw, phport.getPortNumber());
462 portObject = op.newPort(sw.getDPID(), phport.getPortNumber());
463 portObject.setState("ACTIVE");
464 setPortStateImpl(portObject, phport.getState(), phport.getName());
465 sw.addPort(portObject);
466 log.info("SwitchStorage:addPort dpid:{} port:{} done",
467 sw.getDPID(), phport.getPortNumber());
468 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800469
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800470 return portObject;
471 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700472 // TODO There's an issue here where a port with that ID could already
473 // exist when we try to add this one (because it's left over from an
474 // old topology). We need to remove an old port with the same ID when
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800475 // we add the new port. Also it seems that old ports like this are
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700476 // never cleaned up and will remain in the DB in the ACTIVE state forever.
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800477 /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700478 IPortObject p = op.newPort(sw.getDPID(), portNum);
479 p.setState("ACTIVE");
480 sw.addPort(p);
481 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
482 sw.getDPID(), portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800483
Naoki Shiota987a5722013-10-23 11:59:36 -0700484 return p;
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800485 }*/
yoshi2fd4c7e2013-11-22 15:47:55 -0800486
Naoki Shiota987a5722013-10-23 11:59:36 -0700487 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
488 if (port != null) {
489 if (state != null) {
490 port.setPortState(state);
491 }
492 if (desc != null) {
493 port.setDesc(desc);
494 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800495
Naoki Shiota987a5722013-10-23 11:59:36 -0700496 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
497 new Object[] {port.getPortId(), state, desc});
498 }
499 }
Yuta HIGUCHIef3828d2013-12-17 22:17:07 -0800500}