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