Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import com.google.common.collect.HashMultimap; |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.SetMultimap; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.onlab.util.Identifier; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.PortNumber; |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 35 | import org.onosproject.net.domain.DomainService; |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.flow.criteria.Criterion; |
| 40 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 41 | import org.onosproject.net.flow.instructions.Instruction; |
| 42 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 43 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 44 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 45 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 46 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
| 49 | import org.onosproject.net.flowobjective.Objective; |
| 50 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 51 | import org.onosproject.net.intent.Intent; |
| 52 | import org.onosproject.net.intent.IntentCompiler; |
| 53 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 54 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 55 | import org.onosproject.net.resource.ResourceService; |
| 56 | import org.onosproject.net.resource.impl.LabelAllocator; |
| 57 | |
| 58 | import java.util.ArrayList; |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
| 61 | import java.util.Optional; |
| 62 | import java.util.Set; |
| 63 | |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 64 | import static org.onosproject.net.domain.DomainId.LOCAL; |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 65 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
| 66 | |
| 67 | /** |
| 68 | * Compiler to produce flow objectives from link collections. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | public class LinkCollectionIntentObjectiveCompiler |
| 72 | extends LinkCollectionCompiler<Objective> |
| 73 | implements IntentCompiler<LinkCollectionIntent> { |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected IntentConfigurableRegistrator registrator; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected CoreService coreService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected FlowObjectiveService flowObjectiveService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected ResourceService resourceService; |
| 86 | |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected DomainService domainService; |
| 89 | |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 90 | private ApplicationId appId; |
| 91 | |
| 92 | @Activate |
| 93 | public void activate() { |
| 94 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 95 | registrator.registerCompiler(LinkCollectionIntent.class, this, true); |
| 96 | if (labelAllocator == null) { |
| 97 | labelAllocator = new LabelAllocator(resourceService); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | @Deactivate |
| 103 | public void deactivate() { |
| 104 | registrator.unregisterCompiler(LinkCollectionIntent.class, true); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
| 109 | |
| 110 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 111 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 112 | Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of(); |
| 113 | |
| 114 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
| 115 | |
| 116 | computePorts(intent, inputPorts, outputPorts); |
| 117 | |
| 118 | if (encapConstraint.isPresent()) { |
| 119 | labels = labelAllocator.assignLabelToPorts(intent.links(), |
| 120 | intent.id(), |
| 121 | encapConstraint.get().encapType()); |
| 122 | } |
| 123 | |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 124 | ImmutableList.Builder<Intent> intentList = ImmutableList.builder(); |
| 125 | if (this.isDomainProcessingEnabled(intent)) { |
| 126 | intentList.addAll(this.getDomainIntents(intent, domainService)); |
| 127 | } |
| 128 | |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 129 | List<Objective> objectives = new ArrayList<>(); |
| 130 | List<DeviceId> devices = new ArrayList<>(); |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 131 | for (DeviceId deviceId : outputPorts.keySet()) { |
| 132 | // add only objectives that are not inside of a domain |
| 133 | if (LOCAL.equals(domainService.getDomain(deviceId))) { |
| 134 | List<Objective> deviceObjectives = |
| 135 | createRules(intent, |
| 136 | deviceId, |
| 137 | inputPorts.get(deviceId), |
| 138 | outputPorts.get(deviceId), |
| 139 | labels); |
| 140 | deviceObjectives.forEach(objective -> { |
| 141 | objectives.add(objective); |
| 142 | devices.add(deviceId); |
| 143 | }); |
| 144 | } |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 145 | } |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame^] | 146 | // if any objectives have been created |
| 147 | if (!objectives.isEmpty()) { |
| 148 | intentList.add(new FlowObjectiveIntent(appId, intent.key(), devices, |
| 149 | objectives, |
| 150 | intent.resources(), |
| 151 | intent.resourceGroup())); |
| 152 | } |
| 153 | return intentList.build(); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 157 | boolean optimizeTreatments() { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | @Override |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 162 | protected List<Objective> createRules(LinkCollectionIntent intent, |
| 163 | DeviceId deviceId, |
| 164 | Set<PortNumber> inPorts, |
| 165 | Set<PortNumber> outPorts, |
| 166 | Map<ConnectPoint, Identifier<?>> labels) { |
| 167 | |
| 168 | List<Objective> objectives = new ArrayList<>(inPorts.size() * 2); |
| 169 | |
| 170 | /* |
| 171 | * Looking for the encapsulation constraint |
| 172 | */ |
| 173 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
| 174 | |
| 175 | inPorts.forEach(inport -> { |
| 176 | |
| 177 | ForwardingInstructions instructions = this.createForwardingInstruction( |
| 178 | encapConstraint, |
| 179 | intent, |
| 180 | inport, |
| 181 | outPorts, |
| 182 | deviceId, |
| 183 | labels |
| 184 | ); |
| 185 | |
| 186 | Set<TrafficTreatment> treatmentsWithDifferentPort = |
| 187 | Sets.newHashSet(); |
| 188 | |
| 189 | TrafficTreatment.Builder treatmentBuilder = |
| 190 | DefaultTrafficTreatment.builder(); |
| 191 | |
| 192 | for (Instruction inst : instructions.treatment().allInstructions()) { |
| 193 | if (inst.type() == OUTPUT) { |
| 194 | treatmentBuilder.add(inst); |
| 195 | treatmentsWithDifferentPort.add(treatmentBuilder.build()); |
| 196 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 197 | } else { |
| 198 | treatmentBuilder.add(inst); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | EthCriterion ethDst = (EthCriterion) instructions.selector() |
| 203 | .getCriterion(Criterion.Type.ETH_DST); |
| 204 | boolean broadcastObjective = ethDst != null && |
| 205 | (ethDst.mac().isBroadcast() || ethDst.mac().isMulticast()); |
| 206 | |
| 207 | if (treatmentsWithDifferentPort.size() < 2 && !broadcastObjective) { |
| 208 | objectives.addAll(createSimpleNextObjective(instructions, intent)); |
| 209 | } else { |
| 210 | objectives.addAll(createBroadcastObjective(instructions, |
| 211 | treatmentsWithDifferentPort, |
| 212 | intent)); |
| 213 | } |
| 214 | }); |
| 215 | |
| 216 | return objectives; |
| 217 | } |
| 218 | |
| 219 | private List<Objective> createBroadcastObjective(ForwardingInstructions instructions, |
| 220 | Set<TrafficTreatment> treatmentsWithDifferentPort, |
| 221 | LinkCollectionIntent intent) { |
| 222 | List<Objective> objectives = Lists.newArrayList(); |
| 223 | FilteringObjective filteringObjective; |
| 224 | ForwardingObjective forwardingObjective; |
| 225 | NextObjective nextObjective; |
| 226 | |
| 227 | Integer nextId = flowObjectiveService.allocateNextId(); |
| 228 | |
| 229 | forwardingObjective = buildForwardingObjective(instructions.selector(), |
| 230 | nextId, intent.priority()); |
| 231 | |
| 232 | DefaultNextObjective.Builder nxBuilder = DefaultNextObjective.builder(); |
| 233 | nxBuilder.withId(nextId) |
| 234 | .withMeta(instructions.selector()) |
| 235 | .withType(NextObjective.Type.BROADCAST) |
| 236 | .fromApp(appId) |
| 237 | .withPriority(intent.priority()) |
| 238 | .makePermanent(); |
| 239 | |
| 240 | treatmentsWithDifferentPort.forEach(nxBuilder::addTreatment); |
| 241 | nextObjective = nxBuilder.add(); |
| 242 | |
| 243 | filteringObjective = buildFilteringObjective(instructions.selector(), intent.priority()); |
| 244 | |
| 245 | objectives.add(filteringObjective); |
| 246 | objectives.add(forwardingObjective); |
| 247 | objectives.add(nextObjective); |
| 248 | |
| 249 | return objectives; |
| 250 | } |
| 251 | |
| 252 | private List<Objective> createSimpleNextObjective(ForwardingInstructions instructions, |
| 253 | LinkCollectionIntent intent) { |
| 254 | List<Objective> objectives = Lists.newArrayList(); |
| 255 | FilteringObjective filteringObjective; |
| 256 | ForwardingObjective forwardingObjective; |
| 257 | NextObjective nextObjective; |
| 258 | |
| 259 | Integer nextId = flowObjectiveService.allocateNextId(); |
| 260 | |
| 261 | forwardingObjective = buildForwardingObjective(instructions.selector(), |
| 262 | nextId, intent.priority()); |
| 263 | |
| 264 | DefaultNextObjective.Builder nxBuilder = DefaultNextObjective.builder(); |
| 265 | nextObjective = nxBuilder.withId(nextId) |
| 266 | .withMeta(instructions.selector()) |
| 267 | .addTreatment(instructions.treatment()) |
| 268 | .withType(NextObjective.Type.SIMPLE) |
| 269 | .fromApp(appId) |
| 270 | .makePermanent() |
| 271 | .withPriority(intent.priority()) |
| 272 | .add(); |
| 273 | |
| 274 | filteringObjective = buildFilteringObjective(instructions.selector(), intent.priority()); |
| 275 | |
| 276 | objectives.add(filteringObjective); |
| 277 | objectives.add(forwardingObjective); |
| 278 | objectives.add(nextObjective); |
| 279 | |
| 280 | return objectives; |
| 281 | } |
| 282 | |
| 283 | private ForwardingObjective buildForwardingObjective(TrafficSelector selector, Integer nextId, int priority) { |
| 284 | return DefaultForwardingObjective.builder() |
| 285 | .withMeta(selector) |
| 286 | .withSelector(selector) |
| 287 | .nextStep(nextId) |
| 288 | .fromApp(appId) |
| 289 | .withPriority(priority) |
| 290 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 291 | .makePermanent() |
| 292 | .add(); |
| 293 | } |
| 294 | |
| 295 | private FilteringObjective buildFilteringObjective(TrafficSelector selector, int priority) { |
| 296 | FilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 297 | builder.fromApp(appId) |
| 298 | .permit() |
| 299 | .makePermanent() |
| 300 | .withPriority(priority); |
| 301 | |
| 302 | Criterion inPortCriterion = |
| 303 | selector.getCriterion(Criterion.Type.IN_PORT); |
| 304 | |
| 305 | if (inPortCriterion != null) { |
| 306 | builder.withKey(inPortCriterion); |
| 307 | } |
| 308 | |
| 309 | selector.criteria().forEach(builder::addCondition); |
| 310 | return builder.add(); |
| 311 | } |
| 312 | |
| 313 | } |