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 | /** |
| 17 | * This is the class for storing the information of switches into CassandraDB |
| 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; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 21 | protected 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 | |
| 50 | private void setStatus(String dpid, SwitchState state) { |
| 51 | ISwitchObject sw = op.searchSwitch(dpid); |
| 52 | |
| 53 | try { |
| 54 | if (sw != null) { |
| 55 | sw.setState(state.toString()); |
| 56 | op.commit(); |
| 57 | log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state); |
| 58 | } |
| 59 | } catch(Exception e) { |
| 60 | e.printStackTrace(); |
| 61 | op.rollback(); |
| 62 | log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /*** |
| 67 | * This function is for adding the switch into the DB. |
| 68 | * @param dpid The switch dpid you want to add into the DB. |
| 69 | */ |
| 70 | @Override |
| 71 | public void addSwitch(String dpid) { |
| 72 | |
| 73 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 74 | try { |
HIGUCHI Yuta | fabb003 | 2013-06-28 11:31:33 -0700 | [diff] [blame] | 75 | ISwitchObject sw = newSwitch(dpid); |
| 76 | if ( sw == null ) throw new RuntimeException(); |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 77 | op.commit(); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 78 | } catch (Exception e) { |
| 79 | e.printStackTrace(); |
| 80 | op.rollback(); |
| 81 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 82 | } |
| 83 | } |
| 84 | |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 85 | private ISwitchObject newSwitch(String dpid) { |
| 86 | ISwitchObject sw = op.searchSwitch(dpid); |
| 87 | if (sw != null) { |
| 88 | //If existing the switch. set The SW state ACTIVE. |
| 89 | log.info("SwitchStorage:newSwitch dpid:{} already exists", dpid); |
| 90 | sw.setState(SwitchState.ACTIVE.toString()); |
| 91 | } else { |
| 92 | sw = op.newSwitch(dpid); |
| 93 | if (sw != null) { |
| 94 | sw.setState(SwitchState.ACTIVE.toString()); |
| 95 | log.info("SwitchStorage:newSwitch dpid:{} added", dpid); |
| 96 | } else { |
| 97 | log.error("switchStorage:newSwitch dpid:{} failed -> newSwitch failed", dpid); |
| 98 | } |
| 99 | } |
| 100 | return sw; |
| 101 | } |
| 102 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 103 | /*** |
| 104 | * This function is for updating the switch into the DB. |
| 105 | * @param dpid The switch dpid you want to update from the DB |
| 106 | * @param state The state of the switch like ACTIVE, INACTIVE |
| 107 | * @param dmope The DM_OPERATION of the switch |
| 108 | */ |
| 109 | @Override |
| 110 | public void update(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 111 | log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state); |
| 112 | switch(dmope) { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 113 | case UPDATE: |
| 114 | case INSERT: |
| 115 | case CREATE: |
| 116 | addSwitch(dpid); |
| 117 | if (state != SwitchState.ACTIVE) { |
| 118 | setStatus(dpid, state); |
| 119 | } |
| 120 | break; |
| 121 | case DELETE: |
| 122 | deleteSwitch(dpid); |
| 123 | break; |
| 124 | default: |
| 125 | } |
| 126 | } |
| 127 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 128 | /*** |
| 129 | * This function is for deleting the switch into the DB. |
| 130 | * @param dpid The switch dpid you want to delete from the DB. |
| 131 | */ |
| 132 | @Override |
| 133 | public void deleteSwitch(String dpid) { |
| 134 | try { |
| 135 | ISwitchObject sw = op.searchSwitch(dpid); |
| 136 | if (sw != null) { |
| 137 | op.removeSwitch(sw); |
| 138 | op.commit(); |
| 139 | log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid); |
| 140 | } |
| 141 | } catch (Exception e) { |
| 142 | e.printStackTrace(); |
| 143 | op.rollback(); |
| 144 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 145 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 146 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 149 | /*** |
| 150 | * This function is for adding the switch port into the DB. |
| 151 | * @param dpid The switch dpid that has the port. |
| 152 | * @param port The port you want to add the switch. |
| 153 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 154 | @Override |
| 155 | public void addPort(String dpid, OFPhysicalPort port) { |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 156 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 157 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) || |
| 158 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) { |
| 159 | deletePort(dpid, port.getPortNumber()); |
| 160 | return; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 161 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 162 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 163 | try { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 164 | ISwitchObject sw = op.searchSwitch(dpid); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 165 | |
| 166 | if (sw != null) { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 167 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 168 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 169 | if (p != null) { |
Pankaj Berde | e7c2152 | 2013-08-01 16:52:29 -0700 | [diff] [blame] | 170 | log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, port.getPortNumber()); |
| 171 | p.setState("ACTIVE"); |
| 172 | p.setPortState(port.getState()); |
| 173 | p.setDesc(port.getName()); |
| 174 | op.commit(); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 175 | } else { |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 176 | p = op.newPort(dpid, port.getPortNumber()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 177 | p.setState("ACTIVE"); |
| 178 | p.setPortState(port.getState()); |
| 179 | p.setDesc(port.getName()); |
| 180 | sw.addPort(p); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 181 | op.commit(); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 182 | } |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 183 | } else { |
| 184 | log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 185 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 186 | } catch (Exception e) { |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 187 | e.printStackTrace(); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 188 | op.rollback(); |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 189 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 193 | |
| 194 | /*** |
| 195 | * This function is for deleting the switch port from the DB. |
| 196 | * @param dpid The switch dpid that has the port. |
| 197 | * @param port The port you want to delete the switch. |
| 198 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 199 | @Override |
| 200 | public void deletePort(String dpid, short port) { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 201 | try { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 202 | ISwitchObject sw = op.searchSwitch(dpid); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 203 | |
| 204 | if (sw != null) { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 205 | IPortObject p = op.searchPort(dpid, port); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 206 | if (p != null) { |
Pankaj Berde | e7c2152 | 2013-08-01 16:52:29 -0700 | [diff] [blame] | 207 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port); |
| 208 | p.setState("INACTIVE"); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 209 | op.commit(); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 210 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 211 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 212 | } catch (Exception e) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 213 | e.printStackTrace(); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 214 | op.rollback(); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 215 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 216 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 217 | } |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public void addSwitch(IOFSwitch sw) { |
| 221 | // TODO Auto-generated method stub |
| 222 | String dpid = sw.getStringId(); |
| 223 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 224 | try { |
| 225 | ISwitchObject switchObject = newSwitch(dpid); |
| 226 | for (OFPhysicalPort port: sw.getPorts()) { |
| 227 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
| 228 | if (p != null) { |
| 229 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
Jonathan Hart | 55a6aff | 2013-09-22 17:23:37 +1200 | [diff] [blame] | 230 | p.setState("ACTIVE"); |
| 231 | p.setPortState(port.getState()); |
| 232 | p.setDesc(port.getName()); |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 233 | } else { |
| 234 | p = op.newPort(dpid, port.getPortNumber()); |
| 235 | p.setState("ACTIVE"); |
| 236 | p.setPortState(port.getState()); |
| 237 | p.setDesc(port.getName()); |
| 238 | switchObject.addPort(p); |
| 239 | } |
| 240 | } |
| 241 | op.commit(); |
| 242 | } catch (Exception e) { |
| 243 | e.printStackTrace(); |
| 244 | op.rollback(); |
| 245 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 246 | } |
| 247 | |
| 248 | } |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 249 | } |