HIGUCHI Yuta | ed49ef7 | 2013-06-12 11:34:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core.internal; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 64647d2 | 2013-11-04 19:07:03 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 6 | import net.floodlightcontroller.core.IOFSwitch; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 7 | import net.onrc.onos.graph.DBOperation; |
| 8 | import net.onrc.onos.graph.GraphDBManager; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 13 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 14 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 15 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 16 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 17 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 20 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 21 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 22 | * This is the class for storing the information of switches into GraphDB |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 23 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 24 | public class SwitchStorageImpl implements ISwitchStorage { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 25 | |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 26 | protected DBOperation op; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 27 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 28 | public final long measureONOSTimeProp = Long.valueOf(System.getProperty("benchmark.measureONOS", "0")); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 29 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 30 | /*** |
| 31 | * Initialize function. Before you use this class, please call this method |
| 32 | * @param conf configuration file for Cassandra DB |
| 33 | */ |
| 34 | @Override |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 35 | public void init(final String dbStore, final String conf) { |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 36 | op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
| 37 | //op = GraphDBManager.getDBOperation(dbStore, conf); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 38 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 39 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 40 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 41 | /*** |
| 42 | * Finalize/close function. After you use this class, please call this method. |
| 43 | * It will close the DB connection. |
| 44 | */ |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 45 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 46 | public void finalize() { |
| 47 | close(); |
| 48 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 49 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 50 | /*** |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 56 | op.close(); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 57 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 58 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 59 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 64 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 65 | /*** |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 66 | * 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 Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 71 | /* |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 75 | * We especially don't need this update method to re-implement |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 76 | * the functions of other methods. |
| 77 | */ |
| 78 | @Deprecated |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 79 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 80 | public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 81 | boolean success = false; |
| 82 | ISwitchObject sw = null; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 83 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 84 | log.info("SwitchStorage:update {} dpid:{}", dmope, dpid); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 85 | switch(dmope) { |
| 86 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 87 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 100 | case INSERT: |
| 101 | case CREATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 102 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 116 | break; |
| 117 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 118 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 130 | break; |
| 131 | default: |
| 132 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 133 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 134 | return success; |
| 135 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 136 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public boolean addSwitch(IOFSwitch sw) { |
| 139 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 140 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 141 | String dpid = sw.getStringId(); |
| 142 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 143 | 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 148 | try { |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 149 | if (measureONOSTimeProp == 1) { |
| 150 | totalStartTime = System.nanoTime(); |
| 151 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 152 | ISwitchObject curr = op.searchSwitch(dpid); |
| 153 | if (curr != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 154 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 155 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 156 | if (measureONOSTimeProp == 1) { |
| 157 | startUpdSwitchTime = System.nanoTime(); |
| 158 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 159 | setSwitchStateImpl(curr, SwitchState.ACTIVE); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 160 | if (measureONOSTimeProp == 1) { |
| 161 | endUpdSwitchTime = System.nanoTime(); |
| 162 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 163 | } else { |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 164 | if (measureONOSTimeProp == 1) { |
| 165 | startSwitchTime = System.nanoTime(); |
| 166 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 167 | curr = addSwitchImpl(dpid); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 168 | if (measureONOSTimeProp == 1) { |
| 169 | endSwitchTime = System.nanoTime(); |
| 170 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 171 | } |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 172 | if (measureONOSTimeProp == 1) { |
| 173 | startPortTime = System.nanoTime(); |
| 174 | } |
| 175 | long noOfPorts = 0; |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 176 | for (OFPhysicalPort port: sw.getPorts()) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 177 | //addPort(dpid, port); |
| 178 | addPortImpl(curr, port); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 179 | noOfPorts++; |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 180 | } |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 181 | if (measureONOSTimeProp == 1) { |
| 182 | endPortTime = System.nanoTime(); |
| 183 | } |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 184 | // XXX for now delete devices when we change a port to prevent |
| 185 | // having stale devices. |
| 186 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 187 | deviceStorage.init("",""); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 188 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 195 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 196 | op.commit(); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 197 | 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 210 | success = true; |
| 211 | } catch (Exception e) { |
| 212 | op.rollback(); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 213 | log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 214 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 215 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 216 | return success; |
| 217 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 218 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 219 | /*** |
| 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 Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 223 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 226 | @Deprecated |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public boolean addSwitch(String dpid) { |
| 229 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 230 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 231 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 232 | try { |
| 233 | ISwitchObject sw = op.searchSwitch(dpid); |
| 234 | if (sw != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 235 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 236 | 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 Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 246 | log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 247 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 248 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 249 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 259 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 260 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 272 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 273 | return success; |
| 274 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 275 | |
| 276 | @Override |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 277 | public boolean deactivateSwitch(String dpid) { |
| 278 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 279 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 280 | try { |
| 281 | ISwitchObject switchObject = op.searchSwitch(dpid); |
| 282 | if (switchObject != null) { |
| 283 | setSwitchStateImpl(switchObject, SwitchState.INACTIVE); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 284 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 285 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 299 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 300 | return success; |
| 301 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 302 | |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 303 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 304 | public boolean updatePort(String dpid, short portNum, int state, String desc) { |
| 305 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 306 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 307 | try { |
| 308 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 309 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 310 | 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 Radoslavov | e86bbfc | 2013-11-05 08:32:14 -0800 | [diff] [blame] | 315 | op.commit(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 316 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 317 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 325 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 326 | |
| 327 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 328 | } |
| 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 333 | * @param phport The port you want to add the switch. |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 334 | */ |
| 335 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 336 | public boolean addPort(String dpid, OFPhysicalPort phport) { |
| 337 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 338 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 339 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) || |
| 340 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) { |
| 341 | // just dispatch to deletePort() |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 342 | // TODO This is wrong. We need to make sure the port is in the |
| 343 | // DB with the correct info and port state. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 344 | return deletePort(dpid, phport.getPortNumber()); |
| 345 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 346 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 347 | try { |
| 348 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 349 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 350 | if (sw != null) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 351 | IPortObject portObject = addPortImpl(sw, phport); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 352 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 353 | // XXX for now delete devices when we change a port to prevent |
| 354 | // having stale devices. |
| 355 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 356 | deviceStorage.init("",""); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 357 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 358 | for (IDeviceObject deviceObject : portObject.getDevices()) { |
| 359 | deviceStorage.removeDevice(deviceObject); |
| 360 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 361 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 362 | op.commit(); |
| 363 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 364 | } else { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 365 | log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 366 | } |
| 367 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 368 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 369 | e.printStackTrace(); |
| 370 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber()); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 371 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 372 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 373 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 374 | } |
| 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 382 | public boolean deletePort(String dpid, short port) { |
| 383 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 384 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 385 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 386 | deviceStorage.init("",""); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 387 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 388 | try { |
| 389 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 390 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 391 | if (sw != null) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 392 | IPortObject p = sw.getPort(port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 393 | if (p != null) { |
| 394 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 395 | p.setState("INACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 396 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 397 | // 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 402 | op.commit(); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 405 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 406 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 407 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 408 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 409 | e.printStackTrace(); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 410 | log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 413 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Pavlin Radoslavov | 64647d2 | 2013-11-04 19:07:03 -0800 | [diff] [blame] | 416 | /** |
| 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 436 | private ISwitchObject addSwitchImpl(String dpid) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 437 | if (dpid != null) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 438 | ISwitchObject sw = op.newSwitch(dpid); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 439 | sw.setState(SwitchState.ACTIVE.toString()); |
| 440 | log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid); |
| 441 | return sw; |
| 442 | } else { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 443 | return null; |
| 444 | } |
| 445 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 446 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 447 | private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) { |
| 448 | if (sw != null && state != null) { |
| 449 | sw.setState(state.toString()); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 450 | log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}", |
| 451 | sw.getDPID(), state.toString()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 452 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 453 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 454 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 455 | private void deleteSwitchImpl(ISwitchObject sw) { |
| 456 | if (sw != null) { |
| 457 | op.removeSwitch(sw); |
| 458 | log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done", |
| 459 | sw.getDPID()); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 460 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 461 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 462 | |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 463 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 464 | private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) { |
| 465 | IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 466 | |
| 467 | log.info("SwitchStorage:addPort dpid:{} port:{}", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 468 | sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 469 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 470 | if (portObject != null) { |
| 471 | setPortStateImpl(portObject, phport.getState(), phport.getName()); |
| 472 | portObject.setState("ACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 473 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 474 | // This a convoluted way of checking if the port is attached |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 475 | // or not, but doing it this way avoids using the |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 476 | // ISwitchObject.getPort method which uses GremlinGroovy query |
| 477 | // and takes forever. |
| 478 | boolean attached = false; |
| 479 | for (IPortObject portsOnSwitch : sw.getPorts()) { |
Yuta HIGUCHI | aa1fac7 | 2013-12-15 14:47:56 -0800 | [diff] [blame] | 480 | if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 481 | attached = true; |
| 482 | break; |
| 483 | } |
| 484 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 485 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 486 | if (!attached) { |
| 487 | sw.addPort(portObject); |
| 488 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 489 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 490 | /* |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 495 | |
| 496 | log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 497 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 507 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 508 | return portObject; |
| 509 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 510 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 513 | // we add the new port. Also it seems that old ports like this are |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 514 | // never cleaned up and will remain in the DB in the ACTIVE state forever. |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 515 | /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 516 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 521 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 522 | return p; |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 523 | }*/ |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 524 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 525 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 533 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 534 | log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done", |
| 535 | new Object[] {port.getPortId(), state, desc}); |
| 536 | } |
| 537 | } |
Yuta HIGUCHI | ef3828d | 2013-12-17 22:17:07 -0800 | [diff] [blame] | 538 | } |