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