blob: 37c0dcbd3559e9fb5cce75c09e57071b7fcaf6f0 [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;
Michele Santuari4b6019e2014-12-19 11:31:45 +010054import org.slf4j.Logger;
55
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070056import java.util.Collections;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -070057import java.util.HashMap;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080058import java.util.Iterator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080059import java.util.LinkedList;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080060import java.util.List;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -070061import java.util.Map;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080062import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070063import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080064import java.util.stream.Stream;
Michele Santuari4b6019e2014-12-19 11:31:45 +010065
Ray Milkey71ade562015-02-18 15:08:07 -080066import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070067import static org.onosproject.net.LinkKey.linkKey;
Ray Milkey71ade562015-02-18 15:08:07 -080068import static org.slf4j.LoggerFactory.getLogger;
69
Michele Santuari6096acd2016-02-09 17:00:37 +010070/**
71 * @deprecated in Goldeneye Release, in favour of encapsulation
72 * constraint {@link org.onosproject.net.intent.constraint.EncapsulationConstraint}
73 */
74@Deprecated
Michele Santuari4b6019e2014-12-19 11:31:45 +010075@Component(immediate = true)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080076public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
Michele Santuari4b6019e2014-12-19 11:31:45 +010077
78 private final Logger log = getLogger(getClass());
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080081 protected IntentExtensionService intentExtensionService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010082
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070087 protected ResourceService resourceService;
Michele Santuari4b6019e2014-12-19 11:31:45 +010088
89 protected ApplicationId appId;
90
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080091 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080092 public List<Intent> compile(MplsPathIntent intent, List<Intent> installable) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070093 Map<LinkKey, MplsLabel> labels = assignMplsLabel(intent);
94 List<FlowRule> rules = generateRules(intent, labels);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080095
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070096 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080097 }
98
Michele Santuari4b6019e2014-12-19 11:31:45 +010099 @Activate
100 public void activate() {
101 appId = coreService.registerApplication("org.onosproject.net.intent");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800102 intentExtensionService.registerCompiler(MplsPathIntent.class, this);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100103 }
104
105 @Deactivate
106 public void deactivate() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800107 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100108 }
109
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700110 private Map<LinkKey, MplsLabel> assignMplsLabel(MplsPathIntent intent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100111 // TODO: do it better... Suggestions?
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700112 Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100113 .links().size() - 2);
114 for (int i = 1; i <= intent.path().links().size() - 2; i++) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700115 LinkKey link = linkKey(intent.path().links().get(i));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100116 linkRequest.add(link);
117 // add the inverse link. I want that the label is reserved both for
118 // the direct and inverse link
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700119 linkRequest.add(linkKey(link.dst(), link.src()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100120 }
121
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700122 Map<LinkKey, MplsLabel> labels = findMplsLabels(linkRequest);
123 if (labels.isEmpty()) {
124 return Collections.emptyMap();
125 }
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700126
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800127 // for short term solution: same label is used for both directions
128 // TODO: introduce the concept of Tx and Rx resources of a port
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800129 Set<Resource> resources = labels.entrySet().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800130 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800131 Resources.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800132 .resource(),
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800133 Resources.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue())
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800134 .resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800135 ))
136 .collect(Collectors.toSet());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700137 List<org.onosproject.net.newresource.ResourceAllocation> allocations =
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800138 resourceService.allocate(intent.id(), ImmutableList.copyOf(resources));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700139 if (allocations.isEmpty()) {
140 Collections.emptyMap();
141 }
142
143 return labels;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700144 }
145
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700146 private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) {
147 Map<LinkKey, MplsLabel> labels = new HashMap<>();
148 for (LinkKey link : links) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800149 Set<MplsLabel> forward = findMplsLabel(link.src());
150 Set<MplsLabel> backward = findMplsLabel(link.dst());
151 Set<MplsLabel> common = Sets.intersection(forward, backward);
152 if (common.isEmpty()) {
153 continue;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700154 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800155 labels.put(link, common.iterator().next());
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700156 }
157
158 return labels;
159 }
160
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800161 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800162 return resourceService.getAvailableResourceValues(
163 Resources.discrete(cp.deviceId(), cp.port()).id(),
164 MplsLabel.class);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100165 }
166
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700167 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
168 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100169 }
170
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800171 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700172 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100173
174 Iterator<Link> links = intent.path().links().iterator();
175 Link srcLink = links.next();
176 ConnectPoint prev = srcLink.dst();
177
178 Link link = links.next();
179 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800180 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100181
182 // Ingress traffic
183 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700184 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100185 checkNotNull(mpls);
186 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800187 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100188
189 prev = link.dst();
190
191 while (links.hasNext()) {
192
193 link = links.next();
194
195 if (links.hasNext()) {
196 // Transit traffic
197 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700198 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100199 checkNotNull(mpls);
200 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800201 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100202 prevLabel = mpls;
203
204 } else {
205 // Egress traffic
206 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800207 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100208 }
209
210 prev = link.dst();
211 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800212 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100213 }
214
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800215 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700216 MplsPathIntent intent,
217 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100218
219 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
220 .builder(intent.selector());
221 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
222 ingressSelector.matchInPort(inPort);
223
224 if (intent.ingressLabel().isPresent()) {
225 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
226 .matchMplsLabel(intent.ingressLabel().get());
227
228 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700229 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100230 } else {
231 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700232 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100233 }
234 // Add the output action
235 treat.setOutput(link.src().port());
236
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800237 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100238 }
239
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800240 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700241 MplsPathIntent intent,
242 MplsLabel prevLabel,
243 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100244
245 // Ignore the ingress Traffic Selector and use only the MPLS label
246 // assigned in the previous link
247 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
248 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700249 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100250 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
251
252 // Set the new label only if the label on the packet is
253 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200254 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700255 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100256 }
257
258 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800259 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100260 }
261
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800262 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700263 MplsPathIntent intent,
264 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100265 // egress point: either set the egress MPLS label or pop the
266 // MPLS label based on the intent annotations
267
268 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
269 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700270 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100271
272 // apply the intent's treatments
273 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
274 .treatment());
275
Dusan Pajin6b887c92015-07-01 18:32:49 +0200276 // check if the treatement is popVlan or setVlan (rewrite),
277 // than selector needs to match any VlanId
278 for (Instruction instruct : intent.treatment().allInstructions()) {
279 if (instruct instanceof L2ModificationInstruction) {
280 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
281 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
282 break;
283 }
284 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
285 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
286 selector.matchVlanId(VlanId.ANY);
287 }
288 }
289 }
290
Michele Santuari4b6019e2014-12-19 11:31:45 +0100291 if (intent.egressLabel().isPresent()) {
292 treat.setMpls(intent.egressLabel().get());
293 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200294 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100295 }
296 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800297 return createFlowRule(intent, link.src().deviceId(),
298 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100299 }
300
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800301 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
302 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700303 return DefaultFlowRule.builder()
304 .forDevice(deviceId)
305 .withSelector(selector)
306 .withTreatment(treat)
307 .withPriority(intent.priority())
308 .fromApp(appId)
309 .makePermanent()
310 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100311 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200312
313 // if the ingress ethertype is defined, the egress traffic
314 // will be use that value, otherwise the IPv4 ethertype is used.
315 private EthType outputEthType(TrafficSelector selector) {
316 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
317 if (c != null && c instanceof EthTypeCriterion) {
318 EthTypeCriterion ethertype = (EthTypeCriterion) c;
319 return ethertype.ethType();
320 } else {
321 return EthType.EtherType.IPV4.ethType();
322 }
323 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100324}