blob: 7c4d9ce3cbcd67a1c0920b0b94ba9830effaac54 [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
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -08006import com.tinkerpop.blueprints.impls.ramcloud.PerfMon;
7
Pankaj Berdebbd38612013-06-22 05:59:12 -07008import net.floodlightcontroller.core.IOFSwitch;
yoshi2fd4c7e2013-11-22 15:47:55 -08009import net.onrc.onos.graph.DBOperation;
10import net.onrc.onos.graph.GraphDBManager;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070011import net.onrc.onos.ofcontroller.core.ISwitchStorage;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080012import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070013import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080015import net.onrc.onos.ofcontroller.core.ISwitchStorage;
Pankaj Berdeafb20532013-01-08 15:05:24 -080016
Pankaj Berde3200ea02013-01-04 15:48:36 -080017import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080018import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
19import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080022
Teruef33dc32013-06-20 09:54:37 -070023/**
Naoki Shiota987a5722013-10-23 11:59:36 -070024 * This is the class for storing the information of switches into GraphDB
Teruef33dc32013-06-20 09:54:37 -070025 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080026public class SwitchStorageImpl implements ISwitchStorage {
Pankaj Berde3200ea02013-01-04 15:48:36 -080027
yoshi0fee3de2013-11-23 09:13:37 -080028 protected DBOperation op;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070029 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
yoshid76fa1f2013-12-19 14:20:34 -080030 public final long measureONOSTimeProp = Long.valueOf(System.getProperty("benchmark.measureONOS", "0"));
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -080031 public final long measureAllTimeProp = Long.valueOf(System.getProperty("benchmark.measureAll", "0"));
32
33 private static PerfMon pm = PerfMon.getInstance();
Teruef33dc32013-06-20 09:54:37 -070034
Pankaj Berde3200ea02013-01-04 15:48:36 -080035 /***
36 * Initialize function. Before you use this class, please call this method
37 * @param conf configuration file for Cassandra DB
38 */
39 @Override
yoshi0fee3de2013-11-23 09:13:37 -080040 public void init(final String dbStore, final String conf) {
yoshib6a66eb2014-01-21 10:11:49 -080041 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
yoshid38cd312013-12-02 19:54:44 -080042 //op = GraphDBManager.getDBOperation(dbStore, conf);
Pankaj Berde3200ea02013-01-04 15:48:36 -080043 }
yoshi2fd4c7e2013-11-22 15:47:55 -080044
yoshi2fd4c7e2013-11-22 15:47:55 -080045
Teruef33dc32013-06-20 09:54:37 -070046 /***
47 * Finalize/close function. After you use this class, please call this method.
48 * It will close the DB connection.
49 */
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080050 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080051 protected void finalize() {
Teruef33dc32013-06-20 09:54:37 -070052 close();
53 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080054
Teruef33dc32013-06-20 09:54:37 -070055 /***
56 * Finalize/close function. After you use this class, please call this method.
57 * It will close the DB connection. This is for Java garbage collection.
58 */
59 @Override
60 public void close() {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080061 op.close();
Teruef33dc32013-06-20 09:54:37 -070062 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080063
Naoki Shiota987a5722013-10-23 11:59:36 -070064 // Method designing policy:
65 // op.commit() and op.rollback() MUST called in public (first-class) methods.
66 // A first-class method MUST NOT call other first-class method.
67 // Routine process should be implemented in private method.
68 // A private method MUST NOT call commit or rollback.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080069
Teruef33dc32013-06-20 09:54:37 -070070 /***
Teruef33dc32013-06-20 09:54:37 -070071 * This function is for updating the switch into the DB.
72 * @param dpid The switch dpid you want to update from the DB
73 * @param state The state of the switch like ACTIVE, INACTIVE
74 * @param dmope The DM_OPERATION of the switch
75 */
Jonathan Hartadc63892013-11-08 14:03:55 -080076 /*
77 * Jono, 11/8/2013
78 * We don't need this update method that demultiplexes DM_OPERATIONS,
79 * we can have clients just call the required methods directly.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080080 * We especially don't need this update method to re-implement
Jonathan Hartadc63892013-11-08 14:03:55 -080081 * the functions of other methods.
82 */
83 @Deprecated
Teruef33dc32013-06-20 09:54:37 -070084 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070085 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
86 boolean success = false;
87 ISwitchObject sw = null;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -080088
Naoki Shiota987a5722013-10-23 11:59:36 -070089 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070090 switch(dmope) {
91 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070092 try {
93 sw = op.searchSwitch(dpid);
94 if (sw != null) {
95 setSwitchStateImpl(sw, state);
96 op.commit();
97 success = true;
98 }
99 } catch (Exception e) {
100 op.rollback();
101 e.printStackTrace();
102 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
103 }
104 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700105 case INSERT:
106 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700107 try {
108 sw = addSwitchImpl(dpid);
109 if (sw != null) {
110 if (state != SwitchState.ACTIVE) {
111 setSwitchStateImpl(sw, state);
112 }
113 op.commit();
114 success = true;
115 }
116 } catch (Exception e) {
117 op.rollback();
118 e.printStackTrace();
119 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
120 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700121 break;
122 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700123 try {
124 sw = op.searchSwitch(dpid);
125 if (sw != null) {
126 deleteSwitchImpl(sw);
127 op.commit();
128 success = true;
129 }
130 } catch (Exception e) {
131 op.rollback();
132 e.printStackTrace();
133 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
134 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700135 break;
136 default:
137 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800138
Naoki Shiota987a5722013-10-23 11:59:36 -0700139 return success;
140 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800141
Naoki Shiota987a5722013-10-23 11:59:36 -0700142 @Override
143 public boolean addSwitch(IOFSwitch sw) {
144 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800145
Naoki Shiota987a5722013-10-23 11:59:36 -0700146 String dpid = sw.getStringId();
147 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800148 long startSwitchTime = 0, endSwitchTime = 0;
149 long startUpdSwitchTime = 0, endUpdSwitchTime=0;
150 long startPortTime = 0, endPortTime=0;
151 long totalStartTime =0, totalEndTime=0;
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800152 long Tstamp1=0;
yoshid76fa1f2013-12-19 14:20:34 -0800153
Naoki Shiota987a5722013-10-23 11:59:36 -0700154 try {
yoshid76fa1f2013-12-19 14:20:34 -0800155 if (measureONOSTimeProp == 1) {
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800156 log.error("Performance: addSwitch dpid= {} Start", dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800157 totalStartTime = System.nanoTime();
158 }
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800159 pm.addswitch_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700160 ISwitchObject curr = op.searchSwitch(dpid);
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800161 if (measureONOSTimeProp == 1) {
162 Tstamp1 = System.nanoTime();
163 log.error("Performance: addSwitch dpid= {} searchSwitch done at {} took {}", dpid, Tstamp1, Tstamp1-totalStartTime);
164 }
165
Naoki Shiota987a5722013-10-23 11:59:36 -0700166 if (curr != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800167 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700168 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
yoshid76fa1f2013-12-19 14:20:34 -0800169 if (measureONOSTimeProp == 1) {
170 startUpdSwitchTime = System.nanoTime();
171 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700172 setSwitchStateImpl(curr, SwitchState.ACTIVE);
yoshid76fa1f2013-12-19 14:20:34 -0800173 if (measureONOSTimeProp == 1) {
174 endUpdSwitchTime = System.nanoTime();
175 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 } else {
yoshid76fa1f2013-12-19 14:20:34 -0800177 if (measureONOSTimeProp == 1) {
178 startSwitchTime = System.nanoTime();
179 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700180 curr = addSwitchImpl(dpid);
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800181 pm.addswitch_end();
yoshid76fa1f2013-12-19 14:20:34 -0800182 if (measureONOSTimeProp == 1) {
183 endSwitchTime = System.nanoTime();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800184 //log.error("Performance: addSwitch dpid= {} addSwitchImpl done at {} took {}", dpid, endSwitchTime, endSwitchTime-startSwitchTime);
185 log.error("Performance: addSwitch dpid= {} End searchSwitch {} addSwitchImpl {} total {} diff {}", dpid, Tstamp1-totalStartTime, endSwitchTime-startSwitchTime, endSwitchTime-totalStartTime,endSwitchTime-totalStartTime-(Tstamp1-totalStartTime)-(endSwitchTime-startSwitchTime));
yoshid76fa1f2013-12-19 14:20:34 -0800186 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700187 }
yoshid76fa1f2013-12-19 14:20:34 -0800188 if (measureONOSTimeProp == 1) {
189 startPortTime = System.nanoTime();
190 }
191 long noOfPorts = 0;
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800192 pm.addport_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700193 for (OFPhysicalPort port: sw.getPorts()) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800194 //addPort(dpid, port);
195 addPortImpl(curr, port);
yoshid76fa1f2013-12-19 14:20:34 -0800196 noOfPorts++;
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800197 pm.addport_incr();
Naoki Shiota987a5722013-10-23 11:59:36 -0700198 }
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800199 pm.addport_end();
yoshid76fa1f2013-12-19 14:20:34 -0800200 if (measureONOSTimeProp == 1) {
201 endPortTime = System.nanoTime();
202 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800203 // XXX for now delete devices when we change a port to prevent
204 // having stale devices.
205 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800206 deviceStorage.init("","");
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800207 for (IPortObject portObject : curr.getPorts()) {
208 for (IDeviceObject deviceObject : portObject.getDevices()) {
209 // The deviceStorage has to remove on the object gained by its own
210 // FramedGraph, it can't use our objects from here
211 deviceStorage.removeDeviceImpl(deviceStorage.getDeviceByMac(deviceObject.getMACAddress()));
212 }
213 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800214
Naoki Shiota987a5722013-10-23 11:59:36 -0700215 op.commit();
yoshid76fa1f2013-12-19 14:20:34 -0800216 if (measureONOSTimeProp == 1) {
217 totalEndTime = System.nanoTime();
218 }
219 if (startSwitchTime != 0) {
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800220 //log.error("Performance -- switch add total time {}", endSwitchTime - startSwitchTime);
221 log.error("Performance -- switch add total time {} including_search {}", endSwitchTime - startSwitchTime, endSwitchTime - totalStartTime);
yoshid76fa1f2013-12-19 14:20:34 -0800222 }
223 if (startUpdSwitchTime != 0) {
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800224 log.error("Performance -- switch update total time {} including_search {}", endUpdSwitchTime - startUpdSwitchTime, endUpdSwitchTime - totalStartTime);
yoshid76fa1f2013-12-19 14:20:34 -0800225 }
226 if (startPortTime != 0) {
227 log.error("Performance @@ port add total time {} no of ports written {}", endPortTime - startPortTime, noOfPorts);
228 }
yoshi97159552014-01-10 13:18:20 -0800229 if (totalStartTime != 0) {
230 log.error("Performance && total time for add switch {}", totalEndTime - totalStartTime);
231 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700232 success = true;
233 } catch (Exception e) {
234 op.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800235 log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700236 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800237
Naoki Shiota987a5722013-10-23 11:59:36 -0700238 return success;
239 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800240
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 /***
242 * This function is for adding the switch into the DB.
243 * @param dpid The switch dpid you want to add into the DB.
244 */
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700245 // This method is only called by tests, so we probably don't need it.
246 // If we need both addSwitch interfaces, one should call the other
247 // rather than implementing the same logic twice.
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800248 @Deprecated
Naoki Shiota987a5722013-10-23 11:59:36 -0700249 @Override
250 public boolean addSwitch(String dpid) {
251 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800252
Naoki Shiota987a5722013-10-23 11:59:36 -0700253 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
254 try {
255 ISwitchObject sw = op.searchSwitch(dpid);
256 if (sw != null) {
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800257 //If existing the switch. set The SW state ACTIVE.
Naoki Shiota987a5722013-10-23 11:59:36 -0700258 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
259 setSwitchStateImpl(sw, SwitchState.ACTIVE);
260 } else {
261 addSwitchImpl(dpid);
262 }
263 op.commit();
264 success = true;
265 } catch (Exception e) {
266 op.rollback();
267 e.printStackTrace();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800268 log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e);
Naoki Shiota987a5722013-10-23 11:59:36 -0700269 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800270
Naoki Shiota987a5722013-10-23 11:59:36 -0700271 return success;
272 }
273
274 /***
275 * This function is for deleting the switch into the DB.
276 * @param dpid The switch dpid you want to delete from the DB.
277 */
278 @Override
279 public boolean deleteSwitch(String dpid) {
280 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800281
Naoki Shiota987a5722013-10-23 11:59:36 -0700282 try {
283 ISwitchObject sw = op.searchSwitch(dpid);
284 if (sw != null) {
285 deleteSwitchImpl(sw);
286 op.commit();
287 }
288 success = true;
289 } catch (Exception e) {
290 op.rollback();
291 e.printStackTrace();
292 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
293 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800294
Naoki Shiota987a5722013-10-23 11:59:36 -0700295 return success;
296 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800297
298 @Override
Jonathan Hartadc63892013-11-08 14:03:55 -0800299 public boolean deactivateSwitch(String dpid) {
300 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800301
Jonathan Hartadc63892013-11-08 14:03:55 -0800302 try {
303 ISwitchObject switchObject = op.searchSwitch(dpid);
304 if (switchObject != null) {
305 setSwitchStateImpl(switchObject, SwitchState.INACTIVE);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800306
Jonathan Hartadc63892013-11-08 14:03:55 -0800307 for (IPortObject portObject : switchObject.getPorts()) {
308 portObject.setState("INACTIVE");
309 }
310 op.commit();
311 success = true;
312 }
313 else {
314 log.warn("Switch {} not found when trying to deactivate", dpid);
315 }
316 } catch (Exception e) {
317 // TODO what type of exception is thrown when we can't commit?
318 op.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800319 log.error("SwitchStorage:deactivateSwitch "+dpid+" failed", e);
Jonathan Hartadc63892013-11-08 14:03:55 -0800320 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800321
Jonathan Hartadc63892013-11-08 14:03:55 -0800322 return success;
323 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700324
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800325 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700326 public boolean updatePort(String dpid, short portNum, int state, String desc) {
327 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800328
Naoki Shiota987a5722013-10-23 11:59:36 -0700329 try {
330 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800331
Naoki Shiota987a5722013-10-23 11:59:36 -0700332 if (sw != null) {
333 IPortObject p = sw.getPort(portNum);
334 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
335 if (p != null) {
336 setPortStateImpl(p, state, desc);
Pavlin Radoslavove86bbfc2013-11-05 08:32:14 -0800337 op.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700338 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700339 success = true;
340 } else {
341 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
342 }
343 } catch (Exception e) {
344 op.rollback();
345 e.printStackTrace();
346 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800347 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700348
349 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700350 }
351
352 /***
353 * This function is for adding the switch port into the DB.
354 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700355 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700356 */
357 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700358 public boolean addPort(String dpid, OFPhysicalPort phport) {
359 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800360
Naoki Shiota987a5722013-10-23 11:59:36 -0700361 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
362 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
363 // just dispatch to deletePort()
Jonathan Hartadc63892013-11-08 14:03:55 -0800364 // TODO This is wrong. We need to make sure the port is in the
365 // DB with the correct info and port state.
Naoki Shiota987a5722013-10-23 11:59:36 -0700366 return deletePort(dpid, phport.getPortNumber());
367 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800368
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700369 try {
370 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800371
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700372 if (sw != null) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800373 IPortObject portObject = addPortImpl(sw, phport);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800374
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800375 // XXX for now delete devices when we change a port to prevent
376 // having stale devices.
377 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800378 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800379
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800380 for (IDeviceObject deviceObject : portObject.getDevices()) {
381 deviceStorage.removeDevice(deviceObject);
382 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800383
Naoki Shiota987a5722013-10-23 11:59:36 -0700384 op.commit();
385 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700386 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700387 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700388 }
389 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700390 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700391 e.printStackTrace();
392 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800393 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800394
Naoki Shiota987a5722013-10-23 11:59:36 -0700395 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700396 }
397
398 /***
399 * This function is for deleting the switch port from the DB.
400 * @param dpid The switch dpid that has the port.
401 * @param port The port you want to delete the switch.
402 */
403 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700404 public boolean deletePort(String dpid, short port) {
405 boolean success = false;
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800406
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800407 DeviceStorageImpl deviceStorage = new DeviceStorageImpl();
yoshi0fee3de2013-11-23 09:13:37 -0800408 deviceStorage.init("","");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800409
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700410 try {
411 ISwitchObject sw = op.searchSwitch(dpid);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800412
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700413 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700414 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700415 if (p != null) {
416 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Jonathan Hartadc63892013-11-08 14:03:55 -0800417 p.setState("INACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800418
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800419 // XXX for now delete devices when we change a port to prevent
420 // having stale devices.
421 for (IDeviceObject d : p.getDevices()) {
422 deviceStorage.removeDevice(d);
423 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700424 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700425 }
426 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800427
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800428 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700429 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700430 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700431 e.printStackTrace();
Jonathan Hartadc63892013-11-08 14:03:55 -0800432 log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700433 }
434
Naoki Shiota987a5722013-10-23 11:59:36 -0700435 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700436 }
437
Pavlin Radoslavov64647d22013-11-04 19:07:03 -0800438 /**
439 * Get list of all ports on the switch specified by given DPID.
440 *
441 * @param dpid DPID of desired switch.
442 * @return List of port IDs. Empty list if no port was found.
443 */
444 @Override
445 public List<Short> getPorts(String dpid) {
446 List<Short> ports = new ArrayList<Short>();
447
448 ISwitchObject srcSw = op.searchSwitch(dpid);
449 if (srcSw != null) {
450 for (IPortObject srcPort : srcSw.getPorts()) {
451 ports.add(srcPort.getNumber());
452 }
453 }
454
455 return ports;
456 }
457
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700458 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700459 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700460 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700461 sw.setState(SwitchState.ACTIVE.toString());
462 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
463 return sw;
464 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700465 return null;
466 }
467 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800468
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700469 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
470 if (sw != null && state != null) {
471 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700472 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800473 sw.getDPID(), state);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700474 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800475 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800476
Naoki Shiota987a5722013-10-23 11:59:36 -0700477 private void deleteSwitchImpl(ISwitchObject sw) {
478 if (sw != null) {
479 op.removeSwitch(sw);
480 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
481 sw.getDPID());
yoshi2fd4c7e2013-11-22 15:47:55 -0800482 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700483 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800484
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800485
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800486 private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) {
487 IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800488
489 log.info("SwitchStorage:addPort dpid:{} port:{}",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800490 sw.getDPID(), phport.getPortNumber());
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800491
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800492 if (portObject != null) {
493 setPortStateImpl(portObject, phport.getState(), phport.getName());
494 portObject.setState("ACTIVE");
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800495
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800496 // This a convoluted way of checking if the port is attached
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800497 // or not, but doing it this way avoids using the
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800498 // ISwitchObject.getPort method which uses GremlinGroovy query
499 // and takes forever.
500 boolean attached = false;
501 for (IPortObject portsOnSwitch : sw.getPorts()) {
Yuta HIGUCHIaa1fac72013-12-15 14:47:56 -0800502 if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) {
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800503 attached = true;
504 break;
505 }
506 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800507
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800508 if (!attached) {
509 sw.addPort(portObject);
510 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800511
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800512 /*
513 if (sw.getPort(phport.getPortNumber()) == null) {
514 // The port exists but the switch has no "on" link to it
515 sw.addPort(portObject);
516 }*/
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800517
518 log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE",
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800519 sw.getDPID(), phport.getPortNumber());
520 } else {
521 //addPortImpl(sw, phport.getPortNumber());
522 portObject = op.newPort(sw.getDPID(), phport.getPortNumber());
523 portObject.setState("ACTIVE");
524 setPortStateImpl(portObject, phport.getState(), phport.getName());
525 sw.addPort(portObject);
526 log.info("SwitchStorage:addPort dpid:{} port:{} done",
527 sw.getDPID(), phport.getPortNumber());
528 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800529
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800530 return portObject;
531 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700532 // TODO There's an issue here where a port with that ID could already
533 // exist when we try to add this one (because it's left over from an
534 // old topology). We need to remove an old port with the same ID when
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800535 // we add the new port. Also it seems that old ports like this are
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700536 // never cleaned up and will remain in the DB in the ACTIVE state forever.
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800537 /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700538 IPortObject p = op.newPort(sw.getDPID(), portNum);
539 p.setState("ACTIVE");
540 sw.addPort(p);
541 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
542 sw.getDPID(), portNum);
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800543
Naoki Shiota987a5722013-10-23 11:59:36 -0700544 return p;
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800545 }*/
yoshi2fd4c7e2013-11-22 15:47:55 -0800546
Naoki Shiota987a5722013-10-23 11:59:36 -0700547 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
548 if (port != null) {
549 if (state != null) {
550 port.setPortState(state);
551 }
552 if (desc != null) {
553 port.setDesc(desc);
554 }
Yuta HIGUCHIa0200a92013-12-15 14:50:41 -0800555
Naoki Shiota987a5722013-10-23 11:59:36 -0700556 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
557 new Object[] {port.getPortId(), state, desc});
558 }
559 }
Yuta HIGUCHIef3828d2013-12-17 22:17:07 -0800560}