Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 18 | import java.util.Collection; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 19 | import java.util.HashMap; |
| 20 | import java.util.HashSet; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 21 | import java.util.List; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 22 | import java.util.Map; |
| 23 | import java.util.Map.Entry; |
| 24 | import java.util.Set; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.flow.DefaultFlowRule; |
| 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.FlowRule; |
| 41 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 42 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 43 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.intent.IntentExtensionService; |
| 47 | import org.onosproject.net.intent.IntentInstaller; |
| 48 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 49 | import org.onosproject.net.intent.PathIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 50 | |
| 51 | import com.google.common.collect.Lists; |
| 52 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 53 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 55 | * segment intents. |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 56 | */ |
| 57 | @Component(immediate = true) |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 58 | public class LinkCollectionIntentInstaller |
| 59 | implements IntentInstaller<LinkCollectionIntent> { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IntentExtensionService intentManager; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 65 | protected CoreService coreService; |
| 66 | |
| 67 | private ApplicationId appId; |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 71 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 72 | intentManager.registerInstaller(LinkCollectionIntent.class, this); |
| 73 | } |
| 74 | |
| 75 | @Deactivate |
| 76 | public void deactivate() { |
| 77 | intentManager.unregisterInstaller(PathIntent.class); |
| 78 | } |
| 79 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 80 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 81 | public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 82 | Map<DeviceId, Set<PortNumber>> inputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 83 | Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 84 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 85 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 86 | for (Link link : intent.links()) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 87 | if (inputMap.get(link.dst().deviceId()) == null) { |
| 88 | inputMap.put(link.dst().deviceId(), new HashSet<PortNumber>()); |
| 89 | } |
| 90 | inputMap.get(link.dst().deviceId()).add(link.dst().port()); |
| 91 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 92 | if (outputMap.get(link.src().deviceId()) == null) { |
| 93 | outputMap.put(link.src().deviceId(), new HashSet<PortNumber>()); |
| 94 | } |
| 95 | outputMap.get(link.src().deviceId()).add(link.src().port()); |
| 96 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 99 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 100 | if (inputMap.get(ingressPoint.deviceId()) == null) { |
| 101 | inputMap |
| 102 | .put(ingressPoint.deviceId(), new HashSet<PortNumber>()); |
| 103 | } |
| 104 | inputMap.get(ingressPoint.deviceId()).add(ingressPoint.port()); |
| 105 | |
| 106 | } |
| 107 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 108 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 109 | if (outputMap.get(egressPoint.deviceId()) == null) { |
| 110 | outputMap |
| 111 | .put(egressPoint.deviceId(), new HashSet<PortNumber>()); |
| 112 | } |
| 113 | outputMap.get(egressPoint.deviceId()).add(egressPoint.port()); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 118 | Set<PortNumber> ingressPoints = inputMap.get(entry.getKey()); |
| 119 | rules.addAll(createBatchEntries(FlowRuleOperation.ADD, intent, |
| 120 | entry.getKey(), |
| 121 | ingressPoints, |
| 122 | entry.getValue())); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 123 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 124 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 125 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 129 | public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 130 | Map<DeviceId, Set<PortNumber>> inputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 131 | Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 132 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 133 | |
| 134 | for (Link link : intent.links()) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 135 | if (inputMap.get(link.dst().deviceId()) == null) { |
| 136 | inputMap.put(link.dst().deviceId(), new HashSet<PortNumber>()); |
| 137 | } |
| 138 | inputMap.get(link.dst().deviceId()).add(link.dst().port()); |
| 139 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 140 | if (outputMap.get(link.src().deviceId()) == null) { |
| 141 | outputMap.put(link.src().deviceId(), new HashSet<PortNumber>()); |
| 142 | } |
| 143 | outputMap.get(link.src().deviceId()).add(link.src().port()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 144 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 145 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 146 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 147 | if (inputMap.get(ingressPoint.deviceId()) == null) { |
| 148 | inputMap |
| 149 | .put(ingressPoint.deviceId(), new HashSet<PortNumber>()); |
| 150 | } |
| 151 | inputMap.get(ingressPoint.deviceId()).add(ingressPoint.port()); |
| 152 | |
| 153 | } |
| 154 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 155 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 156 | if (outputMap.get(egressPoint.deviceId()) == null) { |
| 157 | outputMap |
| 158 | .put(egressPoint.deviceId(), new HashSet<PortNumber>()); |
| 159 | } |
| 160 | outputMap.get(egressPoint.deviceId()).add(egressPoint.port()); |
| 161 | } |
| 162 | |
| 163 | for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 164 | Set<PortNumber> ingressPoints = inputMap.get(entry.getKey()); |
| 165 | rules.addAll(createBatchEntries(FlowRuleOperation.REMOVE, intent, |
| 166 | entry.getKey(), |
| 167 | ingressPoints, |
| 168 | entry.getValue())); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 169 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 170 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 171 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 172 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 173 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 174 | @Override |
Brian O'Connor | 812e868 | 2015-02-19 13:45:13 -0800 | [diff] [blame] | 175 | public List<FlowRuleBatchOperation> replace(LinkCollectionIntent oldIntent, |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 176 | LinkCollectionIntent newIntent) { |
Brian O'Connor | 812e868 | 2015-02-19 13:45:13 -0800 | [diff] [blame] | 177 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 178 | batches.addAll(uninstall(oldIntent)); |
| 179 | batches.addAll(install(newIntent)); |
| 180 | return batches; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 183 | /** |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 184 | * Creates a collection of FlowRuleBatchEntry based on the provided |
| 185 | * parameters. |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 186 | * |
| 187 | * @param operation the FlowRuleOperation to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 188 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 189 | * @param deviceId the device ID for the flow rule |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 190 | * @param inPorts the logical input ports of the flow rule |
| 191 | * @param outPorts the output ports of the flow rule |
| 192 | * @return a collection with the new flow rule batch entries |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 193 | */ |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 194 | private Collection<FlowRuleBatchEntry> createBatchEntries( |
| 195 | FlowRuleOperation operation, |
| 196 | LinkCollectionIntent intent, |
| 197 | DeviceId deviceId, |
| 198 | Set<PortNumber> inPorts, |
| 199 | Set<PortNumber> outPorts) { |
| 200 | Collection<FlowRuleBatchEntry> result = Lists.newLinkedList(); |
| 201 | Set<PortNumber> ingressPorts = new HashSet<PortNumber>(); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 202 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 203 | // |
| 204 | // Collect all ingress ports for this device. |
| 205 | // The intent treatment is applied only on those ports. |
| 206 | // |
| 207 | for (ConnectPoint cp : intent.ingressPoints()) { |
| 208 | if (cp.deviceId().equals(deviceId)) { |
| 209 | ingressPorts.add(cp.port()); |
| 210 | } |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 211 | } |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 212 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 213 | // |
| 214 | // Create two treatments: one for setting the output ports, |
| 215 | // and a second one that applies the intent treatment and sets the |
| 216 | // output ports. |
| 217 | // NOTE: The second one is created only if there are ingress ports. |
| 218 | // |
| 219 | TrafficTreatment.Builder defaultTreatmentBuilder = |
| 220 | DefaultTrafficTreatment.builder(); |
| 221 | for (PortNumber outPort : outPorts) { |
| 222 | defaultTreatmentBuilder.setOutput(outPort); |
| 223 | } |
| 224 | TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build(); |
| 225 | TrafficTreatment intentTreatment = null; |
| 226 | if (!ingressPorts.isEmpty()) { |
| 227 | TrafficTreatment.Builder intentTreatmentBuilder = |
| 228 | DefaultTrafficTreatment.builder(intent.treatment()); |
| 229 | for (PortNumber outPort : outPorts) { |
| 230 | intentTreatmentBuilder.setOutput(outPort); |
| 231 | } |
| 232 | intentTreatment = intentTreatmentBuilder.build(); |
| 233 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 234 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 235 | for (PortNumber inPort : inPorts) { |
| 236 | TrafficSelector selector = DefaultTrafficSelector |
| 237 | .builder(intent.selector()).matchInport(inPort).build(); |
| 238 | TrafficTreatment treatment = defaultTreatment; |
| 239 | if (ingressPorts.contains(inPort)) { |
| 240 | // Use the intent treatment if this is ingress port |
| 241 | treatment = intentTreatment; |
| 242 | } |
| 243 | FlowRule rule = new DefaultFlowRule(deviceId, |
Brian O'Connor | 392619e | 2014-11-20 12:06:33 -0800 | [diff] [blame] | 244 | selector, treatment, 123, |
| 245 | appId, (short) (intent.id().fingerprint() & 0xffff), 0, true); |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 246 | result.add(new FlowRuleBatchEntry(operation, rule)); |
| 247 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 248 | |
Pavlin Radoslavov | 1b78723 | 2015-02-23 15:07:31 -0800 | [diff] [blame] | 249 | return result; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 250 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 251 | } |