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