blob: 9c4f53500ecdd9cd63c2c18964edd0b34202c3c9 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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 SHIMIZUe18cb122016-02-22 21:04:56 -080051import org.onosproject.net.resource.Resource;
52import org.onosproject.net.resource.ResourceAllocation;
53import org.onosproject.net.resource.ResourceService;
54import org.onosproject.net.resource.Resources;
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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080093 public List<Intent> compile(MplsPathIntent intent, List<Intent> installable) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070094 Map<LinkKey, MplsLabel> labels = assignMplsLabel(intent);
95 List<FlowRule> rules = generateRules(intent, labels);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080096
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070097 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080098 }
99
Michele Santuari4b6019e2014-12-19 11:31:45 +0100100 @Activate
101 public void activate() {
102 appId = coreService.registerApplication("org.onosproject.net.intent");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800103 intentExtensionService.registerCompiler(MplsPathIntent.class, this);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100104 }
105
106 @Deactivate
107 public void deactivate() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800108 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100109 }
110
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700111 private Map<LinkKey, MplsLabel> assignMplsLabel(MplsPathIntent intent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100112 // TODO: do it better... Suggestions?
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700113 Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100114 .links().size() - 2);
115 for (int i = 1; i <= intent.path().links().size() - 2; i++) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700116 LinkKey link = linkKey(intent.path().links().get(i));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100117 linkRequest.add(link);
118 // add the inverse link. I want that the label is reserved both for
119 // the direct and inverse link
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700120 linkRequest.add(linkKey(link.dst(), link.src()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100121 }
122
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700123 Map<LinkKey, MplsLabel> labels = findMplsLabels(linkRequest);
124 if (labels.isEmpty()) {
125 return Collections.emptyMap();
126 }
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700127
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800128 // for short term solution: same label is used for both directions
129 // TODO: introduce the concept of Tx and Rx resources of a port
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800130 Set<Resource> resources = labels.entrySet().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800131 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800132 Resources.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800133 .resource(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800134 Resources.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800135 .resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800136 ))
137 .collect(Collectors.toSet());
Sho SHIMIZUe18cb122016-02-22 21:04:56 -0800138 List<ResourceAllocation> allocations =
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800139 resourceService.allocate(intent.id(), ImmutableList.copyOf(resources));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700140 if (allocations.isEmpty()) {
141 Collections.emptyMap();
142 }
143
144 return labels;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700145 }
146
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700147 private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) {
148 Map<LinkKey, MplsLabel> labels = new HashMap<>();
149 for (LinkKey link : links) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800150 Set<MplsLabel> forward = findMplsLabel(link.src());
151 Set<MplsLabel> backward = findMplsLabel(link.dst());
152 Set<MplsLabel> common = Sets.intersection(forward, backward);
153 if (common.isEmpty()) {
154 continue;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700155 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800156 labels.put(link, common.iterator().next());
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700157 }
158
159 return labels;
160 }
161
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800162 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800163 return resourceService.getAvailableResourceValues(
164 Resources.discrete(cp.deviceId(), cp.port()).id(),
165 MplsLabel.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100166 }
167
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700168 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
169 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100170 }
171
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800172 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700173 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100174
175 Iterator<Link> links = intent.path().links().iterator();
176 Link srcLink = links.next();
177 ConnectPoint prev = srcLink.dst();
178
179 Link link = links.next();
180 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800181 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100182
183 // Ingress traffic
184 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700185 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100186 checkNotNull(mpls);
187 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800188 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100189
190 prev = link.dst();
191
192 while (links.hasNext()) {
193
194 link = links.next();
195
196 if (links.hasNext()) {
197 // Transit traffic
198 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700199 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100200 checkNotNull(mpls);
201 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800202 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100203 prevLabel = mpls;
204
205 } else {
206 // Egress traffic
207 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800208 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100209 }
210
211 prev = link.dst();
212 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800213 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100214 }
215
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800216 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700217 MplsPathIntent intent,
218 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100219
220 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
221 .builder(intent.selector());
222 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
223 ingressSelector.matchInPort(inPort);
224
225 if (intent.ingressLabel().isPresent()) {
226 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
227 .matchMplsLabel(intent.ingressLabel().get());
228
229 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700230 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100231 } else {
232 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700233 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100234 }
235 // Add the output action
236 treat.setOutput(link.src().port());
237
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800238 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100239 }
240
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800241 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700242 MplsPathIntent intent,
243 MplsLabel prevLabel,
244 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100245
246 // Ignore the ingress Traffic Selector and use only the MPLS label
247 // assigned in the previous link
248 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
249 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700250 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100251 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
252
253 // Set the new label only if the label on the packet is
254 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200255 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700256 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100257 }
258
259 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800260 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100261 }
262
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800263 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700264 MplsPathIntent intent,
265 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100266 // egress point: either set the egress MPLS label or pop the
267 // MPLS label based on the intent annotations
268
269 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
270 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700271 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100272
273 // apply the intent's treatments
274 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
275 .treatment());
276
Dusan Pajin6b887c92015-07-01 18:32:49 +0200277 // check if the treatement is popVlan or setVlan (rewrite),
278 // than selector needs to match any VlanId
279 for (Instruction instruct : intent.treatment().allInstructions()) {
280 if (instruct instanceof L2ModificationInstruction) {
281 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
282 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
283 break;
284 }
285 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
286 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
287 selector.matchVlanId(VlanId.ANY);
288 }
289 }
290 }
291
Michele Santuari4b6019e2014-12-19 11:31:45 +0100292 if (intent.egressLabel().isPresent()) {
293 treat.setMpls(intent.egressLabel().get());
294 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200295 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100296 }
297 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800298 return createFlowRule(intent, link.src().deviceId(),
299 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100300 }
301
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800302 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
303 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700304 return DefaultFlowRule.builder()
305 .forDevice(deviceId)
306 .withSelector(selector)
307 .withTreatment(treat)
308 .withPriority(intent.priority())
309 .fromApp(appId)
310 .makePermanent()
311 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100312 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200313
314 // if the ingress ethertype is defined, the egress traffic
315 // will be use that value, otherwise the IPv4 ethertype is used.
316 private EthType outputEthType(TrafficSelector selector) {
317 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
318 if (c != null && c instanceof EthTypeCriterion) {
319 EthTypeCriterion ethertype = (EthTypeCriterion) c;
320 return ethertype.ethType();
321 } else {
322 return EthType.EtherType.IPV4.ethType();
323 }
324 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100325}