blob: 095c8e91e4d9085e56f7d700f041f0452f269e55 [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 Santuari6096acd2016-02-09 17:00:37 +010071/**
72 * @deprecated in Goldeneye Release, in favour of encapsulation
73 * constraint {@link org.onosproject.net.intent.constraint.EncapsulationConstraint}
74 */
75@Deprecated
Michele Santuari4b6019e2014-12-19 11:31:45 +010076@Component(immediate = true)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080077public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
Michele Santuari4b6019e2014-12-19 11:31:45 +010078
79 private final Logger log = getLogger(getClass());
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080082 protected IntentExtensionService intentExtensionService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010083
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected CoreService coreService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070088 protected ResourceService resourceService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010089
90 protected ApplicationId appId;
91
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092 @Override
93 public List<Intent> compile(MplsPathIntent intent, List<Intent> installable,
94 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070095 Map<LinkKey, MplsLabel> labels = assignMplsLabel(intent);
96 List<FlowRule> rules = generateRules(intent, labels);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080097
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070098 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 }
100
Michele Santuari4b6019e2014-12-19 11:31:45 +0100101 @Activate
102 public void activate() {
103 appId = coreService.registerApplication("org.onosproject.net.intent");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 intentExtensionService.registerCompiler(MplsPathIntent.class, this);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100105 }
106
107 @Deactivate
108 public void deactivate() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800109 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100110 }
111
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700112 private Map<LinkKey, MplsLabel> assignMplsLabel(MplsPathIntent intent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100113 // TODO: do it better... Suggestions?
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700114 Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100115 .links().size() - 2);
116 for (int i = 1; i <= intent.path().links().size() - 2; i++) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700117 LinkKey link = linkKey(intent.path().links().get(i));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100118 linkRequest.add(link);
119 // add the inverse link. I want that the label is reserved both for
120 // the direct and inverse link
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700121 linkRequest.add(linkKey(link.dst(), link.src()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100122 }
123
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700124 Map<LinkKey, MplsLabel> labels = findMplsLabels(linkRequest);
125 if (labels.isEmpty()) {
126 return Collections.emptyMap();
127 }
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700128
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800129 // for short term solution: same label is used for both directions
130 // TODO: introduce the concept of Tx and Rx resources of a port
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800131 Set<Resource> resources = labels.entrySet().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800132 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800133 Resources.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800134 .resource(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800135 Resources.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800136 .resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800137 ))
138 .collect(Collectors.toSet());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700139 List<org.onosproject.net.newresource.ResourceAllocation> allocations =
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800140 resourceService.allocate(intent.id(), ImmutableList.copyOf(resources));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700141 if (allocations.isEmpty()) {
142 Collections.emptyMap();
143 }
144
145 return labels;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700146 }
147
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700148 private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) {
149 Map<LinkKey, MplsLabel> labels = new HashMap<>();
150 for (LinkKey link : links) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800151 Set<MplsLabel> forward = findMplsLabel(link.src());
152 Set<MplsLabel> backward = findMplsLabel(link.dst());
153 Set<MplsLabel> common = Sets.intersection(forward, backward);
154 if (common.isEmpty()) {
155 continue;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700156 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800157 labels.put(link, common.iterator().next());
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700158 }
159
160 return labels;
161 }
162
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800163 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800164 return resourceService.getAvailableResourceValues(
165 Resources.discrete(cp.deviceId(), cp.port()).id(),
166 MplsLabel.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100167 }
168
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700169 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
170 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100171 }
172
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800173 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700174 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100175
176 Iterator<Link> links = intent.path().links().iterator();
177 Link srcLink = links.next();
178 ConnectPoint prev = srcLink.dst();
179
180 Link link = links.next();
181 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800182 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100183
184 // Ingress traffic
185 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700186 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100187 checkNotNull(mpls);
188 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800189 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100190
191 prev = link.dst();
192
193 while (links.hasNext()) {
194
195 link = links.next();
196
197 if (links.hasNext()) {
198 // Transit traffic
199 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700200 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100201 checkNotNull(mpls);
202 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800203 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100204 prevLabel = mpls;
205
206 } else {
207 // Egress traffic
208 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800209 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100210 }
211
212 prev = link.dst();
213 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800214 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100215 }
216
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800217 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700218 MplsPathIntent intent,
219 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100220
221 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
222 .builder(intent.selector());
223 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
224 ingressSelector.matchInPort(inPort);
225
226 if (intent.ingressLabel().isPresent()) {
227 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
228 .matchMplsLabel(intent.ingressLabel().get());
229
230 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700231 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100232 } else {
233 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700234 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100235 }
236 // Add the output action
237 treat.setOutput(link.src().port());
238
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800239 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100240 }
241
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800242 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700243 MplsPathIntent intent,
244 MplsLabel prevLabel,
245 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100246
247 // Ignore the ingress Traffic Selector and use only the MPLS label
248 // assigned in the previous link
249 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
250 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700251 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100252 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
253
254 // Set the new label only if the label on the packet is
255 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200256 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700257 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100258 }
259
260 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800261 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100262 }
263
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800264 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700265 MplsPathIntent intent,
266 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100267 // egress point: either set the egress MPLS label or pop the
268 // MPLS label based on the intent annotations
269
270 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
271 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700272 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100273
274 // apply the intent's treatments
275 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
276 .treatment());
277
Dusan Pajin6b887c92015-07-01 18:32:49 +0200278 // check if the treatement is popVlan or setVlan (rewrite),
279 // than selector needs to match any VlanId
280 for (Instruction instruct : intent.treatment().allInstructions()) {
281 if (instruct instanceof L2ModificationInstruction) {
282 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
283 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
284 break;
285 }
286 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
287 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
288 selector.matchVlanId(VlanId.ANY);
289 }
290 }
291 }
292
Michele Santuari4b6019e2014-12-19 11:31:45 +0100293 if (intent.egressLabel().isPresent()) {
294 treat.setMpls(intent.egressLabel().get());
295 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200296 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100297 }
298 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800299 return createFlowRule(intent, link.src().deviceId(),
300 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100301 }
302
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800303 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
304 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700305 return DefaultFlowRule.builder()
306 .forDevice(deviceId)
307 .withSelector(selector)
308 .withTreatment(treat)
309 .withPriority(intent.priority())
310 .fromApp(appId)
311 .makePermanent()
312 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100313 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200314
315 // if the ingress ethertype is defined, the egress traffic
316 // will be use that value, otherwise the IPv4 ethertype is used.
317 private EthType outputEthType(TrafficSelector selector) {
318 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
319 if (c != null && c instanceof EthTypeCriterion) {
320 EthTypeCriterion ethertype = (EthTypeCriterion) c;
321 return ethertype.ethType();
322 } else {
323 return EthType.EtherType.IPV4.ethType();
324 }
325 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100326}