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