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; |
| 5 | |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 6 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; |
| 8 | |
| 9 | import org.apache.commons.configuration.BaseConfiguration; |
| 10 | import org.apache.commons.configuration.Configuration; |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 11 | import org.openflow.protocol.OFPhysicalPort; |
| 12 | |
| 13 | import com.thinkaurelius.titan.core.TitanException; |
| 14 | import com.thinkaurelius.titan.core.TitanFactory; |
| 15 | import com.thinkaurelius.titan.core.TitanGraph; |
| 16 | import com.tinkerpop.blueprints.Direction; |
| 17 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 18 | import com.tinkerpop.blueprints.Edge; |
| 19 | import com.tinkerpop.blueprints.Vertex; |
| 20 | import net.floodlightcontroller.core.ISwitchStorage; |
| 21 | |
| 22 | public class SwitchStorageImpl implements ISwitchStorage { |
| 23 | public TitanGraph graph; |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 24 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 25 | |
| 26 | @Override |
| 27 | public void update(String dpid, SwitchState state, DM_OPERATION op) { |
| 28 | // TODO Auto-generated method stub |
| 29 | switch(op) { |
| 30 | |
| 31 | case UPDATE: |
| 32 | case INSERT: |
| 33 | case CREATE: |
| 34 | addSwitch(dpid); |
| 35 | if (state != SwitchState.ACTIVE) { |
| 36 | setStatus(dpid, state); |
| 37 | } |
| 38 | break; |
| 39 | case DELETE: |
| 40 | deleteSwitch(dpid); |
| 41 | break; |
| 42 | default: |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private void setStatus(String dpid, SwitchState state) { |
| 47 | // TODO Auto-generated method stub |
| 48 | Vertex sw; |
| 49 | try { |
| 50 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 51 | sw.setProperty("state",state); |
| 52 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 53 | log.trace("SwitchStorage:setSTatus dpid:{} state: {} done", dpid, state); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 54 | } |
| 55 | } catch (TitanException e) { |
| 56 | // TODO: handle exceptions |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 57 | log.trace("SwitchStorage:setSTatus dpid:{} state: {} failed", dpid, state); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void addPort(String dpid, OFPhysicalPort port) { |
| 65 | // TODO Auto-generated method stub |
| 66 | Vertex sw; |
| 67 | try { |
| 68 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 69 | log.trace("SwitchStorage:addPort dpid:{} port:{}", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 70 | // TODO: Check if port exists |
| 71 | Vertex p = graph.addVertex(null); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 72 | p.setProperty("type","port"); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 73 | p.setProperty("number",port.getPortNumber()); |
| 74 | p.setProperty("state",port.getState()); |
| 75 | p.setProperty("desc",port.getName()); |
| 76 | Edge e = graph.addEdge(null, sw, p, "on"); |
| 77 | e.setProperty("state","ACTIVE"); |
| 78 | e.setProperty("number", port.getPortNumber()); |
| 79 | |
| 80 | graph.stopTransaction(Conclusion.SUCCESS); |
| 81 | } |
| 82 | } catch (TitanException e) { |
| 83 | // TODO: handle exceptions |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 84 | log.trace("SwitchStorage:addPort dpid:{} port:{}", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Collection<OFPhysicalPort> getPorts(long dpid) { |
| 91 | // TODO Auto-generated method stub |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public OFPhysicalPort getPort(String dpid, short portnum) { |
| 97 | // TODO Auto-generated method stub |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public OFPhysicalPort getPort(String dpid, String portName) { |
| 103 | // TODO Auto-generated method stub |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void addSwitch(String dpid) { |
| 109 | |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 110 | log.trace("SwitchStorage:addSwitch(): dpid {} ", dpid); |
| 111 | |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 112 | try { |
| 113 | if (graph.getVertices("dpid",dpid).iterator().hasNext()) { |
| 114 | /* |
| 115 | * Do nothing or throw exception? |
| 116 | */ |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 117 | log.trace("SwitchStorage:addSwitch dpid:{} already exists", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 118 | } else { |
| 119 | Vertex sw = graph.addVertex(null); |
| 120 | |
| 121 | sw.setProperty("type","switch"); |
| 122 | sw.setProperty("dpid", dpid); |
| 123 | sw.setProperty("state",SwitchState.ACTIVE); |
| 124 | graph.stopTransaction(Conclusion.SUCCESS); |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 125 | log.trace("SwitchStorage:addSwitch dpid:{} added", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 126 | } |
| 127 | } catch (TitanException e) { |
| 128 | /* |
| 129 | * retry till we succeed? |
| 130 | */ |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 131 | log.trace("SwitchStorage:addSwitch dpid:{} failed", dpid); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void deleteSwitch(String dpid) { |
| 139 | // TODO Setting inactive but we need to eventually remove data |
| 140 | setStatus(dpid, SwitchState.INACTIVE); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void deletePort(String dpid, short port) { |
| 146 | // TODO Auto-generated method stub |
| 147 | Vertex sw; |
| 148 | try { |
| 149 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 150 | // TODO: Check if port exists |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 151 | log.trace("SwitchStorage:deletePort dpid:{} port:{}", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 152 | Vertex p = sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 153 | if (p != null) { |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 154 | log.trace("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 155 | graph.removeVertex(p); |
| 156 | graph.stopTransaction(Conclusion.SUCCESS); |
| 157 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 158 | } |
| 159 | } catch (TitanException e) { |
| 160 | // TODO: handle exceptions |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 161 | log.trace("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 162 | } |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void deletePort(String dpid, String portName) { |
| 167 | // TODO Auto-generated method stub |
| 168 | |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public List<String> getActiveSwitches() { |
| 173 | // TODO Auto-generated method stub |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void init(String conf) { |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 179 | //TODO extract the DB location from conf |
Pankaj Berde | afb2053 | 2013-01-08 15:05:24 -0800 | [diff] [blame^] | 180 | Configuration db = new BaseConfiguration(); |
| 181 | db.setProperty("storage,backend","local"); |
| 182 | db.setProperty("storage.directory","/tmp/netmap"); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 183 | graph = TitanFactory.open(db); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 184 | graph.createKeyIndex("dpid", Vertex.class); |
Pankaj Berde | 8557a46 | 2013-01-07 08:59:31 -0800 | [diff] [blame] | 185 | graph.createKeyIndex("type", Vertex.class); |
Pankaj Berde | 3200ea0 | 2013-01-04 15:48:36 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } |