Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Sets; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.ConnectPoint; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 29 | import org.onosproject.net.EncapsulationType; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Link; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 31 | import org.onosproject.net.LinkKey; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.DefaultFlowRule; |
| 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.FlowRule; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 38 | import org.onosproject.net.flow.criteria.Criterion; |
| 39 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.FlowRuleIntent; |
| 41 | import org.onosproject.net.intent.Intent; |
| 42 | import org.onosproject.net.intent.IntentCompiler; |
| 43 | import org.onosproject.net.intent.IntentExtensionService; |
| 44 | import org.onosproject.net.intent.PathIntent; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 45 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 46 | import org.onosproject.net.intent.impl.IntentCompilationException; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame^] | 47 | import org.onosproject.net.newresource.Resource; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 48 | import org.onosproject.net.newresource.ResourceService; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 49 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 50 | import org.slf4j.Logger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 51 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 53 | import java.util.HashMap; |
| 54 | import java.util.Iterator; |
| 55 | import java.util.LinkedList; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 56 | import java.util.List; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 57 | import java.util.Map; |
| 58 | import java.util.Optional; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 59 | import java.util.Set; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
| 61 | import java.util.stream.Stream; |
| 62 | |
| 63 | import static org.onosproject.net.LinkKey.linkKey; |
| 64 | import static org.slf4j.LoggerFactory.getLogger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 65 | |
| 66 | @Component(immediate = true) |
| 67 | public class PathIntentCompiler implements IntentCompiler<PathIntent> { |
| 68 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected CoreService coreService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected IntentExtensionService intentManager; |
| 76 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected ResourceService resourceService; |
| 79 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 80 | private ApplicationId appId; |
| 81 | |
| 82 | @Activate |
| 83 | public void activate() { |
| 84 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 85 | intentManager.registerCompiler(PathIntent.class, this); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
| 90 | intentManager.unregisterCompiler(PathIntent.class); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public List<Intent> compile(PathIntent intent, List<Intent> installable, |
| 95 | Set<LinkResourceAllocations> resources) { |
| 96 | // Note: right now recompile is not considered |
| 97 | // TODO: implement recompile behavior |
| 98 | |
| 99 | List<Link> links = intent.path().links(); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 100 | List<FlowRule> rules = new LinkedList<>(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 101 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 102 | Optional<EncapsulationConstraint> enacpConstraint = intent.constraints().stream() |
| 103 | .filter(constraint -> constraint instanceof EncapsulationConstraint) |
| 104 | .map(x -> (EncapsulationConstraint) x).findAny(); |
| 105 | //if no encapsulation or is involved only a single switch use the default behaviour |
| 106 | if (!enacpConstraint.isPresent() || links.size() == 1) { |
| 107 | |
| 108 | for (int i = 0; i < links.size() - 1; i++) { |
| 109 | ConnectPoint ingress = links.get(i).dst(); |
| 110 | ConnectPoint egress = links.get(i + 1).src(); |
| 111 | FlowRule rule = createFlowRule(intent.selector(), intent.treatment(), |
| 112 | ingress, egress, intent.priority(), |
| 113 | isLast(links, i)); |
| 114 | rules.add(rule); |
| 115 | } |
| 116 | |
| 117 | return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources())); |
| 118 | |
| 119 | } else { |
| 120 | if (EncapsulationType.VLAN == enacpConstraint.get().encapType()) { |
| 121 | rules = manageVlanEncap(intent); |
| 122 | } |
| 123 | if (EncapsulationType.MPLS == enacpConstraint.get().encapType()) { |
| 124 | //TODO: to be implemented |
| 125 | rules = Collections.emptyList(); |
| 126 | } |
| 127 | |
| 128 | return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 129 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private FlowRule createFlowRule(TrafficSelector originalSelector, TrafficTreatment originalTreatment, |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 133 | ConnectPoint ingress, ConnectPoint egress, |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 134 | int priority, boolean applyTreatment) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 135 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 136 | .matchInPort(ingress.port()) |
| 137 | .build(); |
| 138 | |
| 139 | TrafficTreatment.Builder treatmentBuilder; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 140 | if (applyTreatment) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 141 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 142 | } else { |
| 143 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 144 | } |
| 145 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 146 | |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 147 | return DefaultFlowRule.builder() |
| 148 | .forDevice(ingress.deviceId()) |
| 149 | .withSelector(selector) |
| 150 | .withTreatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 151 | .withPriority(priority) |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 152 | .fromApp(appId) |
| 153 | .makePermanent() |
| 154 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 157 | private List<FlowRule> manageVlanEncap(PathIntent intent) { |
| 158 | Map<LinkKey, VlanId> vlanIds = assignVlanId(intent); |
| 159 | |
| 160 | Iterator<Link> links = intent.path().links().iterator(); |
| 161 | Link srcLink = links.next(); |
| 162 | |
| 163 | Link link = links.next(); |
| 164 | // List of flow rules to be installed |
| 165 | List<FlowRule> rules = new LinkedList<>(); |
| 166 | |
| 167 | // Ingress traffic |
| 168 | VlanId vlanId = vlanIds.get(linkKey(link)); |
| 169 | if (vlanId == null) { |
| 170 | throw new IntentCompilationException("No available VLAN ID for " + link); |
| 171 | } |
| 172 | VlanId prevVlanId = vlanId; |
| 173 | |
| 174 | //Tag the traffic with the new VLAN |
| 175 | TrafficTreatment treat = DefaultTrafficTreatment.builder() |
| 176 | .setVlanId(vlanId) |
| 177 | .build(); |
| 178 | |
| 179 | rules.add(createFlowRule(intent.selector(), treat, srcLink.dst(), link.src(), intent.priority(), true)); |
| 180 | |
| 181 | ConnectPoint prev = link.dst(); |
| 182 | |
| 183 | while (links.hasNext()) { |
| 184 | |
| 185 | link = links.next(); |
| 186 | |
| 187 | if (links.hasNext()) { |
| 188 | // Transit traffic |
| 189 | VlanId egressVlanId = vlanIds.get(linkKey(link)); |
| 190 | if (egressVlanId == null) { |
| 191 | throw new IntentCompilationException("No available VLAN ID for " + link); |
| 192 | } |
| 193 | prevVlanId = egressVlanId; |
| 194 | |
| 195 | TrafficSelector transitSelector = DefaultTrafficSelector.builder() |
| 196 | .matchInPort(prev.port()) |
| 197 | .matchVlanId(prevVlanId).build(); |
| 198 | |
| 199 | TrafficTreatment.Builder transitTreat = DefaultTrafficTreatment.builder(); |
| 200 | |
| 201 | // Set the new vlanId only if the previous one is different |
| 202 | if (!prevVlanId.equals(egressVlanId)) { |
| 203 | transitTreat.setVlanId(egressVlanId); |
| 204 | } |
| 205 | rules.add(createFlowRule(transitSelector, |
| 206 | transitTreat.build(), prev, link.src(), intent.priority(), true)); |
| 207 | prev = link.dst(); |
| 208 | } else { |
| 209 | // Egress traffic |
| 210 | TrafficSelector egressSelector = DefaultTrafficSelector.builder() |
| 211 | .matchInPort(prev.port()) |
| 212 | .matchVlanId(prevVlanId).build(); |
| 213 | |
| 214 | //TODO: think to other cases for egress packet restoration |
| 215 | Optional<VlanIdCriterion> vlanCriteria = intent.selector().criteria() |
| 216 | .stream().filter(criteria -> criteria.type() == Criterion.Type.VLAN_VID) |
| 217 | .map(criteria -> (VlanIdCriterion) criteria) |
| 218 | .findAny(); |
| 219 | TrafficTreatment.Builder egressTreat = DefaultTrafficTreatment.builder(intent.treatment()); |
| 220 | if (vlanCriteria.isPresent()) { |
| 221 | egressTreat.setVlanId(vlanCriteria.get().vlanId()); |
| 222 | } else { |
| 223 | egressTreat.popVlan(); |
| 224 | } |
| 225 | |
| 226 | rules.add(createFlowRule(egressSelector, |
| 227 | egressTreat.build(), prev, link.src(), intent.priority(), true)); |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | return rules; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | private Map<LinkKey, VlanId> assignVlanId(PathIntent intent) { |
| 236 | Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path() |
| 237 | .links().size() - 2); |
| 238 | for (int i = 1; i <= intent.path().links().size() - 2; i++) { |
| 239 | LinkKey link = linkKey(intent.path().links().get(i)); |
| 240 | linkRequest.add(link); |
| 241 | // add the inverse link. I want that the VLANID is reserved both for |
| 242 | // the direct and inverse link |
| 243 | linkRequest.add(linkKey(link.dst(), link.src())); |
| 244 | } |
| 245 | |
| 246 | Map<LinkKey, VlanId> vlanIds = findVlanIds(linkRequest); |
| 247 | if (vlanIds.isEmpty()) { |
| 248 | log.warn("No VLAN IDs available"); |
| 249 | return Collections.emptyMap(); |
| 250 | } |
| 251 | |
| 252 | //same VLANID is used for both directions |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame^] | 253 | Set<Resource> resources = vlanIds.entrySet().stream() |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 254 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame^] | 255 | Resource.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue()), |
| 256 | Resource.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue()) |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 257 | )) |
| 258 | .collect(Collectors.toSet()); |
| 259 | List<org.onosproject.net.newresource.ResourceAllocation> allocations = |
| 260 | resourceService.allocate(intent.id(), ImmutableList.copyOf(resources)); |
| 261 | if (allocations.isEmpty()) { |
| 262 | Collections.emptyMap(); |
| 263 | } |
| 264 | |
| 265 | return vlanIds; |
| 266 | } |
| 267 | |
| 268 | private Map<LinkKey, VlanId> findVlanIds(Set<LinkKey> links) { |
| 269 | Map<LinkKey, VlanId> vlanIds = new HashMap<>(); |
| 270 | for (LinkKey link : links) { |
| 271 | Set<VlanId> forward = findVlanId(link.src()); |
| 272 | Set<VlanId> backward = findVlanId(link.dst()); |
| 273 | Set<VlanId> common = Sets.intersection(forward, backward); |
| 274 | if (common.isEmpty()) { |
| 275 | continue; |
| 276 | } |
| 277 | vlanIds.put(link, common.iterator().next()); |
| 278 | } |
| 279 | return vlanIds; |
| 280 | } |
| 281 | |
| 282 | private Set<VlanId> findVlanId(ConnectPoint cp) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame^] | 283 | return resourceService.getAvailableResources(Resource.discrete(cp.deviceId(), cp.port())).stream() |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 284 | .filter(x -> x.last() instanceof VlanId) |
| 285 | .map(x -> (VlanId) x.last()) |
| 286 | .collect(Collectors.toSet()); |
| 287 | } |
| 288 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 289 | private boolean isLast(List<Link> links, int i) { |
| 290 | return i == links.size() - 2; |
| 291 | } |
| 292 | } |