Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 1 | package net.onrc.onos.flow; |
| 2 | |
| 3 | import java.util.ArrayList; |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 4 | import java.util.Collections; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.LinkedList; |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 8 | import java.util.List; |
| 9 | import java.util.Map; |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 10 | import java.util.Queue; |
| 11 | import java.util.Set; |
| 12 | |
| 13 | import org.openflow.util.HexString; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | import com.tinkerpop.blueprints.Direction; |
| 18 | import com.tinkerpop.blueprints.Vertex; |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 19 | |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IOFSwitch; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 21 | import net.onrc.onos.graph.GraphDBOperation; |
| 22 | import net.onrc.onos.graph.LocalTopologyEventListener; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 24 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 25 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 26 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 28 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 29 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 30 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 33 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 34 | import net.onrc.onos.ofcontroller.util.Port; |
| 35 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 36 | |
| 37 | public class FlowManagerImpl implements IFlowManager { |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 38 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame^] | 39 | protected final static Logger log = LoggerFactory.getLogger(LocalTopologyEventListener.class); |
Toshio Koide | 57b99b5 | 2013-06-13 14:09:23 -0700 | [diff] [blame] | 40 | protected GraphDBOperation op; |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
| 43 | public void createFlow(IPortObject src_port, IPortObject dest_port) { |
| 44 | // TODO Auto-generated method stub |
| 45 | |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public Iterable<FlowPath> getFlows(IPortObject src_port, |
| 50 | IPortObject dest_port) { |
| 51 | // TODO Auto-generated method stub |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Iterable<FlowPath> getOutFlows(IPortObject port) { |
| 57 | // TODO Auto-generated method stub |
| 58 | List<FlowPath> flowPaths = new ArrayList<FlowPath> (); |
| 59 | Iterable<IFlowEntry> flowEntries = port.getOutFlowEntries(); |
| 60 | |
| 61 | for(IFlowEntry fe: flowEntries) { |
Pankaj Berde | b4bdf70 | 2013-03-28 13:37:24 -0700 | [diff] [blame] | 62 | IFlowPath flow = fe.getFlow(); |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 63 | FlowPath flowPath = new FlowPath(flow); |
| 64 | flowPaths.add(flowPath); |
| 65 | } |
| 66 | return flowPaths; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void reconcileFlows(IPortObject src_port) { |
| 71 | // TODO Auto-generated method stub |
| 72 | |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 73 | log.debug("Reconcile Flows for Port removed: {}:{}",src_port.getSwitch().getDPID(),src_port.getNumber()); |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 74 | Iterable<IFlowEntry> flowEntries = src_port.getOutFlowEntries(); |
| 75 | |
| 76 | for(IFlowEntry fe: flowEntries) { |
| 77 | IFlowPath flow = fe.getFlow(); |
| 78 | reconcileFlow(flow); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private void reconcileFlow(IFlowPath flow) { |
| 83 | // TODO Auto-generated method stub |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 84 | String src_dpid = flow.getSrcSwitch(); |
| 85 | String dst_dpid = flow.getDstSwitch(); |
| 86 | Short src_port = flow.getSrcPort(); |
| 87 | Short dst_port = flow.getDstPort(); |
| 88 | IPortObject src = null; |
| 89 | IPortObject dst = null; |
Toshio Koide | 57b99b5 | 2013-06-13 14:09:23 -0700 | [diff] [blame] | 90 | src = op.searchPort(src_dpid, src_port); |
| 91 | dst = op.searchPort(dst_dpid, dst_port); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 92 | if (src != null && dst != null) { |
| 93 | FlowPath newFlow = this.computeFlowPath(src,dst); |
| 94 | installFlow(newFlow); |
| 95 | removeFlow(flow); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | private void removeFlow(IFlowPath flow) { |
| 101 | // TODO Auto-generated method stub |
| 102 | |
| 103 | } |
| 104 | |
| 105 | private void installFlow(FlowPath newFlow) { |
| 106 | // TODO Auto-generated method stub |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void reconcileFlow(IPortObject src_port, IPortObject dest_port) { |
| 112 | // TODO Auto-generated method stub |
| 113 | |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public FlowPath computeFlowPath(IPortObject src_port, IPortObject dest_port) { |
| 118 | // TODO Auto-generated method stub |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 119 | DataPath dataPath = new DataPath(); |
| 120 | |
| 121 | // FIXME: Bad idea to use FloodLight data structures (SwitchPort) |
| 122 | |
| 123 | dataPath.setSrcPort(new SwitchPort(new Dpid(src_port.getSwitch().getDPID()), |
| 124 | new Port(src_port.getNumber()))); |
| 125 | dataPath.setDstPort(new SwitchPort(new Dpid(src_port.getSwitch().getDPID()), |
| 126 | new Port(src_port.getNumber()))); |
| 127 | |
| 128 | if (src_port.getSwitch().equals(dest_port.getSwitch())) { |
| 129 | // on same switch create quick path |
| 130 | FlowEntry flowEntry = new FlowEntry(); |
| 131 | flowEntry.setDpid(new Dpid(src_port.getSwitch().getDPID())); |
| 132 | flowEntry.setInPort(new Port(src_port.getNumber())); |
| 133 | flowEntry.setOutPort(new Port(src_port.getNumber())); |
| 134 | flowEntry.setFlowEntryMatch(new FlowEntryMatch()); |
| 135 | flowEntry.flowEntryMatch().enableInPort(flowEntry.inPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 136 | |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 137 | // Set the outgoing port output action |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 138 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 139 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 140 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 141 | flowEntryActions.addAction(flowEntryAction); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 142 | dataPath.flowEntries().add(flowEntry); |
| 143 | |
| 144 | FlowPath flowPath = new FlowPath(); |
| 145 | flowPath.setDataPath(dataPath); |
| 146 | |
| 147 | return flowPath; |
| 148 | } |
| 149 | Vertex v_src = src_port.getSwitch().asVertex(); |
| 150 | Vertex v_dest = dest_port.getSwitch().asVertex(); |
| 151 | |
| 152 | // |
| 153 | // Implement the Shortest Path computation by using Breath First Search |
| 154 | // |
| 155 | Set<Vertex> visitedSet = new HashSet<Vertex>(); |
| 156 | Queue<Vertex> processingList = new LinkedList<Vertex>(); |
| 157 | Map<Vertex, Vertex> previousVertexMap = new HashMap<Vertex, Vertex>(); |
| 158 | |
| 159 | processingList.add(v_src); |
| 160 | visitedSet.add(v_src); |
| 161 | Boolean path_found = false; |
| 162 | while (! processingList.isEmpty()) { |
| 163 | Vertex nextVertex = processingList.poll(); |
| 164 | if (v_dest.equals(nextVertex)) { |
| 165 | path_found = true; |
| 166 | break; |
| 167 | } |
| 168 | for (Vertex parentPort : nextVertex.getVertices(Direction.OUT, "on")) { |
| 169 | for (Vertex childPort : parentPort.getVertices(Direction.OUT, "link")) { |
| 170 | for (Vertex child : childPort.getVertices(Direction.IN, "on")) { |
| 171 | // Ignore inactive switches |
| 172 | String state = child.getProperty("state").toString(); |
| 173 | if (! state.equals(SwitchState.ACTIVE.toString())) |
| 174 | continue; |
| 175 | |
| 176 | if (! visitedSet.contains(child)) { |
| 177 | previousVertexMap.put(parentPort, nextVertex); |
| 178 | previousVertexMap.put(childPort, parentPort); |
| 179 | previousVertexMap.put(child, childPort); |
| 180 | visitedSet.add(child); |
| 181 | processingList.add(child); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | if (! path_found) { |
| 188 | return null; // No path found |
| 189 | } |
| 190 | |
| 191 | List<Vertex> resultPath = new LinkedList<Vertex>(); |
| 192 | Vertex previousVertex = v_dest; |
| 193 | resultPath.add(v_dest); |
| 194 | while (! v_src.equals(previousVertex)) { |
| 195 | Vertex currentVertex = previousVertexMap.get(previousVertex); |
| 196 | resultPath.add(currentVertex); |
| 197 | previousVertex = currentVertex; |
| 198 | } |
| 199 | Collections.reverse(resultPath); |
| 200 | |
| 201 | // Loop through the result and prepare the return result |
| 202 | // as a list of Flow Entries. |
| 203 | // |
| 204 | long nodeId = 0; |
| 205 | short portId = 0; |
| 206 | Port inPort = new Port(src_port.getNumber()); |
| 207 | Port outPort = new Port(); |
| 208 | int idx = 0; |
| 209 | for (Vertex v: resultPath) { |
| 210 | String type = v.getProperty("type").toString(); |
| 211 | // System.out.println("type: " + type); |
| 212 | if (type.equals("port")) { |
Yuta HIGUCHI | 6f0e439 | 2013-10-09 17:43:34 -0700 | [diff] [blame] | 213 | //String number = v.getProperty("number").toString(); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 214 | // System.out.println("number: " + number); |
| 215 | |
| 216 | Object obj = v.getProperty("number"); |
| 217 | // String class_str = obj.getClass().toString(); |
| 218 | if (obj instanceof Short) { |
| 219 | portId = (Short)obj; |
| 220 | } else if (obj instanceof Integer) { |
| 221 | Integer int_nodeId = (Integer)obj; |
| 222 | portId = int_nodeId.shortValue(); |
| 223 | // int int_nodeId = (Integer)obj; |
| 224 | // portId = (short)int_nodeId.; |
| 225 | } |
| 226 | } else if (type.equals("switch")) { |
| 227 | String dpid = v.getProperty("dpid").toString(); |
| 228 | nodeId = HexString.toLong(dpid); |
| 229 | |
| 230 | // System.out.println("dpid: " + dpid); |
| 231 | } |
| 232 | idx++; |
| 233 | if (idx == 1) { |
| 234 | continue; |
| 235 | } |
| 236 | int mod = idx % 3; |
| 237 | if (mod == 0) { |
| 238 | // Setup the incoming port |
| 239 | inPort = new Port(portId); |
| 240 | continue; |
| 241 | } |
| 242 | if (mod == 2) { |
| 243 | // Setup the outgoing port, and add the Flow Entry |
| 244 | outPort = new Port(portId); |
| 245 | |
| 246 | FlowEntry flowEntry = new FlowEntry(); |
| 247 | flowEntry.setDpid(new Dpid(nodeId)); |
| 248 | flowEntry.setInPort(inPort); |
| 249 | flowEntry.setOutPort(outPort); |
| 250 | flowEntry.setFlowEntryMatch(new FlowEntryMatch()); |
| 251 | flowEntry.flowEntryMatch().enableInPort(flowEntry.inPort()); |
| 252 | |
| 253 | // Set the outgoing port output action |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 254 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 255 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 256 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 257 | flowEntryActions.addAction(flowEntryAction); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 258 | dataPath.flowEntries().add(flowEntry); |
| 259 | continue; |
| 260 | } |
| 261 | } |
| 262 | if (idx > 0) { |
| 263 | // Add the last Flow Entry |
| 264 | FlowEntry flowEntry = new FlowEntry(); |
| 265 | flowEntry.setDpid(new Dpid(nodeId)); |
| 266 | flowEntry.setInPort(inPort); |
| 267 | flowEntry.setOutPort(new Port(dest_port.getNumber())); |
| 268 | flowEntry.setFlowEntryMatch(new FlowEntryMatch()); |
| 269 | flowEntry.flowEntryMatch().enableInPort(flowEntry.inPort()); |
| 270 | |
| 271 | // Set the outgoing port output action |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 272 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 273 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 274 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 275 | flowEntryActions.addAction(flowEntryAction); |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 276 | dataPath.flowEntries().add(flowEntry); |
| 277 | dataPath.flowEntries().add(flowEntry); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | if (dataPath.flowEntries().size() > 0) { |
| 282 | FlowPath flowPath = new FlowPath(); |
| 283 | flowPath.setDataPath(dataPath); |
| 284 | |
| 285 | return flowPath; |
| 286 | } |
| 287 | return null; |
| 288 | |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public Iterable<FlowEntry> getFlowEntries(FlowPath flow) { |
| 293 | // TODO Auto-generated method stub |
| 294 | return null; |
| 295 | } |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 296 | |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 297 | |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 298 | @Override |
Pavlin Radoslavov | 2b8e622 | 2013-04-03 11:05:01 -0700 | [diff] [blame] | 299 | public boolean installRemoteFlowEntry(FlowPath flowPath, |
| 300 | FlowEntry entry) { |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 301 | // TODO Auto-generated method stub |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | @Override |
Pavlin Radoslavov | 2b8e622 | 2013-04-03 11:05:01 -0700 | [diff] [blame] | 306 | public boolean removeRemoteFlowEntry(FlowPath flowPath, |
| 307 | FlowEntry entry) { |
Pankaj Berde | 83d8338 | 2013-03-28 13:55:34 -0700 | [diff] [blame] | 308 | return false; |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 309 | // TODO Auto-generated method stub |
| 310 | |
| 311 | } |
| 312 | |
Pankaj Berde | b4bdf70 | 2013-03-28 13:37:24 -0700 | [diff] [blame] | 313 | @Override |
Pavlin Radoslavov | 2b8e622 | 2013-04-03 11:05:01 -0700 | [diff] [blame] | 314 | public boolean installFlowEntry(IOFSwitch mySwitch, |
| 315 | FlowPath flowPath, |
| 316 | FlowEntry flowEntry) { |
Pankaj Berde | b4bdf70 | 2013-03-28 13:37:24 -0700 | [diff] [blame] | 317 | // TODO Auto-generated method stub |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | @Override |
Pavlin Radoslavov | 2b8e622 | 2013-04-03 11:05:01 -0700 | [diff] [blame] | 322 | public boolean removeFlowEntry(IOFSwitch mySwitch, |
| 323 | FlowPath flowPath, |
| 324 | FlowEntry flowEntry) { |
Pankaj Berde | b4bdf70 | 2013-03-28 13:37:24 -0700 | [diff] [blame] | 325 | // TODO Auto-generated method stub |
| 326 | return false; |
| 327 | } |
Pankaj Berde | 37f037f | 2013-04-03 11:52:11 -0700 | [diff] [blame] | 328 | |
Pankaj Berde | b4bdf70 | 2013-03-28 13:37:24 -0700 | [diff] [blame] | 329 | |
Pankaj Berde | fc30d3f | 2013-03-28 13:34:44 -0700 | [diff] [blame] | 330 | } |