Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.internal; |
| 2 | |
Pankaj Berde | d1259e8 | 2013-01-23 14:10:00 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
| 5 | import java.util.List; |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 6 | import java.util.Set; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 7 | |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
| 10 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 11 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 12 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 13 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 14 | |
| 15 | import com.thinkaurelius.titan.core.TitanException; |
| 16 | import com.thinkaurelius.titan.core.TitanFactory; |
| 17 | import com.thinkaurelius.titan.core.TitanGraph; |
| 18 | import com.tinkerpop.blueprints.Direction; |
| 19 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 20 | import com.tinkerpop.blueprints.Edge; |
| 21 | import com.tinkerpop.blueprints.Vertex; |
| 22 | import net.floodlightcontroller.core.ISwitchStorage; |
| 23 | |
| 24 | public class SwitchStorageImpl implements ISwitchStorage { |
| 25 | public TitanGraph graph; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 26 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 27 | |
| 28 | @Override |
| 29 | public void update(String dpid, SwitchState state, DM_OPERATION op) { |
| 30 | // TODO Auto-generated method stub |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 31 | log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 32 | switch(op) { |
| 33 | |
| 34 | case UPDATE: |
| 35 | case INSERT: |
| 36 | case CREATE: |
| 37 | addSwitch(dpid); |
| 38 | if (state != SwitchState.ACTIVE) { |
| 39 | setStatus(dpid, state); |
| 40 | } |
| 41 | break; |
| 42 | case DELETE: |
| 43 | deleteSwitch(dpid); |
| 44 | break; |
| 45 | default: |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private void setStatus(String dpid, SwitchState state) { |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 50 | Vertex sw; |
| 51 | try { |
| 52 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 53 | sw.setProperty("state",state.toString()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 54 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 55 | log.info("SwitchStorage:setSTatus dpid:{} state: {} done", dpid, state); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 56 | } |
| 57 | } catch (TitanException e) { |
| 58 | // TODO: handle exceptions |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 59 | log.info("SwitchStorage:setSTatus dpid:{} state: {} failed", dpid, state); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void addPort(String dpid, OFPhysicalPort port) { |
| 67 | // TODO Auto-generated method stub |
| 68 | Vertex sw; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 69 | |
| 70 | boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) || |
| 71 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0); |
| 72 | if (portDown) { |
| 73 | deletePort(dpid, port.getPortNumber()); |
| 74 | return; |
| 75 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 76 | try { |
| 77 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 78 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 79 | // TODO: Check if port exists |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 80 | if (sw.query().direction(Direction.OUT).labels("on").has("number",port.getPortNumber()).vertices().iterator().hasNext()) { |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 81 | //TODO: Do nothing for now |
| 82 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 83 | } else { |
| 84 | Vertex p = graph.addVertex(null); |
| 85 | p.setProperty("type","port"); |
| 86 | p.setProperty("number",port.getPortNumber()); |
| 87 | p.setProperty("state",port.getState()); |
| 88 | p.setProperty("desc",port.getName()); |
| 89 | Edge e = graph.addEdge(null, sw, p, "on"); |
| 90 | e.setProperty("state","ACTIVE"); |
| 91 | e.setProperty("number", port.getPortNumber()); |
| 92 | |
| 93 | graph.stopTransaction(Conclusion.SUCCESS); |
| 94 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 95 | } |
| 96 | } catch (TitanException e) { |
| 97 | // TODO: handle exceptions |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 98 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Collection<OFPhysicalPort> getPorts(long dpid) { |
| 105 | // TODO Auto-generated method stub |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public OFPhysicalPort getPort(String dpid, short portnum) { |
| 111 | // TODO Auto-generated method stub |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public OFPhysicalPort getPort(String dpid, String portName) { |
| 117 | // TODO Auto-generated method stub |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void addSwitch(String dpid) { |
| 123 | |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 124 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 125 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 126 | try { |
| 127 | if (graph.getVertices("dpid",dpid).iterator().hasNext()) { |
| 128 | /* |
| 129 | * Do nothing or throw exception? |
| 130 | */ |
Pankaj Berde | 1ccc7fe | 2013-01-22 17:31:06 -0800 | [diff] [blame] | 131 | Vertex sw = graph.getVertices("dpid",dpid).iterator().next(); |
| 132 | |
| 133 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
| 134 | sw.setProperty("state",SwitchState.ACTIVE.toString()); |
| 135 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 136 | } else { |
| 137 | Vertex sw = graph.addVertex(null); |
| 138 | |
| 139 | sw.setProperty("type","switch"); |
| 140 | sw.setProperty("dpid", dpid); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 141 | sw.setProperty("state",SwitchState.ACTIVE.toString()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 142 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 143 | log.info("SwitchStorage:addSwitch dpid:{} added", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 144 | } |
| 145 | } catch (TitanException e) { |
| 146 | /* |
| 147 | * retry till we succeed? |
| 148 | */ |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 149 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void deleteSwitch(String dpid) { |
| 157 | // TODO Setting inactive but we need to eventually remove data |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 158 | Vertex sw; |
| 159 | try { |
| 160 | |
| 161 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 162 | graph.removeVertex(sw); |
| 163 | graph.stopTransaction(Conclusion.SUCCESS); |
| 164 | log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid); |
| 165 | } |
| 166 | } catch (TitanException e) { |
| 167 | // TODO: handle exceptions |
| 168 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
| 169 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 170 | |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void deletePort(String dpid, short port) { |
| 175 | // TODO Auto-generated method stub |
| 176 | Vertex sw; |
| 177 | try { |
| 178 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 179 | // TODO: Check if port exists |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 180 | log.info("SwitchStorage:deletePort dpid:{} port:{}", dpid, port); |
| 181 | if (sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().hasNext()) { |
| 182 | Vertex p = sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
| 183 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 184 | graph.removeVertex(p); |
| 185 | graph.stopTransaction(Conclusion.SUCCESS); |
| 186 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 187 | } |
| 188 | } catch (TitanException e) { |
| 189 | // TODO: handle exceptions |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 190 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 191 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void deletePort(String dpid, String portName) { |
| 196 | // TODO Auto-generated method stub |
| 197 | |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public List<String> getActiveSwitches() { |
Pankaj Berde | d1259e8 | 2013-01-23 14:10:00 -0800 | [diff] [blame] | 202 | // TODO Add unit test |
| 203 | List<String> switches = new ArrayList<String>(); |
| 204 | for (Vertex V : graph.getVertices("type","switch")) { |
| 205 | if (V.getProperty("state").equals(SwitchState.ACTIVE.toString())) { |
| 206 | switches.add((String) V.getProperty("dpid")); |
| 207 | } |
| 208 | } |
| 209 | return switches; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void init(String conf) { |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 214 | |
| 215 | graph = TitanFactory.open(conf); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 216 | |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 217 | // FIXME: Creation on Indexes should be done only once |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 218 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 219 | if (!s.contains("dpid")) { |
| 220 | graph.createKeyIndex("dpid", Vertex.class); |
Pankaj Berde | cb703a5 | 2013-01-09 18:06:33 -0800 | [diff] [blame] | 221 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 222 | } |
| 223 | if (!s.contains("type")) { |
| 224 | graph.createKeyIndex("type", Vertex.class); |
Pankaj Berde | cb703a5 | 2013-01-09 18:06:33 -0800 | [diff] [blame] | 225 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 226 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Pankaj Berde | d1259e8 | 2013-01-23 14:10:00 -0800 | [diff] [blame] | 229 | @Override |
| 230 | public List<String> getAllSwitches() { |
| 231 | // TODO Auto-generated method stub |
| 232 | List<String> switches = new ArrayList<String>(); |
| 233 | for (Vertex V : graph.getVertices("type","switch")) { |
| 234 | switches.add((String) V.getProperty("dpid")); |
| 235 | } |
| 236 | return switches; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public List<String> getInactiveSwitches() { |
| 241 | // TODO Auto-generated method stub |
| 242 | List<String> switches = new ArrayList<String>(); |
| 243 | for (Vertex V : graph.getVertices("type","switch")) { |
| 244 | if (V.getProperty("state").equals(SwitchState.INACTIVE.toString())) { |
| 245 | switches.add((String) V.getProperty("dpid")); |
| 246 | } |
| 247 | } |
| 248 | return switches; |
| 249 | } |
| 250 | |
| 251 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 252 | } |