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 | |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 3 | import net.floodlightcontroller.core.IOFSwitch; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 4 | import net.onrc.onos.graph.GraphDBConnection; |
| 5 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 6 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 7 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 8 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 9 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 10 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 11 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 12 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 15 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 16 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 17 | * This is the class for storing the information of switches into GraphDB |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 18 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 19 | public class SwitchStorageImpl implements ISwitchStorage { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 20 | protected GraphDBOperation op; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 21 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 22 | |
| 23 | /*** |
| 24 | * Initialize function. Before you use this class, please call this method |
| 25 | * @param conf configuration file for Cassandra DB |
| 26 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 27 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 28 | public void init(String conf) { |
| 29 | GraphDBConnection conn = GraphDBConnection.getInstance(conf); |
| 30 | op = new GraphDBOperation(conn); |
| 31 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 32 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 33 | /*** |
| 34 | * Finalize/close function. After you use this class, please call this method. |
| 35 | * It will close the DB connection. |
| 36 | */ |
| 37 | public void finalize() { |
| 38 | close(); |
| 39 | } |
| 40 | |
| 41 | /*** |
| 42 | * Finalize/close function. After you use this class, please call this method. |
Pavlin Radoslavov | ef0cb00 | 2013-06-21 14:55:23 -0700 | [diff] [blame] | 43 | * It will close the DB connection. This is for Java garbage collection. |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Override |
| 46 | public void close() { |
| 47 | op.close(); |
| 48 | } |
| 49 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 50 | // Method designing policy: |
| 51 | // op.commit() and op.rollback() MUST called in public (first-class) methods. |
| 52 | // A first-class method MUST NOT call other first-class method. |
| 53 | // Routine process should be implemented in private method. |
| 54 | // A private method MUST NOT call commit or rollback. |
| 55 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 56 | /*** |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 57 | * This function is for updating the switch into the DB. |
| 58 | * @param dpid The switch dpid you want to update from the DB |
| 59 | * @param state The state of the switch like ACTIVE, INACTIVE |
| 60 | * @param dmope The DM_OPERATION of the switch |
| 61 | */ |
| 62 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 63 | public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 64 | boolean success = false; |
| 65 | ISwitchObject sw = null; |
| 66 | |
| 67 | log.info("SwitchStorage:update {} dpid:{}", dmope, dpid); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 68 | switch(dmope) { |
| 69 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 70 | try { |
| 71 | sw = op.searchSwitch(dpid); |
| 72 | if (sw != null) { |
| 73 | setSwitchStateImpl(sw, state); |
| 74 | op.commit(); |
| 75 | success = true; |
| 76 | } |
| 77 | } catch (Exception e) { |
| 78 | op.rollback(); |
| 79 | e.printStackTrace(); |
| 80 | log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid); |
| 81 | } |
| 82 | break; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 83 | case INSERT: |
| 84 | case CREATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 85 | try { |
| 86 | sw = addSwitchImpl(dpid); |
| 87 | if (sw != null) { |
| 88 | if (state != SwitchState.ACTIVE) { |
| 89 | setSwitchStateImpl(sw, state); |
| 90 | } |
| 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 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 99 | break; |
| 100 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 101 | try { |
| 102 | sw = op.searchSwitch(dpid); |
| 103 | if (sw != null) { |
| 104 | deleteSwitchImpl(sw); |
| 105 | op.commit(); |
| 106 | success = true; |
| 107 | } |
| 108 | } catch (Exception e) { |
| 109 | op.rollback(); |
| 110 | e.printStackTrace(); |
| 111 | log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid); |
| 112 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 113 | break; |
| 114 | default: |
| 115 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 116 | |
| 117 | return success; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean addSwitch(IOFSwitch sw) { |
| 122 | boolean success = false; |
| 123 | |
| 124 | String dpid = sw.getStringId(); |
| 125 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 126 | |
| 127 | try { |
| 128 | ISwitchObject curr = op.searchSwitch(dpid); |
| 129 | if (curr != null) { |
| 130 | //If existing the switch. set The SW state ACTIVE. |
| 131 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 132 | setSwitchStateImpl(curr, SwitchState.ACTIVE); |
| 133 | } else { |
| 134 | curr = addSwitchImpl(dpid); |
| 135 | } |
| 136 | |
| 137 | for (OFPhysicalPort port: sw.getPorts()) { |
| 138 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
| 139 | if (p != null) { |
| 140 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
| 141 | setPortStateImpl(p, port.getState(), port.getName()); |
| 142 | p.setState("ACTIVE"); |
| 143 | } else { |
| 144 | p = addPortImpl(curr, port.getPortNumber()); |
| 145 | setPortStateImpl(p, port.getState(), port.getName()); |
| 146 | } |
| 147 | } |
| 148 | op.commit(); |
| 149 | success = true; |
| 150 | } catch (Exception e) { |
| 151 | op.rollback(); |
| 152 | e.printStackTrace(); |
| 153 | log.error("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 154 | } |
| 155 | |
| 156 | return success; |
| 157 | } |
| 158 | |
| 159 | /*** |
| 160 | * This function is for adding the switch into the DB. |
| 161 | * @param dpid The switch dpid you want to add into the DB. |
| 162 | */ |
| 163 | @Override |
| 164 | public boolean addSwitch(String dpid) { |
| 165 | boolean success = false; |
| 166 | |
| 167 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 168 | try { |
| 169 | ISwitchObject sw = op.searchSwitch(dpid); |
| 170 | if (sw != null) { |
| 171 | //If existing the switch. set The SW state ACTIVE. |
| 172 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 173 | setSwitchStateImpl(sw, SwitchState.ACTIVE); |
| 174 | } else { |
| 175 | addSwitchImpl(dpid); |
| 176 | } |
| 177 | op.commit(); |
| 178 | success = true; |
| 179 | } catch (Exception e) { |
| 180 | op.rollback(); |
| 181 | e.printStackTrace(); |
| 182 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 183 | } |
| 184 | |
| 185 | return success; |
| 186 | } |
| 187 | |
| 188 | /*** |
| 189 | * This function is for deleting the switch into the DB. |
| 190 | * @param dpid The switch dpid you want to delete from the DB. |
| 191 | */ |
| 192 | @Override |
| 193 | public boolean deleteSwitch(String dpid) { |
| 194 | boolean success = false; |
| 195 | |
| 196 | try { |
| 197 | ISwitchObject sw = op.searchSwitch(dpid); |
| 198 | if (sw != null) { |
| 199 | deleteSwitchImpl(sw); |
| 200 | op.commit(); |
| 201 | } |
| 202 | success = true; |
| 203 | } catch (Exception e) { |
| 204 | op.rollback(); |
| 205 | e.printStackTrace(); |
| 206 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
| 207 | } |
| 208 | |
| 209 | return success; |
| 210 | } |
| 211 | |
| 212 | public boolean updatePort(String dpid, short portNum, int state, String desc) { |
| 213 | boolean success = false; |
| 214 | |
| 215 | try { |
| 216 | ISwitchObject sw = op.searchSwitch(dpid); |
| 217 | |
| 218 | if (sw != null) { |
| 219 | IPortObject p = sw.getPort(portNum); |
| 220 | log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum); |
| 221 | if (p != null) { |
| 222 | setPortStateImpl(p, state, desc); |
| 223 | } |
| 224 | op.commit(); |
| 225 | success = true; |
| 226 | } else { |
| 227 | log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum); |
| 228 | } |
| 229 | } catch (Exception e) { |
| 230 | op.rollback(); |
| 231 | e.printStackTrace(); |
| 232 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum); |
| 233 | } |
| 234 | |
| 235 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /*** |
| 239 | * This function is for adding the switch port into the DB. |
| 240 | * @param dpid The switch dpid that has the port. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 241 | * @param phport The port you want to add the switch. |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 242 | */ |
| 243 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 244 | public boolean addPort(String dpid, OFPhysicalPort phport) { |
| 245 | boolean success = false; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 246 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 247 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) || |
| 248 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) { |
| 249 | // just dispatch to deletePort() |
| 250 | return deletePort(dpid, phport.getPortNumber()); |
| 251 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 252 | |
| 253 | try { |
| 254 | ISwitchObject sw = op.searchSwitch(dpid); |
| 255 | |
| 256 | if (sw != null) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 257 | IPortObject p = sw.getPort(phport.getPortNumber()); |
| 258 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, phport.getPortNumber()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 259 | if (p != null) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 260 | setPortStateImpl(p, phport.getState(), phport.getName()); |
| 261 | log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, phport.getPortNumber()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 262 | } else { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 263 | addPortImpl(sw, phport.getPortNumber()); |
| 264 | setPortStateImpl(p, phport.getState(), phport.getName()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 265 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 266 | op.commit(); |
| 267 | success = true; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 268 | } else { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 269 | 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] | 270 | } |
| 271 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 272 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 273 | e.printStackTrace(); |
| 274 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 275 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 276 | |
| 277 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /*** |
| 281 | * This function is for deleting the switch port from the DB. |
| 282 | * @param dpid The switch dpid that has the port. |
| 283 | * @param port The port you want to delete the switch. |
| 284 | */ |
| 285 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 286 | public boolean deletePort(String dpid, short port) { |
| 287 | boolean success = false; |
| 288 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 289 | try { |
| 290 | ISwitchObject sw = op.searchSwitch(dpid); |
| 291 | |
| 292 | if (sw != null) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 293 | IPortObject p = sw.getPort(port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 294 | if (p != null) { |
| 295 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 296 | deletePortImpl(p); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 297 | op.commit(); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } catch (Exception e) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 301 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 302 | e.printStackTrace(); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 303 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 306 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 309 | private ISwitchObject addSwitchImpl(String dpid) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 310 | if (dpid != null) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 311 | ISwitchObject sw = op.newSwitch(dpid); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 312 | sw.setState(SwitchState.ACTIVE.toString()); |
| 313 | log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid); |
| 314 | return sw; |
| 315 | } else { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 316 | return null; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) { |
| 321 | if (sw != null && state != null) { |
| 322 | sw.setState(state.toString()); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 323 | log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}", |
| 324 | sw.getDPID(), state.toString()); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 325 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 326 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 327 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 328 | private void deleteSwitchImpl(ISwitchObject sw) { |
| 329 | if (sw != null) { |
| 330 | op.removeSwitch(sw); |
| 331 | log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done", |
| 332 | sw.getDPID()); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private IPortObject addPortImpl(ISwitchObject sw, short portNum) { |
| 337 | IPortObject p = op.newPort(sw.getDPID(), portNum); |
| 338 | p.setState("ACTIVE"); |
| 339 | sw.addPort(p); |
| 340 | log.info("SwitchStorage:addPortImpl dpid:{} port:{} done", |
| 341 | sw.getDPID(), portNum); |
| 342 | |
| 343 | return p; |
| 344 | } |
| 345 | |
| 346 | private void setPortStateImpl(IPortObject port, Integer state, String desc) { |
| 347 | if (port != null) { |
| 348 | if (state != null) { |
| 349 | port.setPortState(state); |
| 350 | } |
| 351 | if (desc != null) { |
| 352 | port.setDesc(desc); |
| 353 | } |
| 354 | |
| 355 | log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done", |
| 356 | new Object[] {port.getPortId(), state, desc}); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private void deletePortImpl(IPortObject port) { |
| 361 | if (port != null) { |
| 362 | op.removePort(port); |
| 363 | log.info("SwitchStorage:deletePortImpl port:{} done", |
| 364 | port.getPortId()); |
| 365 | } |
| 366 | } |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 367 | } |