blob: a9bf1b7241b1edbdde9be7456681b07cdc95aed2 [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 SHIMIZU8fa670a2016-01-14 11:17:18 -0800128 Resource.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue()),
129 Resource.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue())
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800130 ))
131 .collect(Collectors.toSet());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700132 List<org.onosproject.net.newresource.ResourceAllocation> allocations =
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800133 resourceService.allocate(intent.id(), ImmutableList.copyOf(resources));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700134 if (allocations.isEmpty()) {
135 Collections.emptyMap();
136 }
137
138 return labels;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700139 }
140
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700141 private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) {
142 Map<LinkKey, MplsLabel> labels = new HashMap<>();
143 for (LinkKey link : links) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800144 Set<MplsLabel> forward = findMplsLabel(link.src());
145 Set<MplsLabel> backward = findMplsLabel(link.dst());
146 Set<MplsLabel> common = Sets.intersection(forward, backward);
147 if (common.isEmpty()) {
148 continue;
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700149 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800150 labels.put(link, common.iterator().next());
Sho SHIMIZUcc0e65d2015-10-28 18:38:45 -0700151 }
152
153 return labels;
154 }
155
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800156 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800157 return resourceService.getAvailableResources(Resource.discrete(cp.deviceId(), cp.port())).stream()
Sho SHIMIZUc9546a32015-11-10 11:22:28 -0800158 .filter(x -> x.last() instanceof MplsLabel)
159 .map(x -> (MplsLabel) x.last())
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800160 .collect(Collectors.toSet());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100161 }
162
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700163 private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
164 return labels.get(link);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100165 }
166
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800167 private List<FlowRule> generateRules(MplsPathIntent intent,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700168 Map<LinkKey, MplsLabel> labels) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100169
170 Iterator<Link> links = intent.path().links().iterator();
171 Link srcLink = links.next();
172 ConnectPoint prev = srcLink.dst();
173
174 Link link = links.next();
175 // List of flow rules to be installed
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800176 List<FlowRule> rules = new LinkedList<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100177
178 // Ingress traffic
179 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700180 MplsLabel mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100181 checkNotNull(mpls);
182 MplsLabel prevLabel = mpls;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800183 rules.add(ingressFlow(prev.port(), link, intent, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100184
185 prev = link.dst();
186
187 while (links.hasNext()) {
188
189 link = links.next();
190
191 if (links.hasNext()) {
192 // Transit traffic
193 // Get the new MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700194 mpls = getMplsLabel(labels, linkKey(link));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100195 checkNotNull(mpls);
196 rules.add(transitFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800197 prevLabel, mpls));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100198 prevLabel = mpls;
199
200 } else {
201 // Egress traffic
202 rules.add(egressFlow(prev.port(), link, intent,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800203 prevLabel));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100204 }
205
206 prev = link.dst();
207 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800208 return rules;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100209 }
210
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800211 private FlowRule ingressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700212 MplsPathIntent intent,
213 MplsLabel label) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100214
215 TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
216 .builder(intent.selector());
217 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
218 ingressSelector.matchInPort(inPort);
219
220 if (intent.ingressLabel().isPresent()) {
221 ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
222 .matchMplsLabel(intent.ingressLabel().get());
223
224 // Swap the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700225 treat.setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100226 } else {
227 // Push and set the MPLS label
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700228 treat.pushMpls().setMpls(label);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100229 }
230 // Add the output action
231 treat.setOutput(link.src().port());
232
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800233 return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100234 }
235
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800236 private FlowRule transitFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700237 MplsPathIntent intent,
238 MplsLabel prevLabel,
239 MplsLabel outLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100240
241 // Ignore the ingress Traffic Selector and use only the MPLS label
242 // assigned in the previous link
243 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
244 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700245 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100246 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
247
248 // Set the new label only if the label on the packet is
249 // different
Michele Santuari316d8cf2015-07-14 17:24:54 +0200250 if (!prevLabel.equals(outLabel)) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700251 treat.setMpls(outLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100252 }
253
254 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800255 return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100256 }
257
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800258 private FlowRule egressFlow(PortNumber inPort, Link link,
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700259 MplsPathIntent intent,
260 MplsLabel prevLabel) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100261 // egress point: either set the egress MPLS label or pop the
262 // MPLS label based on the intent annotations
263
264 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
265 selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700266 .matchMplsLabel(prevLabel);
Michele Santuari4b6019e2014-12-19 11:31:45 +0100267
268 // apply the intent's treatments
269 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
270 .treatment());
271
Dusan Pajin6b887c92015-07-01 18:32:49 +0200272 // check if the treatement is popVlan or setVlan (rewrite),
273 // than selector needs to match any VlanId
274 for (Instruction instruct : intent.treatment().allInstructions()) {
275 if (instruct instanceof L2ModificationInstruction) {
276 L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct;
277 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) {
278 break;
279 }
280 if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP ||
281 l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
282 selector.matchVlanId(VlanId.ANY);
283 }
284 }
285 }
286
Michele Santuari4b6019e2014-12-19 11:31:45 +0100287 if (intent.egressLabel().isPresent()) {
288 treat.setMpls(intent.egressLabel().get());
289 } else {
Michele Santuari316d8cf2015-07-14 17:24:54 +0200290 treat.popMpls(outputEthType(intent.selector()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100291 }
292 treat.setOutput(link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800293 return createFlowRule(intent, link.src().deviceId(),
294 selector.build(), treat.build());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100295 }
296
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800297 protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId,
298 TrafficSelector selector, TrafficTreatment treat) {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700299 return DefaultFlowRule.builder()
300 .forDevice(deviceId)
301 .withSelector(selector)
302 .withTreatment(treat)
303 .withPriority(intent.priority())
304 .fromApp(appId)
305 .makePermanent()
306 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100307 }
Michele Santuari316d8cf2015-07-14 17:24:54 +0200308
309 // if the ingress ethertype is defined, the egress traffic
310 // will be use that value, otherwise the IPv4 ethertype is used.
311 private EthType outputEthType(TrafficSelector selector) {
312 Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE);
313 if (c != null && c instanceof EthTypeCriterion) {
314 EthTypeCriterion ethertype = (EthTypeCriterion) c;
315 return ethertype.ethType();
316 } else {
317 return EthType.EtherType.IPV4.ethType();
318 }
319 }
Michele Santuari4b6019e2014-12-19 11:31:45 +0100320}