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; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 7 | import net.onrc.onos.graph.GraphDBConnection; |
| 8 | import net.onrc.onos.graph.GraphDBOperation; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 12 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 13 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 14 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 15 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 16 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 19 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 20 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 21 | * This is the class for storing the information of switches into GraphDB |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 22 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 23 | public class SwitchStorageImpl implements ISwitchStorage { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 24 | protected GraphDBOperation op; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 25 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 26 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 27 | /*** |
| 28 | * Initialize function. Before you use this class, please call this method |
| 29 | * @param conf configuration file for Cassandra DB |
| 30 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 31 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 32 | public void init(String conf) { |
| 33 | GraphDBConnection conn = GraphDBConnection.getInstance(conf); |
| 34 | op = new GraphDBOperation(conn); |
| 35 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 36 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 37 | /*** |
| 38 | * Finalize/close function. After you use this class, please call this method. |
| 39 | * It will close the DB connection. |
| 40 | */ |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 41 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 42 | protected void finalize() { |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 43 | close(); |
| 44 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 45 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 46 | /*** |
| 47 | * Finalize/close function. After you use this class, please call this method. |
Pavlin Radoslavov | ef0cb00 | 2013-06-21 14:55:23 -0700 | [diff] [blame] | 48 | * It will close the DB connection. This is for Java garbage collection. |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 49 | */ |
| 50 | @Override |
| 51 | public void close() { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 52 | op.close(); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 53 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 54 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 55 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 60 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 61 | /*** |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 62 | * 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 Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 67 | /* |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 71 | * We especially don't need this update method to re-implement |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 72 | * the functions of other methods. |
| 73 | */ |
| 74 | @Deprecated |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 75 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 76 | public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 77 | boolean success = false; |
| 78 | ISwitchObject sw = null; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 79 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 80 | log.info("SwitchStorage:update {} dpid:{}", dmope, dpid); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 81 | switch(dmope) { |
| 82 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 83 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 96 | case INSERT: |
| 97 | case CREATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 98 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 112 | break; |
| 113 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 114 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 126 | break; |
| 127 | default: |
| 128 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 129 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 130 | return success; |
| 131 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 132 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public boolean addSwitch(IOFSwitch sw) { |
| 135 | boolean success = false; |
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 | String dpid = sw.getStringId(); |
| 138 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 139 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 140 | try { |
| 141 | ISwitchObject curr = op.searchSwitch(dpid); |
| 142 | if (curr != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 143 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 144 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 145 | setSwitchStateImpl(curr, SwitchState.ACTIVE); |
| 146 | } else { |
| 147 | curr = addSwitchImpl(dpid); |
| 148 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 149 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 150 | for (OFPhysicalPort port: sw.getPorts()) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 151 | //addPort(dpid, port); |
| 152 | addPortImpl(curr, port); |
| 153 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 154 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 155 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 156 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 167 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 168 | op.commit(); |
| 169 | success = true; |
| 170 | } catch (Exception e) { |
| 171 | op.rollback(); |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 172 | log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 173 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 174 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 175 | 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 Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 182 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 185 | @Deprecated |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public boolean addSwitch(String dpid) { |
| 188 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 189 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 190 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 191 | try { |
| 192 | ISwitchObject sw = op.searchSwitch(dpid); |
| 193 | if (sw != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 194 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 195 | 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 HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 205 | log.error("SwitchStorage:addSwitch dpid:"+dpid+" failed", e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 206 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 207 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 208 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 218 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 219 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 231 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 232 | return success; |
| 233 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 234 | |
| 235 | @Override |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 236 | public boolean deactivateSwitch(String dpid) { |
| 237 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 239 | try { |
| 240 | ISwitchObject switchObject = op.searchSwitch(dpid); |
| 241 | if (switchObject != null) { |
| 242 | setSwitchStateImpl(switchObject, SwitchState.INACTIVE); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 243 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 244 | 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 HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 256 | log.error("SwitchStorage:deactivateSwitch "+dpid+" failed", e); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 257 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 258 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 259 | return success; |
| 260 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 261 | |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 262 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 263 | public boolean updatePort(String dpid, short portNum, int state, String desc) { |
| 264 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 265 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 266 | try { |
| 267 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 268 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 269 | 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 Radoslavov | e86bbfc | 2013-11-05 08:32:14 -0800 | [diff] [blame] | 274 | op.commit(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 275 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 276 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 284 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 285 | |
| 286 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 287 | } |
| 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 292 | * @param phport The port you want to add the switch. |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 293 | */ |
| 294 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 295 | public boolean addPort(String dpid, OFPhysicalPort phport) { |
| 296 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 297 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 298 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) || |
| 299 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) { |
| 300 | // just dispatch to deletePort() |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 301 | // TODO This is wrong. We need to make sure the port is in the |
| 302 | // DB with the correct info and port state. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 303 | return deletePort(dpid, phport.getPortNumber()); |
| 304 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 305 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 306 | try { |
| 307 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 308 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 309 | if (sw != null) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 310 | IPortObject portObject = addPortImpl(sw, phport); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 311 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 312 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 316 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 317 | for (IDeviceObject deviceObject : portObject.getDevices()) { |
| 318 | deviceStorage.removeDevice(deviceObject); |
| 319 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 320 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 321 | op.commit(); |
| 322 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 323 | } else { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 324 | 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] | 325 | } |
| 326 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 327 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 328 | e.printStackTrace(); |
| 329 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber()); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 330 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 331 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 332 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 333 | } |
| 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 341 | public boolean deletePort(String dpid, short port) { |
| 342 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 343 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 344 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
| 345 | deviceStorage.init(""); |
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) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 351 | IPortObject p = sw.getPort(port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 352 | if (p != null) { |
| 353 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 354 | p.setState("INACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 355 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 356 | // 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 361 | op.commit(); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 364 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 365 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 366 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 367 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 368 | e.printStackTrace(); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 369 | log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 372 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Pavlin Radoslavov | 64647d2 | 2013-11-04 19:07:03 -0800 | [diff] [blame] | 375 | /** |
| 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 395 | private ISwitchObject addSwitchImpl(String dpid) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 396 | if (dpid != null) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 397 | ISwitchObject sw = op.newSwitch(dpid); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 398 | sw.setState(SwitchState.ACTIVE.toString()); |
| 399 | log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid); |
| 400 | return sw; |
| 401 | } else { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 402 | return null; |
| 403 | } |
| 404 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 405 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 406 | private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) { |
| 407 | if (sw != null && state != null) { |
| 408 | sw.setState(state.toString()); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 409 | log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}", |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 410 | sw.getDPID(), state); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 411 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 412 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 413 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 414 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 422 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 423 | private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) { |
| 424 | IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 425 | |
| 426 | log.info("SwitchStorage:addPort dpid:{} port:{}", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 427 | sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 428 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 429 | if (portObject != null) { |
| 430 | setPortStateImpl(portObject, phport.getState(), phport.getName()); |
| 431 | portObject.setState("ACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 432 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 433 | // This a convoluted way of checking if the port is attached |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 434 | // or not, but doing it this way avoids using the |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 435 | // ISwitchObject.getPort method which uses GremlinGroovy query |
| 436 | // and takes forever. |
| 437 | boolean attached = false; |
| 438 | for (IPortObject portsOnSwitch : sw.getPorts()) { |
Yuta HIGUCHI | aa1fac7 | 2013-12-15 14:47:56 -0800 | [diff] [blame] | 439 | if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 440 | attached = true; |
| 441 | break; |
| 442 | } |
| 443 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 444 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 445 | if (!attached) { |
| 446 | sw.addPort(portObject); |
| 447 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 448 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 449 | /* |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 454 | |
| 455 | log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 456 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 466 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 467 | return portObject; |
| 468 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 469 | // 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 472 | // 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] | 473 | // 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] | 474 | /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 475 | 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 480 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 481 | return p; |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 482 | }*/ |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 483 | |
| 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 HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 492 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 493 | log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done", |
| 494 | new Object[] {port.getPortId(), state, desc}); |
| 495 | } |
| 496 | } |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 497 | } |