Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 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; |
Brian O'Connor | 8a52cdd | 2014-02-25 15:35:25 -0800 | [diff] [blame] | 8 | import java.util.LinkedList; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.Set; |
| 12 | |
| 13 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.intent.FlowEntry; |
| 15 | import net.onrc.onos.core.intent.Intent; |
| 16 | import net.onrc.onos.core.intent.IntentOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.intent.IntentOperationList; |
| 19 | import net.onrc.onos.core.intent.PathIntent; |
| 20 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.topology.LinkEvent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 22 | |
Brian O'Connor | 8a52cdd | 2014-02-25 15:35:25 -0800 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 26 | /** |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 27 | * The PlanCalcRuntime class receives a list of intents and operations (IntentOperationList), |
| 28 | * and does the following: |
| 29 | * <ul> |
| 30 | * <li> Convert the Intent into FlowEntries. |
| 31 | * <li> Determine the dependency between FlowEntries. |
| 32 | * <li> Create sets of FlowEntries to be installed by the OpenFlow protocol. |
| 33 | * </ul> |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | public class PlanCalcRuntime { |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 37 | |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 38 | private static final Logger log = LoggerFactory.getLogger(PlanCalcRuntime.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 39 | |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 40 | /** |
| 41 | * The method produces a plan from a list of IntentOperations. |
| 42 | * <p> |
| 43 | * In this context, a plan is a list of sets of flow entries. Flow entries within |
| 44 | * a set can be installed in parallel. Each set of flow entries is dependent on |
| 45 | * all previous sets in the list. |
| 46 | * |
| 47 | * @param intentOps list of IntentOperations (Intent + Operation) for plan computation |
| 48 | * @return plan (list of set of flow entries) |
| 49 | */ |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 50 | public List<Set<FlowEntry>> computePlan(IntentOperationList intentOps) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | long start = System.nanoTime(); |
| 52 | List<Collection<FlowEntry>> flowEntries = computeFlowEntries(intentOps); |
| 53 | long step1 = System.nanoTime(); |
| 54 | List<Set<FlowEntry>> plan = buildPhases(flowEntries); |
| 55 | long step2 = System.nanoTime(); |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 56 | log.debug("MEASUREMENT: Compute flow entries: {} ns, Build phases: {} ns", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | (step1 - start), (step2 - step1)); |
| 58 | return plan; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Converts PathIntents to FlowEntries so they can be installed. |
| 63 | * <p> |
| 64 | * Note: This method only supports ShortestPathIntents at the moment. |
| 65 | * |
| 66 | * @param intentOps list of IntentOperations |
| 67 | * @return a list of FlowEntry objects for each Intent in intentOps |
| 68 | */ |
Brian O'Connor | 8a52cdd | 2014-02-25 15:35:25 -0800 | [diff] [blame] | 69 | private List<Collection<FlowEntry>> computeFlowEntries(IntentOperationList intentOps) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | List<Collection<FlowEntry>> flowEntries = new LinkedList<>(); |
| 71 | for (IntentOperation i : intentOps) { |
| 72 | if (!(i.intent instanceof PathIntent)) { |
| 73 | log.warn("Not a path intent: {}", i); |
| 74 | continue; |
| 75 | } |
| 76 | PathIntent intent = (PathIntent) i.intent; |
| 77 | Intent parent = intent.getParentIntent(); |
| 78 | long srcPort, dstPort; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 79 | long lastDstSw = -1, lastDstPort = -1, firstSrcSw = -1; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | MACAddress srcMac, dstMac; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 81 | int idleTimeout = 0, hardTimeout = 0, firstSwitchIdleTimeout = 0, firstSwitchHardTimeout = 0; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 82 | Long cookieId = null; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 83 | int srcIP, dstIP; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | if (parent instanceof ShortestPathIntent) { |
| 85 | ShortestPathIntent pathIntent = (ShortestPathIntent) parent; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | srcPort = pathIntent.getSrcPortNumber(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 87 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 88 | if (pathIntent.getSrcMac() != ShortestPathIntent.EMPTYMACADDRESS) { |
| 89 | srcMac = MACAddress.valueOf(pathIntent.getSrcMac()); |
| 90 | } else { |
| 91 | srcMac = null; |
| 92 | } |
| 93 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 94 | if (pathIntent.getDstMac() != ShortestPathIntent.EMPTYMACADDRESS) { |
Jonathan Hart | 20c801c | 2014-06-02 11:11:07 -0700 | [diff] [blame] | 95 | dstMac = MACAddress.valueOf(pathIntent.getDstMac()); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 96 | } else { |
| 97 | dstMac = null; |
| 98 | } |
| 99 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 100 | srcIP = pathIntent.getSrcIp(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 101 | dstIP = pathIntent.getDstIp(); |
| 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | lastDstSw = pathIntent.getDstSwitchDpid(); |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 104 | firstSrcSw = pathIntent.getSrcSwitchDpid(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | lastDstPort = pathIntent.getDstPortNumber(); |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 106 | idleTimeout = pathIntent.getIdleTimeout(); |
| 107 | hardTimeout = pathIntent.getHardTimeout(); |
| 108 | firstSwitchIdleTimeout = pathIntent.getFirstSwitchIdleTimeout(); |
Jonathan Hart | 33e1f30 | 2014-05-30 14:37:09 -0700 | [diff] [blame] | 109 | firstSwitchHardTimeout = pathIntent.getFirstSwitchHardTimeout(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 110 | try { |
| 111 | cookieId = Long.valueOf(pathIntent.getId()); |
| 112 | } catch (NumberFormatException e) { |
| 113 | log.trace("NumberFormatException : ", e); |
| 114 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | } else { |
| 116 | log.warn("Unsupported Intent: {}", parent); |
| 117 | continue; |
| 118 | } |
| 119 | List<FlowEntry> entries = new ArrayList<>(); |
| 120 | for (LinkEvent linkEvent : intent.getPath()) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 121 | long sw = linkEvent.getSrc().getDpid().value(); |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 122 | dstPort = linkEvent.getSrc().getPortNumber().value(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 123 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac, |
| 124 | srcIP, dstIP, i.operator); |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 125 | if (sw != firstSrcSw) { |
| 126 | fe.setIdleTimeout(idleTimeout); |
| 127 | fe.setHardTimeout(hardTimeout); |
| 128 | } else { |
| 129 | fe.setIdleTimeout(firstSwitchIdleTimeout); |
| 130 | fe.setHardTimeout(firstSwitchHardTimeout); |
| 131 | } |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 132 | if (cookieId != null) { |
| 133 | log.trace("cookieId is set: {}", cookieId); |
| 134 | fe.setFlowEntryId(cookieId); |
| 135 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | entries.add(fe); |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 137 | srcPort = linkEvent.getDst().getPortNumber().value(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | } |
| 139 | if (lastDstSw >= 0 && lastDstPort >= 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | long sw = lastDstSw; |
| 141 | dstPort = lastDstPort; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 142 | FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac, |
| 143 | srcIP, dstIP, i.operator); |
TeruU | e3bbe2f | 2014-05-28 16:11:30 -0700 | [diff] [blame] | 144 | if (cookieId != null) { |
| 145 | log.trace("cookieId is set: {}", cookieId); |
| 146 | fe.setFlowEntryId(cookieId); |
| 147 | } |
TeruU | e5d0bf1 | 2014-05-28 17:50:02 -0700 | [diff] [blame] | 148 | if (sw != firstSrcSw) { |
| 149 | fe.setIdleTimeout(idleTimeout); |
| 150 | fe.setHardTimeout(hardTimeout); |
| 151 | } else { |
| 152 | fe.setIdleTimeout(firstSwitchIdleTimeout); |
| 153 | fe.setHardTimeout(firstSwitchHardTimeout); |
| 154 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | entries.add(fe); |
| 156 | } |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 157 | // reverse order of flow entries so they are installed backwards |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | Collections.reverse(entries); |
| 159 | flowEntries.add(entries); |
| 160 | } |
| 161 | return flowEntries; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 162 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 163 | |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 164 | /** |
| 165 | * TODO |
| 166 | * |
| 167 | * Note: This method is for a testing purpose. Please leave it right now. |
| 168 | * |
| 169 | * @param flowEntries |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 170 | * @return a list of phases, where each phase is a set of FlowEntries |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 171 | */ |
| 172 | @SuppressWarnings("unused") |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 173 | private List<Set<FlowEntry>> simpleBuildPhases(List<Collection<FlowEntry>> flowEntries) { |
| 174 | List<Set<FlowEntry>> plan = new ArrayList<>(); |
| 175 | Set<FlowEntry> phase = new HashSet<>(); |
| 176 | for (Collection<FlowEntry> c : flowEntries) { |
| 177 | phase.addAll(c); |
| 178 | } |
| 179 | plan.add(phase); |
| 180 | |
| 181 | return plan; |
| 182 | } |
| 183 | |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Merges the lists generated by computeFlowEntries() into install phases. |
| 186 | * <p> |
| 187 | * This function will also remove duplicate entries. |
| 188 | * |
| 189 | * @param flowEntries list of lists of flowEntries |
| 190 | * @return a list of sets of FlowEntries to be installed |
| 191 | */ |
Brian O'Connor | 8a52cdd | 2014-02-25 15:35:25 -0800 | [diff] [blame] | 192 | private List<Set<FlowEntry>> buildPhases(List<Collection<FlowEntry>> flowEntries) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | Map<FlowEntry, Integer> map = new HashMap<>(); |
| 194 | List<Set<FlowEntry>> plan = new ArrayList<>(); |
Brian O'Connor | 762aca3 | 2014-06-12 14:18:23 -0700 | [diff] [blame] | 195 | // merge equal FlowEntries |
| 196 | //TODO: explore ways to merge FlowEntries that contain the same match condition |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | for (Collection<FlowEntry> c : flowEntries) { |
| 198 | for (FlowEntry e : c) { |
| 199 | Integer i = map.get(e); |
| 200 | if (i == null) { |
| 201 | i = Integer.valueOf(0); |
| 202 | } |
| 203 | switch (e.getOperator()) { |
| 204 | case ADD: |
| 205 | i += 1; |
| 206 | break; |
| 207 | case REMOVE: |
| 208 | i -= 1; |
| 209 | break; |
| 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | map.put(e, i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 216 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 217 | // really simple first iteration of plan |
| 218 | //TODO: optimize the map in phases |
| 219 | Set<FlowEntry> phase = new HashSet<>(); |
Pavlin Radoslavov | 45a0ad3 | 2014-04-10 12:16:40 -0700 | [diff] [blame] | 220 | for (Map.Entry<FlowEntry, Integer> entry : map.entrySet()) { |
| 221 | FlowEntry e = entry.getKey(); |
| 222 | Integer i = entry.getValue(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 223 | if (i == 0) { |
| 224 | continue; |
| 225 | } else if (i > 0) { |
| 226 | e.setOperator(Operator.ADD); |
| 227 | } else if (i < 0) { |
| 228 | e.setOperator(Operator.REMOVE); |
| 229 | } |
| 230 | phase.add(e); |
| 231 | } |
| 232 | plan.add(phase); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 233 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 234 | return plan; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 235 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 236 | } |