Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame^] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.HashSet; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 11 | |
| 12 | import net.floodlightcontroller.util.MACAddress; |
| 13 | import net.onrc.onos.intent.FlowEntry; |
| 14 | import net.onrc.onos.intent.Intent; |
| 15 | import net.onrc.onos.intent.PathIntent; |
| 16 | import net.onrc.onos.intent.PathIntents; |
| 17 | import net.onrc.onos.intent.ShortestPathIntent; |
| 18 | import net.onrc.onos.ofcontroller.networkgraph.Link; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 20 | import net.onrc.onos.ofcontroller.networkgraph.Port; |
| 21 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | * @author Brian O'Connor <bocon@onlab.us> |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | public class PlanCalcRuntime { |
| 30 | NetworkGraph graph; |
| 31 | protected PathIntents intents; |
| 32 | protected Set<Collection<FlowEntry>> flowEntries; |
| 33 | protected List<Set<FlowEntry>> plan; |
| 34 | |
| 35 | public PlanCalcRuntime(NetworkGraph graph) { |
| 36 | this.graph = graph; |
| 37 | this.flowEntries = new HashSet<>(); |
| 38 | this.plan = new ArrayList<>(); |
| 39 | } |
| 40 | |
| 41 | public void addIntents(PathIntents intents) { |
| 42 | this.intents = intents; |
| 43 | computeFlowEntries(); |
| 44 | constructPlan(); |
| 45 | } |
| 46 | |
| 47 | public List<Set<FlowEntry>> getPlan() { |
| 48 | return plan; |
| 49 | } |
| 50 | |
| 51 | public void computeFlowEntries() { |
| 52 | for(PathIntent intent : intents.getIntents()) { |
| 53 | Intent parent = intent.getParentIntent(); |
| 54 | Port srcPort, dstPort, lastDstPort = null; |
| 55 | MACAddress srcMac, dstMac; |
| 56 | if(parent instanceof ShortestPathIntent) { |
| 57 | ShortestPathIntent pathIntent = (ShortestPathIntent) parent; |
| 58 | Switch srcSwitch = graph.getSwitch(pathIntent.getSrcSwitchDpid()); |
| 59 | srcPort = srcSwitch.getPort(pathIntent.getSrcPortNumber()); |
| 60 | srcMac = MACAddress.valueOf(pathIntent.getSrcMac()); |
| 61 | dstMac = MACAddress.valueOf(pathIntent.getDstMac()); |
| 62 | Switch dstSwitch = graph.getSwitch(pathIntent.getDstSwitchDpid()); |
| 63 | lastDstPort = dstSwitch.getPort(pathIntent.getDstPortNumber()); |
| 64 | } |
| 65 | else { |
| 66 | // TODO: log this error |
| 67 | continue; |
| 68 | } |
| 69 | List<FlowEntry> entries = new ArrayList<>(); |
| 70 | for(Link link : intent.getPath(graph)) { |
| 71 | Switch sw = link.getSourceSwitch(); |
| 72 | dstPort = link.getSourcePort(); |
| 73 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac); |
| 74 | entries.add(fe); |
| 75 | srcPort = link.getDestinationPort(); |
| 76 | } |
| 77 | if(lastDstPort != null) { |
| 78 | Switch sw = lastDstPort.getSwitch(); |
| 79 | dstPort = lastDstPort; |
| 80 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac); |
| 81 | entries.add(fe); |
| 82 | } |
| 83 | // install flow entries in reverse order |
| 84 | Collections.reverse(entries); |
| 85 | flowEntries.add(entries); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public void constructPlan() { |
| 90 | Map<FlowEntry, Integer> map = new HashMap<>(); |
| 91 | for(Collection<FlowEntry> c : flowEntries) { |
| 92 | for(FlowEntry e: c) { |
| 93 | Integer i = map.get(e); |
| 94 | if(i == null) { |
| 95 | map.put(e, 1); |
| 96 | } |
| 97 | else { |
| 98 | i += 1; |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // really simple first iteration of plan |
| 105 | //TODO: optimize the map in phases |
| 106 | plan.add(map.keySet()); |
| 107 | } |
| 108 | } |