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