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 | |
| 6 | import org.openflow.protocol.OFPhysicalPort; |
| 7 | |
| 8 | import com.thinkaurelius.titan.core.TitanException; |
| 9 | import com.thinkaurelius.titan.core.TitanFactory; |
| 10 | import com.thinkaurelius.titan.core.TitanGraph; |
| 11 | import com.tinkerpop.blueprints.Direction; |
| 12 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 13 | import com.tinkerpop.blueprints.Edge; |
| 14 | import com.tinkerpop.blueprints.Vertex; |
| 15 | import net.floodlightcontroller.core.ISwitchStorage; |
| 16 | |
| 17 | public class SwitchStorageImpl implements ISwitchStorage { |
| 18 | public TitanGraph graph; |
| 19 | |
| 20 | @Override |
| 21 | public void update(String dpid, SwitchState state, DM_OPERATION op) { |
| 22 | // TODO Auto-generated method stub |
| 23 | switch(op) { |
| 24 | |
| 25 | case UPDATE: |
| 26 | case INSERT: |
| 27 | case CREATE: |
| 28 | addSwitch(dpid); |
| 29 | if (state != SwitchState.ACTIVE) { |
| 30 | setStatus(dpid, state); |
| 31 | } |
| 32 | break; |
| 33 | case DELETE: |
| 34 | deleteSwitch(dpid); |
| 35 | break; |
| 36 | default: |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | private void setStatus(String dpid, SwitchState state) { |
| 41 | // TODO Auto-generated method stub |
| 42 | Vertex sw; |
| 43 | try { |
| 44 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 45 | sw.setProperty("state",state); |
| 46 | graph.stopTransaction(Conclusion.SUCCESS); |
| 47 | } |
| 48 | } catch (TitanException e) { |
| 49 | // TODO: handle exceptions |
| 50 | } |
| 51 | |
| 52 | |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void addPort(String dpid, OFPhysicalPort port) { |
| 57 | // TODO Auto-generated method stub |
| 58 | Vertex sw; |
| 59 | try { |
| 60 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 61 | // TODO: Check if port exists |
| 62 | Vertex p = graph.addVertex(null); |
| 63 | p.setProperty("number",port.getPortNumber()); |
| 64 | p.setProperty("state",port.getState()); |
| 65 | p.setProperty("desc",port.getName()); |
| 66 | Edge e = graph.addEdge(null, sw, p, "on"); |
| 67 | e.setProperty("state","ACTIVE"); |
| 68 | e.setProperty("number", port.getPortNumber()); |
| 69 | |
| 70 | graph.stopTransaction(Conclusion.SUCCESS); |
| 71 | } |
| 72 | } catch (TitanException e) { |
| 73 | // TODO: handle exceptions |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Collection<OFPhysicalPort> getPorts(long dpid) { |
| 80 | // TODO Auto-generated method stub |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public OFPhysicalPort getPort(String dpid, short portnum) { |
| 86 | // TODO Auto-generated method stub |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public OFPhysicalPort getPort(String dpid, String portName) { |
| 92 | // TODO Auto-generated method stub |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void addSwitch(String dpid) { |
| 98 | |
| 99 | try { |
| 100 | if (graph.getVertices("dpid",dpid).iterator().hasNext()) { |
| 101 | /* |
| 102 | * Do nothing or throw exception? |
| 103 | */ |
| 104 | } else { |
| 105 | Vertex sw = graph.addVertex(null); |
| 106 | |
| 107 | sw.setProperty("type","switch"); |
| 108 | sw.setProperty("dpid", dpid); |
| 109 | sw.setProperty("state",SwitchState.ACTIVE); |
| 110 | graph.stopTransaction(Conclusion.SUCCESS); |
| 111 | } |
| 112 | } catch (TitanException e) { |
| 113 | /* |
| 114 | * retry till we succeed? |
| 115 | */ |
| 116 | } |
| 117 | |
| 118 | |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void deleteSwitch(String dpid) { |
| 123 | // TODO Setting inactive but we need to eventually remove data |
| 124 | setStatus(dpid, SwitchState.INACTIVE); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void deletePort(String dpid, short port) { |
| 130 | // TODO Auto-generated method stub |
| 131 | Vertex sw; |
| 132 | try { |
| 133 | if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
| 134 | // TODO: Check if port exists |
| 135 | Vertex p = sw.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
| 136 | graph.removeVertex(p); |
| 137 | graph.stopTransaction(Conclusion.SUCCESS); |
| 138 | } |
| 139 | } catch (TitanException e) { |
| 140 | // TODO: handle exceptions |
| 141 | } |
| 142 | |
| 143 | |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void deletePort(String dpid, String portName) { |
| 148 | // TODO Auto-generated method stub |
| 149 | |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public List<String> getActiveSwitches() { |
| 154 | // TODO Auto-generated method stub |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void init(String conf) { |
| 160 | graph = TitanFactory.open(conf); |
| 161 | graph.createKeyIndex("dpid", Vertex.class); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | } |