blob: 511c0c25ea67ac20844c7b4d39c3afb137b9323f [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
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 */
16package org.onosproject.net.intent.impl.compiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +010017
Sho SHIMIZU44f37612015-11-25 16:23:22 -080018import com.google.common.collect.ImmutableList;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080019import com.google.common.collect.Sets;
Dusan Pajin6b887c92015-07-01 18:32:49 +020020
Michele Santuari4b6019e2014-12-19 11:31:45 +010021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Michele Santuari316d8cf2015-07-14 17:24:54 +020026import org.onlab.packet.EthType;
Michele Santuari4b6019e2014-12-19 11:31:45 +010027import org.onlab.packet.Ethernet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070028import org.onlab.packet.MplsLabel;
Dusan Pajin6b887c92015-07-01 18:32:49 +020029import org.onlab.packet.VlanId;
Michele Santuari4b6019e2014-12-19 11:31:45 +010030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Link;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070035import org.onosproject.net.LinkKey;
Michele Santuari4b6019e2014-12-19 11:31:45 +010036import org.onosproject.net.PortNumber;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.DefaultTrafficSelector;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.FlowRule;
Michele Santuari4b6019e2014-12-19 11:31:45 +010041import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
Michele Santuari4b6019e2014-12-19 11:31:45 +010043import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070044import org.onosproject.net.flow.criteria.EthTypeCriterion;
Dusan Pajin6b887c92015-07-01 18:32:49 +020045import org.onosproject.net.flow.instructions.Instruction;
46import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047import org.onosproject.net.intent.FlowRuleIntent;
48import org.onosproject.net.intent.Intent;
49import org.onosproject.net.intent.IntentCompiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +010050import org.onosproject.net.intent.IntentExtensionService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010051import org.onosproject.net.intent.MplsPathIntent;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080052import org.onosproject.net.newresource.Resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070053import org.onosproject.net.newresource.ResourceService;
Brian O'Connor6de2e202015-05-21 14:30:41 -070054import org.onosproject.net.resource.link.LinkResourceAllocations;
Michele Santuari4b6019e2014-12-19 11:31:45 +010055import org.slf4j.Logger;
56
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070057import java.util.Collections;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -070058import java.util.HashMap;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080059import java.util.Iterator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060import java.util.LinkedList;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080061import java.util.List;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -070062import java.util.Map;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080063import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070064import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080065import java.util.stream.Stream;
Michele Santuari4b6019e2014-12-19 11:31:45 +010066
Ray Milkey71ade562015-02-18 15:08:07 -080067import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070068import static org.onosproject.net.LinkKey.linkKey;
Ray Milkey71ade562015-02-18 15:08:07 -080069import static org.slf4j.LoggerFactory.getLogger;
70
Michele Santuari4b6019e2014-12-19 11:31:45 +010071@Component(immediate = true)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
Michele Santuari4b6019e2014-12-19 11:31:45 +010073
74 private final Logger log = getLogger(getClass());
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080077 protected IntentExtensionService intentExtensionService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010078
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected CoreService coreService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070083 protected ResourceService resourceService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010084
85 protected ApplicationId appId;
86
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080087 @Override
88 public List<Intent> compile(MplsPathIntent intent, List<Intent> installable,
89 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070090 Map<LinkKey, MplsLabel> labels = assignMplsLabel(intent);
91 List<FlowRule> rules = generateRules(intent, labels);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070093 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 }
95
Michele Santuari4b6019e2014-12-19 11:31:45 +010096 @Activate
97 public void activate() {
98 appId = coreService.registerApplication("org.onosproject.net.intent");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 intentExtensionService.registerCompiler(MplsPathIntent.class, this);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100100 }
101
102 @Deactivate
103 public void deactivate() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100105 }
106
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700107 private Map<LinkKey, MplsLabel> assignMplsLabel(MplsPathIntent intent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100108 // TODO: do it better... Suggestions?
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700109 Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100110 .links().size() - 2);
111 for (int i = 1; i <= intent.path().links().size() - 2; i++) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700112 LinkKey link = linkKey(intent.path().links().get(i));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100113 linkRequest.add(link);
114 // add the inverse link. I want that the label is reserved both for
115 // the direct and inverse link
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700116 linkRequest.add(linkKey(link.dst(), link.src()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100117 }
118
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700119 Map<LinkKey, MplsLabel> labels = findMplsLabels(linkRequest);
120 if (labels.isEmpty()) {
121 return Collections.emptyMap();
122 }
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700123
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800124 // for short term solution: same label is used for both directions
125 // TODO: introduce the concept of Tx and Rx resources of a port
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800126 Set<Resource> resources = labels.entrySet().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800127 .flatMap(x -> Stream.of(
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800128 Resource.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue())
129 .resource(),
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800130 Resource.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800131 .resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800132 ))
133 .collect(Collectors.toSet());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700134 List<org.onosproject.net.newresource.ResourceAllocation> allocations =
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800135 resourceService.allocate(intent.id(), ImmutableList.copyOf(resources));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700136 if (allocations.isEmpty()) {
137 Collections.emptyMap();
138 }
139
140 return labels;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700141 }
142
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700143 private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) {
144 Map<LinkKey, MplsLabel> labels = new HashMap<>();
145 for (LinkKey link : links) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800146 Set<MplsLabel> forward = findMplsLabel(link.src());
147 Set<MplsLabel> backward = findMplsLabel(link.dst());
148 Set<MplsLabel> common = Sets.intersection(forward, backward);
149 if (common.isEmpty()) {
150 continue;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700151 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800152 labels.put(link, common.iterator().next());
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700153 }
154
155 return labels;
156 }
157
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800158 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800159 return resourceService.getAvailableResources(Resource.discrete(cp.deviceId(), cp.port()).resource()).stream()
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800160 .filter(x -> x.last() instanceof MplsLabel)
161 .map(x -> (MplsLabel) x.last())
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800162 .collect(Collectors.toSet());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100163 }
164
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700165 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
166 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100167 }
168
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800169 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700170 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100171
172 Iterator<Link> links = intent.path().links().iterator();
173 Link srcLink = links.next();
174 ConnectPoint prev = srcLink.dst();
175
176 Link link = links.next();
177 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800178 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100179
180 // Ingress traffic
181 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700182 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100183 checkNotNull(mpls);
184 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800185 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100186
187 prev = link.dst();
188
189 while (links.hasNext()) {
190
191 link = links.next();
192
193 if (links.hasNext()) {
194 // Transit traffic
195 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700196 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100197 checkNotNull(mpls);
198 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800199 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100200 prevLabel = mpls;
201
202 } else {
203 // Egress traffic
204 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800205 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100206 }
207
208 prev = link.dst();
209 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800210 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100211 }
212
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800213 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700214 MplsPathIntent intent,
215 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100216
217 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
218 .builder(intent.selector());
219 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
220 ingressSelector.matchInPort(inPort);
221
222 if (intent.ingressLabel().isPresent()) {
223 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
224 .matchMplsLabel(intent.ingressLabel().get());
225
226 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700227 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100228 } else {
229 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700230 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100231 }
232 // Add the output action
233 treat.setOutput(link.src().port());
234
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800235 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100236 }
237
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800238 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700239 MplsPathIntent intent,
240 MplsLabel prevLabel,
241 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100242
243 // Ignore the ingress Traffic Selector and use only the MPLS label
244 // assigned in the previous link
245 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
246 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700247 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100248 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
249
250 // Set the new label only if the label on the packet is
251 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200252 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700253 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100254 }
255
256 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800257 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100258 }
259
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800260 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700261 MplsPathIntent intent,
262 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100263 // egress point: either set the egress MPLS label or pop the
264 // MPLS label based on the intent annotations
265
266 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
267 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700268 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100269
270 // apply the intent's treatments
271 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
272 .treatment());
273
Dusan Pajin6b887c92015-07-01 18:32:49 +0200274 // check if the treatement is popVlan or setVlan (rewrite),
275 // than selector needs to match any VlanId
276 for (Instruction instruct : intent.treatment().allInstructions()) {
277 if (instruct instanceof L2ModificationInstruction) {
278 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
279 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
280 break;
281 }
282 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
283 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
284 selector.matchVlanId(VlanId.ANY);
285 }
286 }
287 }
288
Michele Santuari4b6019e2014-12-19 11:31:45 +0100289 if (intent.egressLabel().isPresent()) {
290 treat.setMpls(intent.egressLabel().get());
291 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200292 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100293 }
294 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800295 return createFlowRule(intent, link.src().deviceId(),
296 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100297 }
298
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800299 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
300 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700301 return DefaultFlowRule.builder()
302 .forDevice(deviceId)
303 .withSelector(selector)
304 .withTreatment(treat)
305 .withPriority(intent.priority())
306 .fromApp(appId)
307 .makePermanent()
308 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100309 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200310
311 // if the ingress ethertype is defined, the egress traffic
312 // will be use that value, otherwise the IPv4 ethertype is used.
313 private EthType outputEthType(TrafficSelector selector) {
314 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
315 if (c != null && c instanceof EthTypeCriterion) {
316 EthTypeCriterion ethertype = (EthTypeCriterion) c;
317 return ethertype.ethType();
318 } else {
319 return EthType.EtherType.IPV4.ethType();
320 }
321 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100322}