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 | |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.util.MACAddress; |
| 16 | import net.onrc.onos.intent.FlowEntry; |
| 17 | import net.onrc.onos.intent.Intent; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 18 | import net.onrc.onos.intent.IntentOperation; |
| 19 | import net.onrc.onos.intent.IntentOperation.Operator; |
Toshio Koide | 103ccb2 | 2014-02-18 21:51:25 -0800 | [diff] [blame] | 20 | import net.onrc.onos.intent.IntentOperationList; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 21 | import net.onrc.onos.intent.PathIntent; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 22 | import net.onrc.onos.intent.ShortestPathIntent; |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 24 | //import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 27 | * |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 28 | * @author Brian O'Connor <bocon@onlab.us> |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | public class PlanCalcRuntime { |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 33 | |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 34 | // NetworkGraph graph; |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 35 | private final static Logger log = LoggerFactory.getLogger(PlanCalcRuntime.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 36 | |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 37 | public PlanCalcRuntime(/*NetworkGraph graph*/) { |
| 38 | // this.graph = graph; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | public List<Set<FlowEntry>> computePlan(IntentOperationList intentOps) { |
Brian O'Connor | c366e87 | 2014-02-24 14:08:31 -0800 | [diff] [blame] | 42 | long start = System.nanoTime(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 43 | Set<Collection<FlowEntry>> flowEntries = computeFlowEntries(intentOps); |
Brian O'Connor | c366e87 | 2014-02-24 14:08:31 -0800 | [diff] [blame] | 44 | long step1 = System.nanoTime(); |
| 45 | List<Set<FlowEntry>> plan = buildPhases(flowEntries); |
| 46 | long step2 = System.nanoTime(); |
| 47 | log.error("MEASUREMENT: Compute flow entries: {} ns, Build phases: {} ns", |
| 48 | (step1 - start), (step2 - step1)); |
| 49 | return plan; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private Set<Collection<FlowEntry>> computeFlowEntries(IntentOperationList intentOps) { |
| 53 | Set<Collection<FlowEntry>> flowEntries = new HashSet<>(); |
| 54 | for(IntentOperation i : intentOps) { |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 55 | if(!(i.intent instanceof PathIntent)) { |
| 56 | log.warn("Not a path intent: {}", i); |
| 57 | continue; |
| 58 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 59 | PathIntent intent = (PathIntent) i.intent; |
| 60 | Intent parent = intent.getParentIntent(); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 61 | long srcPort, dstPort; |
| 62 | long lastDstSw = -1, lastDstPort = -1; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 63 | MACAddress srcMac, dstMac; |
| 64 | if(parent instanceof ShortestPathIntent) { |
| 65 | ShortestPathIntent pathIntent = (ShortestPathIntent) parent; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 66 | // Switch srcSwitch = graph.getSwitch(pathIntent.getSrcSwitchDpid()); |
| 67 | // srcPort = srcSwitch.getPort(pathIntent.getSrcPortNumber()); |
| 68 | srcPort = pathIntent.getSrcPortNumber(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 69 | srcMac = MACAddress.valueOf(pathIntent.getSrcMac()); |
| 70 | dstMac = MACAddress.valueOf(pathIntent.getDstMac()); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 71 | // Switch dstSwitch = graph.getSwitch(pathIntent.getDstSwitchDpid()); |
| 72 | lastDstSw = pathIntent.getDstSwitchDpid(); |
| 73 | // lastDstPort = dstSwitch.getPort(pathIntent.getDstPortNumber()); |
| 74 | lastDstPort = pathIntent.getDstPortNumber(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 75 | } |
| 76 | else { |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 77 | log.warn("Unsupported Intent: {}", parent); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 78 | continue; |
| 79 | } |
| 80 | List<FlowEntry> entries = new ArrayList<>(); |
| 81 | for(LinkEvent linkEvent : intent.getPath()) { |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 82 | // Link link = graph.getLink(linkEvent.getSrc().getDpid(), |
| 83 | // linkEvent.getSrc().getNumber(), |
| 84 | // linkEvent.getDst().getDpid(), |
| 85 | // linkEvent.getDst().getNumber()); |
| 86 | // Switch sw = link.getSrcSwitch(); |
| 87 | long sw = linkEvent.getSrc().getDpid(); |
| 88 | // dstPort = link.getSrcPort(); |
| 89 | dstPort = linkEvent.getSrc().getNumber(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 90 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac, i.operator); |
| 91 | entries.add(fe); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 92 | // srcPort = link.getDstPort(); |
| 93 | srcPort = linkEvent.getDst().getNumber(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 94 | } |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 95 | if(lastDstSw >= 0 && lastDstPort >= 0) { |
| 96 | //Switch sw = lastDstPort.getSwitch(); |
| 97 | long sw = lastDstSw; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 98 | dstPort = lastDstPort; |
| 99 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac, i.operator); |
| 100 | entries.add(fe); |
| 101 | } |
| 102 | // install flow entries in reverse order |
| 103 | Collections.reverse(entries); |
| 104 | flowEntries.add(entries); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 105 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 106 | return flowEntries; |
| 107 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 108 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 109 | private List<Set<FlowEntry>> buildPhases(Set<Collection<FlowEntry>> flowEntries) { |
| 110 | Map<FlowEntry, Integer> map = new HashMap<>(); |
| 111 | List<Set<FlowEntry>> plan = new ArrayList<>(); |
| 112 | for(Collection<FlowEntry> c : flowEntries) { |
| 113 | for(FlowEntry e : c) { |
| 114 | Integer i = map.get(e); |
| 115 | if(i == null) { |
| 116 | i = Integer.valueOf(0); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 117 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 118 | switch(e.getOperator()) { |
| 119 | case ADD: |
| 120 | i += 1; |
| 121 | break; |
| 122 | case REMOVE: |
| 123 | i -= 1; |
| 124 | break; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 125 | default: |
| 126 | break; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 127 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 128 | map.put(e, i); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 129 | // System.out.println(e + " " + e.getOperator()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 130 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 131 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 132 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 133 | // really simple first iteration of plan |
| 134 | //TODO: optimize the map in phases |
| 135 | Set<FlowEntry> phase = new HashSet<>(); |
| 136 | for(FlowEntry e : map.keySet()) { |
| 137 | Integer i = map.get(e); |
| 138 | if(i == 0) { |
| 139 | continue; |
| 140 | } |
| 141 | else if(i > 0) { |
| 142 | e.setOperator(Operator.ADD); |
| 143 | } |
| 144 | else if(i < 0) { |
| 145 | e.setOperator(Operator.REMOVE); |
| 146 | } |
| 147 | phase.add(e); |
| 148 | } |
| 149 | plan.add(phase); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 150 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 151 | return plan; |
| 152 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 153 | } |