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