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); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 28 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 29 | /*** |
| 30 | * Initialize function. Before you use this class, please call this method |
| 31 | * @param conf configuration file for Cassandra DB |
| 32 | */ |
| 33 | @Override |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 34 | public void init(final String dbStore, final String conf) { |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 35 | op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
| 36 | //op = GraphDBManager.getDBOperation(dbStore, conf); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 37 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 38 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 39 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 40 | /*** |
| 41 | * Finalize/close function. After you use this class, please call this method. |
| 42 | * It will close the DB connection. |
| 43 | */ |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 44 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 45 | public void finalize() { |
| 46 | close(); |
| 47 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 48 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 49 | /*** |
| 50 | * Finalize/close function. After you use this class, please call this method. |
| 51 | * It will close the DB connection. This is for Java garbage collection. |
| 52 | */ |
| 53 | @Override |
| 54 | public void close() { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 55 | op.close(); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 56 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 57 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 58 | // Method designing policy: |
| 59 | // op.commit() and op.rollback() MUST called in public (first-class) methods. |
| 60 | // A first-class method MUST NOT call other first-class method. |
| 61 | // Routine process should be implemented in private method. |
| 62 | // A private method MUST NOT call commit or rollback. |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 63 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 64 | /*** |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 65 | * This function is for updating the switch into the DB. |
| 66 | * @param dpid The switch dpid you want to update from the DB |
| 67 | * @param state The state of the switch like ACTIVE, INACTIVE |
| 68 | * @param dmope The DM_OPERATION of the switch |
| 69 | */ |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 70 | /* |
| 71 | * Jono, 11/8/2013 |
| 72 | * We don't need this update method that demultiplexes DM_OPERATIONS, |
| 73 | * we can have clients just call the required methods directly. |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 74 | * We especially don't need this update method to re-implement |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 75 | * the functions of other methods. |
| 76 | */ |
| 77 | @Deprecated |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 78 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 79 | public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 80 | boolean success = false; |
| 81 | ISwitchObject sw = null; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 82 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 83 | log.info("SwitchStorage:update {} dpid:{}", dmope, dpid); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 84 | switch(dmope) { |
| 85 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 86 | try { |
| 87 | sw = op.searchSwitch(dpid); |
| 88 | if (sw != null) { |
| 89 | setSwitchStateImpl(sw, state); |
| 90 | op.commit(); |
| 91 | success = true; |
| 92 | } |
| 93 | } catch (Exception e) { |
| 94 | op.rollback(); |
| 95 | e.printStackTrace(); |
| 96 | log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid); |
| 97 | } |
| 98 | break; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 99 | case INSERT: |
| 100 | case CREATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 101 | try { |
| 102 | sw = addSwitchImpl(dpid); |
| 103 | if (sw != null) { |
| 104 | if (state != SwitchState.ACTIVE) { |
| 105 | setSwitchStateImpl(sw, state); |
| 106 | } |
| 107 | op.commit(); |
| 108 | success = true; |
| 109 | } |
| 110 | } catch (Exception e) { |
| 111 | op.rollback(); |
| 112 | e.printStackTrace(); |
| 113 | log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid); |
| 114 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 115 | break; |
| 116 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 117 | try { |
| 118 | sw = op.searchSwitch(dpid); |
| 119 | if (sw != null) { |
| 120 | deleteSwitchImpl(sw); |
| 121 | op.commit(); |
| 122 | success = true; |
| 123 | } |
| 124 | } catch (Exception e) { |
| 125 | op.rollback(); |
| 126 | e.printStackTrace(); |
| 127 | log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid); |
| 128 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 129 | break; |
| 130 | default: |
| 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 | return success; |
| 134 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 135 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public boolean addSwitch(IOFSwitch sw) { |
| 138 | boolean success = false; |
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 | String dpid = sw.getStringId(); |
| 141 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 142 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 143 | try { |
| 144 | ISwitchObject curr = op.searchSwitch(dpid); |
| 145 | if (curr != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 146 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 147 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 148 | setSwitchStateImpl(curr, SwitchState.ACTIVE); |
| 149 | } else { |
| 150 | curr = addSwitchImpl(dpid); |
| 151 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 152 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 153 | for (OFPhysicalPort port: sw.getPorts()) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 154 | //addPort(dpid, port); |
| 155 | addPortImpl(curr, port); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 156 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 157 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 158 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 159 | // XXX for now delete devices when we change a port to prevent |
| 160 | // having stale devices. |
| 161 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 162 | deviceStorage.init("",""); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 163 | for (IPortObject portObject : curr.getPorts()) { |
| 164 | for (IDeviceObject deviceObject : portObject.getDevices()) { |
| 165 | // The deviceStorage has to remove on the object gained by its own |
| 166 | // FramedGraph, it can't use our objects from here |
| 167 | deviceStorage.removeDeviceImpl(deviceStorage.getDeviceByMac(deviceObject.getMACAddress())); |
| 168 | } |
| 169 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 170 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 171 | op.commit(); |
| 172 | success = true; |
| 173 | } catch (Exception e) { |
| 174 | op.rollback(); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 175 | log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 176 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 177 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 178 | return success; |
| 179 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 180 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 181 | /*** |
| 182 | * This function is for adding the switch into the DB. |
| 183 | * @param dpid The switch dpid you want to add into the DB. |
| 184 | */ |
Jonathan Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 185 | // This method is only called by tests, so we probably don't need it. |
| 186 | // If we need both addSwitch interfaces, one should call the other |
| 187 | // rather than implementing the same logic twice. |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 188 | @Deprecated |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public boolean addSwitch(String dpid) { |
| 191 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 192 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 193 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 194 | try { |
| 195 | ISwitchObject sw = op.searchSwitch(dpid); |
| 196 | if (sw != null) { |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 197 | //If existing the switch. set The SW state ACTIVE. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 198 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 199 | setSwitchStateImpl(sw, SwitchState.ACTIVE); |
| 200 | } else { |
| 201 | addSwitchImpl(dpid); |
| 202 | } |
| 203 | op.commit(); |
| 204 | success = true; |
| 205 | } catch (Exception e) { |
| 206 | op.rollback(); |
| 207 | e.printStackTrace(); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 208 | log.error("SwitchStorage:addSwitch dpid:{} failed", dpid, e); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 209 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 210 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 211 | return success; |
| 212 | } |
| 213 | |
| 214 | /*** |
| 215 | * This function is for deleting the switch into the DB. |
| 216 | * @param dpid The switch dpid you want to delete from the DB. |
| 217 | */ |
| 218 | @Override |
| 219 | public boolean deleteSwitch(String dpid) { |
| 220 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 221 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 222 | try { |
| 223 | ISwitchObject sw = op.searchSwitch(dpid); |
| 224 | if (sw != null) { |
| 225 | deleteSwitchImpl(sw); |
| 226 | op.commit(); |
| 227 | } |
| 228 | success = true; |
| 229 | } catch (Exception e) { |
| 230 | op.rollback(); |
| 231 | e.printStackTrace(); |
| 232 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
| 233 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 234 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 235 | return success; |
| 236 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 237 | |
| 238 | @Override |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 239 | public boolean deactivateSwitch(String dpid) { |
| 240 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 241 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 242 | try { |
| 243 | ISwitchObject switchObject = op.searchSwitch(dpid); |
| 244 | if (switchObject != null) { |
| 245 | setSwitchStateImpl(switchObject, SwitchState.INACTIVE); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 246 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 247 | for (IPortObject portObject : switchObject.getPorts()) { |
| 248 | portObject.setState("INACTIVE"); |
| 249 | } |
| 250 | op.commit(); |
| 251 | success = true; |
| 252 | } |
| 253 | else { |
| 254 | log.warn("Switch {} not found when trying to deactivate", dpid); |
| 255 | } |
| 256 | } catch (Exception e) { |
| 257 | // TODO what type of exception is thrown when we can't commit? |
| 258 | op.rollback(); |
| 259 | log.error("SwitchStorage:deactivateSwitch {} failed", dpid, e); |
| 260 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 261 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 262 | return success; |
| 263 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 264 | |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 265 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 266 | public boolean updatePort(String dpid, short portNum, int state, String desc) { |
| 267 | boolean success = false; |
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 | try { |
| 270 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 271 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 272 | if (sw != null) { |
| 273 | IPortObject p = sw.getPort(portNum); |
| 274 | log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum); |
| 275 | if (p != null) { |
| 276 | setPortStateImpl(p, state, desc); |
Pavlin Radoslavov | e86bbfc | 2013-11-05 08:32:14 -0800 | [diff] [blame] | 277 | op.commit(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 278 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 279 | success = true; |
| 280 | } else { |
| 281 | log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum); |
| 282 | } |
| 283 | } catch (Exception e) { |
| 284 | op.rollback(); |
| 285 | e.printStackTrace(); |
| 286 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 287 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 288 | |
| 289 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /*** |
| 293 | * This function is for adding the switch port into the DB. |
| 294 | * @param dpid The switch dpid that has the port. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 295 | * @param phport The port you want to add the switch. |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 296 | */ |
| 297 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 298 | public boolean addPort(String dpid, OFPhysicalPort phport) { |
| 299 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 300 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 301 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) || |
| 302 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) { |
| 303 | // just dispatch to deletePort() |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 304 | // TODO This is wrong. We need to make sure the port is in the |
| 305 | // DB with the correct info and port state. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 306 | return deletePort(dpid, phport.getPortNumber()); |
| 307 | } |
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 | try { |
| 310 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 311 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 312 | if (sw != null) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 313 | IPortObject portObject = addPortImpl(sw, phport); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 314 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 315 | // XXX for now delete devices when we change a port to prevent |
| 316 | // having stale devices. |
| 317 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 318 | deviceStorage.init("",""); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 319 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 320 | for (IDeviceObject deviceObject : portObject.getDevices()) { |
| 321 | deviceStorage.removeDevice(deviceObject); |
| 322 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 323 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 324 | op.commit(); |
| 325 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 326 | } else { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 327 | 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] | 328 | } |
| 329 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 330 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 331 | e.printStackTrace(); |
| 332 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber()); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 333 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 334 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 335 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /*** |
| 339 | * This function is for deleting the switch port from the DB. |
| 340 | * @param dpid The switch dpid that has the port. |
| 341 | * @param port The port you want to delete the switch. |
| 342 | */ |
| 343 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 344 | public boolean deletePort(String dpid, short port) { |
| 345 | boolean success = false; |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 346 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 347 | DeviceStorageImpl deviceStorage = new DeviceStorageImpl(); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 348 | deviceStorage.init("",""); |
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 | try { |
| 351 | ISwitchObject sw = op.searchSwitch(dpid); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 352 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 353 | if (sw != null) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 354 | IPortObject p = sw.getPort(port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 355 | if (p != null) { |
| 356 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 357 | p.setState("INACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 358 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 359 | // XXX for now delete devices when we change a port to prevent |
| 360 | // having stale devices. |
| 361 | for (IDeviceObject d : p.getDevices()) { |
| 362 | deviceStorage.removeDevice(d); |
| 363 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 364 | op.commit(); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 367 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 368 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 369 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 370 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 371 | e.printStackTrace(); |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 372 | log.error("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 375 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Pavlin Radoslavov | 64647d2 | 2013-11-04 19:07:03 -0800 | [diff] [blame] | 378 | /** |
| 379 | * Get list of all ports on the switch specified by given DPID. |
| 380 | * |
| 381 | * @param dpid DPID of desired switch. |
| 382 | * @return List of port IDs. Empty list if no port was found. |
| 383 | */ |
| 384 | @Override |
| 385 | public List<Short> getPorts(String dpid) { |
| 386 | List<Short> ports = new ArrayList<Short>(); |
| 387 | |
| 388 | ISwitchObject srcSw = op.searchSwitch(dpid); |
| 389 | if (srcSw != null) { |
| 390 | for (IPortObject srcPort : srcSw.getPorts()) { |
| 391 | ports.add(srcPort.getNumber()); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return ports; |
| 396 | } |
| 397 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 398 | private ISwitchObject addSwitchImpl(String dpid) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 399 | if (dpid != null) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 400 | ISwitchObject sw = op.newSwitch(dpid); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 401 | sw.setState(SwitchState.ACTIVE.toString()); |
| 402 | log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid); |
| 403 | return sw; |
| 404 | } else { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 405 | return null; |
| 406 | } |
| 407 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 408 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 409 | private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) { |
| 410 | if (sw != null && state != null) { |
| 411 | sw.setState(state.toString()); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 412 | log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}", |
| 413 | sw.getDPID(), state.toString()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 414 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 415 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 416 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 417 | private void deleteSwitchImpl(ISwitchObject sw) { |
| 418 | if (sw != null) { |
| 419 | op.removeSwitch(sw); |
| 420 | log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done", |
| 421 | sw.getDPID()); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 422 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 423 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 424 | |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 425 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 426 | private IPortObject addPortImpl(ISwitchObject sw, OFPhysicalPort phport) { |
| 427 | IPortObject portObject = op.searchPort(sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 428 | |
| 429 | log.info("SwitchStorage:addPort dpid:{} port:{}", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 430 | sw.getDPID(), phport.getPortNumber()); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 431 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 432 | if (portObject != null) { |
| 433 | setPortStateImpl(portObject, phport.getState(), phport.getName()); |
| 434 | portObject.setState("ACTIVE"); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 435 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 436 | // This a convoluted way of checking if the port is attached |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 437 | // or not, but doing it this way avoids using the |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 438 | // ISwitchObject.getPort method which uses GremlinGroovy query |
| 439 | // and takes forever. |
| 440 | boolean attached = false; |
| 441 | for (IPortObject portsOnSwitch : sw.getPorts()) { |
Yuta HIGUCHI | aa1fac7 | 2013-12-15 14:47:56 -0800 | [diff] [blame] | 442 | if (portsOnSwitch.getPortId().equals( portObject.getPortId() )) { |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 443 | attached = true; |
| 444 | break; |
| 445 | } |
| 446 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 447 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 448 | if (!attached) { |
| 449 | sw.addPort(portObject); |
| 450 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 451 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 452 | /* |
| 453 | if (sw.getPort(phport.getPortNumber()) == null) { |
| 454 | // The port exists but the switch has no "on" link to it |
| 455 | sw.addPort(portObject); |
| 456 | }*/ |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 457 | |
| 458 | log.info("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 459 | sw.getDPID(), phport.getPortNumber()); |
| 460 | } else { |
| 461 | //addPortImpl(sw, phport.getPortNumber()); |
| 462 | portObject = op.newPort(sw.getDPID(), phport.getPortNumber()); |
| 463 | portObject.setState("ACTIVE"); |
| 464 | setPortStateImpl(portObject, phport.getState(), phport.getName()); |
| 465 | sw.addPort(portObject); |
| 466 | log.info("SwitchStorage:addPort dpid:{} port:{} done", |
| 467 | sw.getDPID(), phport.getPortNumber()); |
| 468 | } |
Yuta 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 | return portObject; |
| 471 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 472 | // TODO There's an issue here where a port with that ID could already |
| 473 | // exist when we try to add this one (because it's left over from an |
| 474 | // old topology). We need to remove an old port with the same ID when |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 475 | // 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] | 476 | // 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] | 477 | /*private IPortObject addPortImpl(ISwitchObject sw, short portNum) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 478 | IPortObject p = op.newPort(sw.getDPID(), portNum); |
| 479 | p.setState("ACTIVE"); |
| 480 | sw.addPort(p); |
| 481 | log.info("SwitchStorage:addPortImpl dpid:{} port:{} done", |
| 482 | sw.getDPID(), portNum); |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 483 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 484 | return p; |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 485 | }*/ |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 486 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 487 | private void setPortStateImpl(IPortObject port, Integer state, String desc) { |
| 488 | if (port != null) { |
| 489 | if (state != null) { |
| 490 | port.setPortState(state); |
| 491 | } |
| 492 | if (desc != null) { |
| 493 | port.setDesc(desc); |
| 494 | } |
Yuta HIGUCHI | a0200a9 | 2013-12-15 14:50:41 -0800 | [diff] [blame] | 495 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 496 | log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done", |
| 497 | new Object[] {port.getPortId(), state, desc}); |
| 498 | } |
| 499 | } |
Yuta HIGUCHI | ef3828d | 2013-12-17 22:17:07 -0800 | [diff] [blame] | 500 | } |