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