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