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 | |
| 10 | import org.apache.commons.configuration.BaseConfiguration; |
| 11 | import org.apache.commons.configuration.Configuration; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 12 | import org.openflow.protocol.OFPhysicalPort; |
| 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; |
| 68 | try { |
| 69 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 70 | log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 71 | // TODO: Check if port exists |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 72 | 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^] | 73 | //TODO: Do nothing for now |
| 74 | log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber()); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 75 | } else { |
| 76 | Vertex p = graph.addVertex(null); |
| 77 | p.setProperty("type","port"); |
| 78 | p.setProperty("number",port.getPortNumber()); |
| 79 | p.setProperty("state",port.getState()); |
| 80 | p.setProperty("desc",port.getName()); |
| 81 | Edge e = graph.addEdge(null, sw, p, "on"); |
| 82 | e.setProperty("state","ACTIVE"); |
| 83 | e.setProperty("number", port.getPortNumber()); |
| 84 | |
| 85 | graph.stopTransaction(Conclusion.SUCCESS); |
| 86 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 87 | } |
| 88 | } catch (TitanException e) { |
| 89 | // TODO: handle exceptions |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame^] | 90 | log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<OFPhysicalPort> getPorts(long dpid) { |
| 97 | // TODO Auto-generated method stub |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public OFPhysicalPort getPort(String dpid, short portnum) { |
| 103 | // TODO Auto-generated method stub |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public OFPhysicalPort getPort(String dpid, String portName) { |
| 109 | // TODO Auto-generated method stub |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void addSwitch(String dpid) { |
| 115 | |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 116 | log.info("SwitchStorage:addSwitch(): dpid {} ", dpid); |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 117 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 118 | try { |
| 119 | if (graph.getVertices("dpid",dpid).iterator().hasNext()) { |
| 120 | /* |
| 121 | * Do nothing or throw exception? |
| 122 | */ |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 123 | log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 124 | } else { |
| 125 | Vertex sw = graph.addVertex(null); |
| 126 | |
| 127 | sw.setProperty("type","switch"); |
| 128 | sw.setProperty("dpid", dpid); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 129 | sw.setProperty("state",SwitchState.ACTIVE.toString()); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 130 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 131 | log.info("SwitchStorage:addSwitch dpid:{} added", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 132 | } |
| 133 | } catch (TitanException e) { |
| 134 | /* |
| 135 | * retry till we succeed? |
| 136 | */ |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 137 | log.info("SwitchStorage:addSwitch dpid:{} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void deleteSwitch(String dpid) { |
| 145 | // TODO Setting inactive but we need to eventually remove data |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame^] | 146 | Vertex sw; |
| 147 | try { |
| 148 | |
| 149 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 150 | graph.removeVertex(sw); |
| 151 | graph.stopTransaction(Conclusion.SUCCESS); |
| 152 | log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid); |
| 153 | } |
| 154 | } catch (TitanException e) { |
| 155 | // TODO: handle exceptions |
| 156 | log.error("SwitchStorage:deleteSwitch {} failed", dpid); |
| 157 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 158 | |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void deletePort(String dpid, short port) { |
| 163 | // TODO Auto-generated method stub |
| 164 | Vertex sw; |
| 165 | try { |
| 166 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 167 | // TODO: Check if port exists |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 168 | log.info("SwitchStorage:deletePort dpid:{} port:{}", dpid, port); |
| 169 | if (sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().hasNext()) { |
| 170 | Vertex p = sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
| 171 | log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 172 | graph.removeVertex(p); |
| 173 | graph.stopTransaction(Conclusion.SUCCESS); |
| 174 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 175 | } |
| 176 | } catch (TitanException e) { |
| 177 | // TODO: handle exceptions |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 178 | log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 179 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void deletePort(String dpid, String portName) { |
| 184 | // TODO Auto-generated method stub |
| 185 | |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public List<String> getActiveSwitches() { |
| 190 | // TODO Auto-generated method stub |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void init(String conf) { |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 196 | //TODO extract the DB location from conf |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame] | 197 | Configuration db = new BaseConfiguration(); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 198 | |
| 199 | // Local storage gets locked for single process |
Pankaj Berde | cb703a5 | 2013-01-09 18:06:33 -0800 | [diff] [blame] | 200 | db.setProperty("storage.backend","cassandra"); |
| 201 | db.setProperty("storage.hostname","127.0.0.1"); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 202 | graph = TitanFactory.open(db); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 203 | |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 204 | // FIXME: Creation on Indexes should be done only once |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 205 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 206 | if (!s.contains("dpid")) { |
| 207 | graph.createKeyIndex("dpid", Vertex.class); |
Pankaj Berde | cb703a5 | 2013-01-09 18:06:33 -0800 | [diff] [blame] | 208 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 209 | } |
| 210 | if (!s.contains("type")) { |
| 211 | graph.createKeyIndex("type", Vertex.class); |
Pankaj Berde | cb703a5 | 2013-01-09 18:06:33 -0800 | [diff] [blame] | 212 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | 28cc61c | 2013-01-08 18:19:33 -0800 | [diff] [blame] | 213 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | } |