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 | |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 3 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 4 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 5 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 6 | import net.onrc.onos.util.GraphDBConnection; |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 7 | import net.onrc.onos.util.GraphDBOperation; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 8 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 9 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 10 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 11 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 14 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 15 | /** |
| 16 | * This is the class for storing the information of switches into CassandraDB |
| 17 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 18 | public class SwitchStorageImpl implements ISwitchStorage { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 19 | protected GraphDBOperation op; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 20 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 21 | |
| 22 | /*** |
| 23 | * Initialize function. Before you use this class, please call this method |
| 24 | * @param conf configuration file for Cassandra DB |
| 25 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 26 | @Override |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 27 | public void init(String conf) { |
| 28 | GraphDBConnection conn = GraphDBConnection.getInstance(conf); |
| 29 | op = new GraphDBOperation(conn); |
| 30 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 31 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 32 | /*** |
| 33 | * Finalize/close function. After you use this class, please call this method. |
| 34 | * It will close the DB connection. |
| 35 | */ |
| 36 | public void finalize() { |
| 37 | close(); |
| 38 | } |
| 39 | |
| 40 | /*** |
| 41 | * Finalize/close function. After you use this class, please call this method. |
| 42 | * It will close the DB connection. This is for Java gabage collection. |
| 43 | */ |
| 44 | @Override |
| 45 | public void close() { |
| 46 | op.close(); |
| 47 | } |
| 48 | |
| 49 | private void setStatus(String dpid, SwitchState state) { |
| 50 | ISwitchObject sw = op.searchSwitch(dpid); |
| 51 | |
| 52 | try { |
| 53 | if (sw != null) { |
| 54 | sw.setState(state.toString()); |
| 55 | op.commit(); |
| 56 | log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state); |
| 57 | } |
| 58 | } catch(Exception e) { |
| 59 | e.printStackTrace(); |
| 60 | op.rollback(); |
| 61 | log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /*** |
| 66 | * This function is for adding the switch into the DB. |
| 67 | * @param dpid The switch dpid you want to add into the DB. |
| 68 | */ |
| 69 | @Override |
| 70 | public void addSwitch(String dpid) { |
| 71 | |
| 72 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 73 | try { |
| 74 | ISwitchObject sw = op.searchSwitch(dpid); |
| 75 | if (sw != null) { |
| 76 | //If existing the switch. set The SW state ACTIVE. |
| 77 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 78 | sw.setState(SwitchState.ACTIVE.toString()); |
| 79 | op.commit(); |
| 80 | } else { |
| 81 | sw = op.newSwitch(dpid); |
| 82 | |
| 83 | if (sw != null) { |
| 84 | sw.setState(SwitchState.ACTIVE.toString()); |
| 85 | op.commit(); |
| 86 | log.info("SwitchStorage:addSwitch dpid:{} added", dpid); |
| 87 | } else { |
| 88 | log.error("switchStorage:addSwitch dpid:{} failed -> newSwitch failed", dpid); |
| 89 | } |
| 90 | } |
| 91 | } catch (Exception e) { |
| 92 | e.printStackTrace(); |
| 93 | op.rollback(); |
| 94 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /*** |
| 99 | * This function is for updating the switch into the DB. |
| 100 | * @param dpid The switch dpid you want to update from the DB |
| 101 | * @param state The state of the switch like ACTIVE, INACTIVE |
| 102 | * @param dmope The DM_OPERATION of the switch |
| 103 | */ |
| 104 | @Override |
| 105 | public void update(String dpid, SwitchState state, DM_OPERATION dmope) { |
| 106 | log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state); |
| 107 | switch(dmope) { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 108 | case UPDATE: |
| 109 | case INSERT: |
| 110 | case CREATE: |
| 111 | addSwitch(dpid); |
| 112 | if (state != SwitchState.ACTIVE) { |
| 113 | setStatus(dpid, state); |
| 114 | } |
| 115 | break; |
| 116 | case DELETE: |
| 117 | deleteSwitch(dpid); |
| 118 | break; |
| 119 | default: |
| 120 | } |
| 121 | } |
| 122 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 123 | /*** |
| 124 | * This function is for deleting the switch into the DB. |
| 125 | * @param dpid The switch dpid you want to delete from the DB. |
| 126 | */ |
| 127 | @Override |
| 128 | public void deleteSwitch(String dpid) { |
| 129 | try { |
| 130 | ISwitchObject sw = op.searchSwitch(dpid); |
| 131 | if (sw != null) { |
| 132 | op.removeSwitch(sw); |
| 133 | op.commit(); |
| 134 | log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid); |
| 135 | } |
| 136 | } catch (Exception e) { |
| 137 | e.printStackTrace(); |
| 138 | op.rollback(); |
| 139 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 140 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 141 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 144 | /*** |
| 145 | * This function is for adding the switch port into the DB. |
| 146 | * @param dpid The switch dpid that has the port. |
| 147 | * @param port The port you want to add the switch. |
| 148 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 149 | @Override |
| 150 | public void addPort(String dpid, OFPhysicalPort port) { |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 151 | |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 152 | if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) || |
| 153 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) { |
| 154 | deletePort(dpid, port.getPortNumber()); |
| 155 | return; |
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 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 158 | try { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 159 | ISwitchObject sw = op.searchSwitch(dpid); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 160 | |
| 161 | if (sw != null) { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 162 | IPortObject p = op.searchPort(dpid, port.getPortNumber()); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 163 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 164 | if (p != null) { |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 165 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 166 | } else { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 167 | p = op.newPort(port.getPortNumber()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 168 | p.setState("ACTIVE"); |
| 169 | p.setPortState(port.getState()); |
| 170 | p.setDesc(port.getName()); |
| 171 | sw.addPort(p); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 172 | op.commit(); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 173 | } |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 174 | } else { |
| 175 | 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] | 176 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 177 | } catch (Exception e) { |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 178 | e.printStackTrace(); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 179 | op.rollback(); |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 180 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } |
Teru | ef33dc3 | 2013-06-20 09:54:37 -0700 | [diff] [blame] | 184 | |
| 185 | /*** |
| 186 | * This function is for deleting the switch port from the DB. |
| 187 | * @param dpid The switch dpid that has the port. |
| 188 | * @param port The port you want to delete the switch. |
| 189 | */ |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 190 | @Override |
| 191 | public void deletePort(String dpid, short port) { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 192 | try { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 193 | ISwitchObject sw = op.searchSwitch(dpid); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 194 | |
| 195 | if (sw != null) { |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 196 | IPortObject p = op.searchPort(dpid, port); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 197 | if (p != null) { |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 198 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 199 | sw.removePort(p); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 200 | op.removePort(p); |
| 201 | op.commit(); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 202 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 203 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 204 | } catch (Exception e) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 205 | e.printStackTrace(); |
Toshio Koide | 4f3d9eb | 2013-06-13 13:20:55 -0700 | [diff] [blame] | 206 | op.rollback(); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 207 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 208 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 209 | } |
Teru | 4fd5864 | 2013-06-21 07:54:49 -0700 | [diff] [blame] | 210 | } |