blob: 28b8a0409d30bf256c85f7364c1ae4eb5b5deaab [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;
Michele Santuari4b6019e2014-12-19 11:31:45 +010020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Michele Santuari316d8cf2015-07-14 17:24:54 +020025import org.onlab.packet.EthType;
Michele Santuari4b6019e2014-12-19 11:31:45 +010026import org.onlab.packet.Ethernet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070027import org.onlab.packet.MplsLabel;
Dusan Pajin6b887c92015-07-01 18:32:49 +020028import org.onlab.packet.VlanId;
Michele Santuari4b6019e2014-12-19 11:31:45 +010029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Link;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070034import org.onosproject.net.LinkKey;
Michele Santuari4b6019e2014-12-19 11:31:45 +010035import org.onosproject.net.PortNumber;
36import org.onosproject.net.flow.DefaultFlowRule;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.FlowRule;
Michele Santuari4b6019e2014-12-19 11:31:45 +010040import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
Michele Santuari4b6019e2014-12-19 11:31:45 +010042import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070043import org.onosproject.net.flow.criteria.EthTypeCriterion;
Dusan Pajin6b887c92015-07-01 18:32:49 +020044import org.onosproject.net.flow.instructions.Instruction;
45import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046import org.onosproject.net.intent.FlowRuleIntent;
47import org.onosproject.net.intent.Intent;
48import org.onosproject.net.intent.IntentCompiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +010049import org.onosproject.net.intent.IntentExtensionService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010050import org.onosproject.net.intent.MplsPathIntent;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080051import org.onosproject.net.newresource.Resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070052import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080053import org.onosproject.net.newresource.Resources;
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 SHIMIZU460b9722016-01-28 10:48:26 -0800128 Resources.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800129 .resource(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800130 Resources.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 SHIMIZU7332fe42016-02-15 14:58:33 -0800159 return resourceService.getAvailableResourceValues(
160 Resources.discrete(cp.deviceId(), cp.port()).id(),
161 MplsLabel.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100162 }
163
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700164 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
165 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100166 }
167
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800168 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700169 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100170
171 Iterator<Link> links = intent.path().links().iterator();
172 Link srcLink = links.next();
173 ConnectPoint prev = srcLink.dst();
174
175 Link link = links.next();
176 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800177 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100178
179 // Ingress traffic
180 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700181 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100182 checkNotNull(mpls);
183 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800184 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100185
186 prev = link.dst();
187
188 while (links.hasNext()) {
189
190 link = links.next();
191
192 if (links.hasNext()) {
193 // Transit traffic
194 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700195 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100196 checkNotNull(mpls);
197 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800198 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100199 prevLabel = mpls;
200
201 } else {
202 // Egress traffic
203 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800204 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100205 }
206
207 prev = link.dst();
208 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800209 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100210 }
211
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800212 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700213 MplsPathIntent intent,
214 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100215
216 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
217 .builder(intent.selector());
218 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
219 ingressSelector.matchInPort(inPort);
220
221 if (intent.ingressLabel().isPresent()) {
222 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
223 .matchMplsLabel(intent.ingressLabel().get());
224
225 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700226 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100227 } else {
228 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700229 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100230 }
231 // Add the output action
232 treat.setOutput(link.src().port());
233
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800234 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100235 }
236
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800237 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700238 MplsPathIntent intent,
239 MplsLabel prevLabel,
240 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100241
242 // Ignore the ingress Traffic Selector and use only the MPLS label
243 // assigned in the previous link
244 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
245 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700246 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100247 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
248
249 // Set the new label only if the label on the packet is
250 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200251 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700252 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100253 }
254
255 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800256 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100257 }
258
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800259 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700260 MplsPathIntent intent,
261 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100262 // egress point: either set the egress MPLS label or pop the
263 // MPLS label based on the intent annotations
264
265 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
266 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700267 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100268
269 // apply the intent's treatments
270 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
271 .treatment());
272
Dusan Pajin6b887c92015-07-01 18:32:49 +0200273 // check if the treatement is popVlan or setVlan (rewrite),
274 // than selector needs to match any VlanId
275 for (Instruction instruct : intent.treatment().allInstructions()) {
276 if (instruct instanceof L2ModificationInstruction) {
277 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
278 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
279 break;
280 }
281 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
282 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
283 selector.matchVlanId(VlanId.ANY);
284 }
285 }
286 }
287
Michele Santuari4b6019e2014-12-19 11:31:45 +0100288 if (intent.egressLabel().isPresent()) {
289 treat.setMpls(intent.egressLabel().get());
290 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200291 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100292 }
293 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800294 return createFlowRule(intent, link.src().deviceId(),
295 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100296 }
297
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800298 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
299 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700300 return DefaultFlowRule.builder()
301 .forDevice(deviceId)
302 .withSelector(selector)
303 .withTreatment(treat)
304 .withPriority(intent.priority())
305 .fromApp(appId)
306 .makePermanent()
307 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100308 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200309
310 // if the ingress ethertype is defined, the egress traffic
311 // will be use that value, otherwise the IPv4 ethertype is used.
312 private EthType outputEthType(TrafficSelector selector) {
313 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
314 if (c != null && c instanceof EthTypeCriterion) {
315 EthTypeCriterion ethertype = (EthTypeCriterion) c;
316 return ethertype.ethType();
317 } else {
318 return EthType.EtherType.IPV4.ethType();
319 }
320 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100321}