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 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [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; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 12 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 13 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 14 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 15 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 16 | import org.slf4j.Logger; |
| 17 | import org.slf4j.LoggerFactory; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 18 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 19 | /** |
| 20 | * This is the class for storing the information of switches into CassandraDB |
| 21 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 22 | public class SwitchStorageImpl implements ISwitchStorage { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 23 | protected GraphDBOperation op; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 24 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 25 | |
| 26 | /*** |
| 27 | * Initialize function. Before you use this class, please call this method |
| 28 | * @param conf configuration file for Cassandra DB |
| 29 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 30 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 31 | public void init(String conf) { |
| 32 | GraphDBConnection conn = GraphDBConnection.getInstance(conf); |
| 33 | op = new GraphDBOperation(conn); |
| 34 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 35 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 36 | /*** |
| 37 | * Finalize/close function. After you use this class, please call this method. |
| 38 | * It will close the DB connection. |
| 39 | */ |
| 40 | public void finalize() { |
| 41 | close(); |
| 42 | } |
| 43 | |
| 44 | /*** |
| 45 | * Finalize/close function. After you use this class, please call this method. |
Pavlin Radoslavov | ef0cb00 | 2013-06-21 14:55:23 -0700 | [diff] [blame] | 46 | * It will close the DB connection. This is for Java garbage collection. |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 47 | */ |
| 48 | @Override |
| 49 | public void close() { |
| 50 | op.close(); |
| 51 | } |
| 52 | |
| 53 | private void setStatus(String dpid, SwitchState state) { |
| 54 | ISwitchObject sw = op.searchSwitch(dpid); |
| 55 | |
| 56 | try { |
| 57 | if (sw != null) { |
| 58 | sw.setState(state.toString()); |
| 59 | op.commit(); |
| 60 | log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame^] | 61 | // TODO publish UPDATE_SWITCH event here |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 62 | } |
| 63 | } catch(Exception e) { |
| 64 | e.printStackTrace(); |
| 65 | op.rollback(); |
| 66 | log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /*** |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 71 | * This function is for updating the switch into the DB. |
| 72 | * @param dpid The switch dpid you want to update from the DB |
| 73 | * @param state The state of the switch like ACTIVE, INACTIVE |
| 74 | * @param dmope The DM_OPERATION of the switch |
| 75 | */ |
| 76 | @Override |
| 77 | public void update(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 78 | log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame^] | 79 | switch(dmope) { |
| 80 | case UPDATE: |
| 81 | case INSERT: |
| 82 | case CREATE: |
| 83 | addSwitch(dpid); |
| 84 | if (state != SwitchState.ACTIVE) { |
| 85 | setStatus(dpid, state); |
| 86 | } |
| 87 | break; |
| 88 | case DELETE: |
| 89 | deleteSwitch(dpid); |
| 90 | break; |
| 91 | default: |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /*** |
| 96 | * This function is for adding the switch port into the DB. |
| 97 | * @param dpid The switch dpid that has the port. |
| 98 | * @param port The port you want to add the switch. |
| 99 | */ |
| 100 | @Override |
| 101 | public void addPort(String dpid, OFPhysicalPort port) { |
| 102 | |
| 103 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) || |
| 104 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) { |
| 105 | deletePort(dpid, port.getPortNumber()); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | ISwitchObject sw = op.searchSwitch(dpid); |
| 111 | |
| 112 | if (sw != null) { |
| 113 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
| 114 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber()); |
| 115 | if (p != null) { |
| 116 | log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, port.getPortNumber()); |
| 117 | p.setState("ACTIVE"); |
| 118 | p.setPortState(port.getState()); |
| 119 | p.setDesc(port.getName()); |
| 120 | op.commit(); |
| 121 | |
| 122 | // TODO publish UPDATE_PORT event here |
| 123 | } else { |
| 124 | p = op.newPort(dpid, port.getPortNumber()); |
| 125 | p.setState("ACTIVE"); |
| 126 | p.setPortState(port.getState()); |
| 127 | p.setDesc(port.getName()); |
| 128 | sw.addPort(p); |
| 129 | op.commit(); |
| 130 | |
| 131 | // TODO publish ADD_PORT event here |
| 132 | } |
| 133 | } else { |
| 134 | log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber()); |
| 135 | } |
| 136 | } catch (Exception e) { |
| 137 | e.printStackTrace(); |
| 138 | op.rollback(); |
| 139 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber()); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /*** |
| 145 | * This function is for deleting the switch port from the DB. |
| 146 | * @param dpid The switch dpid that has the port. |
| 147 | * @param port The port you want to delete the switch. |
| 148 | */ |
| 149 | @Override |
| 150 | public void deletePort(String dpid, short port) { |
| 151 | try { |
| 152 | ISwitchObject sw = op.searchSwitch(dpid); |
| 153 | |
| 154 | if (sw != null) { |
| 155 | IPortObject p = op.searchPort(dpid, port); |
| 156 | if (p != null) { |
| 157 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
| 158 | p.setState("INACTIVE"); |
| 159 | op.commit(); |
| 160 | // TODO publish DELETE_PORT event here |
| 161 | } |
| 162 | } |
| 163 | } catch (Exception e) { |
| 164 | e.printStackTrace(); |
| 165 | op.rollback(); |
| 166 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void addSwitch(IOFSwitch sw) { |
| 172 | String dpid = sw.getStringId(); |
| 173 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 174 | |
| 175 | ISwitchObject curr = op.searchSwitch(dpid); |
| 176 | if (curr != null) { |
| 177 | //If existing the switch. set The SW state ACTIVE. |
| 178 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 179 | setSwitchStateImpl(curr, SwitchState.ACTIVE); |
| 180 | } else { |
| 181 | curr = addSwitchImpl(dpid); |
| 182 | } |
| 183 | |
| 184 | try { |
| 185 | List<IPortObject> added_ports = new ArrayList<IPortObject>(); |
| 186 | List<IPortObject> updated_ports = new ArrayList<IPortObject>(); |
| 187 | for (OFPhysicalPort port: sw.getPorts()) { |
| 188 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
| 189 | if (p != null) { |
| 190 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
| 191 | p.setState("ACTIVE"); |
| 192 | p.setPortState(port.getState()); |
| 193 | p.setDesc(port.getName()); |
| 194 | |
| 195 | added_ports.add(p); |
| 196 | } else { |
| 197 | p = op.newPort(dpid, port.getPortNumber()); |
| 198 | p.setState("ACTIVE"); |
| 199 | p.setPortState(port.getState()); |
| 200 | p.setDesc(port.getName()); |
| 201 | curr.addPort(p); |
| 202 | |
| 203 | updated_ports.add(p); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | op.commit(); |
| 208 | |
| 209 | // TODO publish ADD_PORT event here |
| 210 | // TODO publish UPDATE_PORT event here |
| 211 | } catch (Exception e) { |
| 212 | e.printStackTrace(); |
| 213 | op.rollback(); |
| 214 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /*** |
| 219 | * This function is for adding the switch into the DB. |
| 220 | * @param dpid The switch dpid you want to add into the DB. |
| 221 | */ |
| 222 | @Override |
| 223 | public void addSwitch(String dpid) { |
| 224 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 225 | |
| 226 | ISwitchObject sw = op.searchSwitch(dpid); |
| 227 | if (sw != null) { |
| 228 | //If existing the switch. set The SW state ACTIVE. |
| 229 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 230 | setSwitchStateImpl(sw, SwitchState.ACTIVE); |
| 231 | } else { |
| 232 | addSwitchImpl(dpid); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private ISwitchObject addSwitchImpl(String dpid) { |
| 237 | try { |
| 238 | ISwitchObject sw = op.newSwitch(dpid); |
| 239 | if (sw != null) { |
| 240 | sw.setState(SwitchState.ACTIVE.toString()); |
| 241 | log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid); |
| 242 | } else { |
| 243 | log.error("switchStorage:addSwitchImpl dpid:{} failed", dpid); |
| 244 | throw new RuntimeException(); |
| 245 | } |
| 246 | |
| 247 | op.commit(); |
| 248 | |
| 249 | // TODO publish ADD_SWITCH event here |
| 250 | return sw; |
| 251 | } catch (Exception e) { |
| 252 | e.printStackTrace(); |
| 253 | op.rollback(); |
| 254 | log.info("SwitchStorage:addSwitchImpl dpid:{} failed", dpid); |
| 255 | return null; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) { |
| 260 | if (sw != null && state != null) { |
| 261 | sw.setState(state.toString()); |
| 262 | op.commit(); |
| 263 | |
| 264 | // TODO publish UPDATE_SWITCH event here |
| 265 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 268 | /*** |
| 269 | * This function is for deleting the switch into the DB. |
| 270 | * @param dpid The switch dpid you want to delete from the DB. |
| 271 | */ |
| 272 | @Override |
| 273 | public void deleteSwitch(String dpid) { |
| 274 | try { |
| 275 | ISwitchObject sw = op.searchSwitch(dpid); |
| 276 | if (sw != null) { |
| 277 | op.removeSwitch(sw); |
| 278 | op.commit(); |
| 279 | log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame^] | 280 | |
| 281 | // TODO publish DELETE_SWITCH event here |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 282 | } |
| 283 | } catch (Exception e) { |
| 284 | e.printStackTrace(); |
| 285 | op.rollback(); |
| 286 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 287 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 288 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 289 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 290 | |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 291 | } |