Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 17 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 20 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Michele Santuari | 316d8cf | 2015-07-14 17:24:54 +0200 | [diff] [blame] | 26 | import org.onlab.packet.EthType; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 28 | import org.onlab.packet.MplsLabel; |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 30 | import org.onlab.util.Tools; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Link; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 36 | import org.onosproject.net.LinkKey; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 46 | import org.onosproject.net.flow.instructions.Instruction; |
| 47 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.FlowRuleIntent; |
| 49 | import org.onosproject.net.intent.Intent; |
| 50 | import org.onosproject.net.intent.IntentCompiler; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 51 | import org.onosproject.net.intent.IntentExtensionService; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 52 | import org.onosproject.net.intent.MplsPathIntent; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 53 | import org.onosproject.net.newresource.Resource; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 54 | import org.onosproject.net.newresource.ResourceService; |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 55 | import org.onosproject.net.newresource.Resources; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 56 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 60 | import java.util.HashMap; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 61 | import java.util.Iterator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | import java.util.LinkedList; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 63 | import java.util.List; |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 64 | import java.util.Map; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 65 | import java.util.Set; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 67 | import java.util.stream.Stream; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 68 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 69 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 70 | import static org.onosproject.net.LinkKey.linkKey; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 71 | import static org.slf4j.LoggerFactory.getLogger; |
| 72 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 73 | @Component(immediate = true) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 74 | public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | protected IntentExtensionService intentExtensionService; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected CoreService coreService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 85 | protected ResourceService resourceService; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 86 | |
| 87 | protected ApplicationId appId; |
| 88 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 89 | @Override |
| 90 | public List<Intent> compile(MplsPathIntent intent, List<Intent> installable, |
| 91 | Set<LinkResourceAllocations> resources) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 92 | Map<LinkKey, MplsLabel> labels = assignMplsLabel(intent); |
| 93 | List<FlowRule> rules = generateRules(intent, labels); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 94 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 95 | return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 98 | @Activate |
| 99 | public void activate() { |
| 100 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 101 | intentExtensionService.registerCompiler(MplsPathIntent.class, this); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 106 | intentExtensionService.unregisterCompiler(MplsPathIntent.class); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 109 | private Map<LinkKey, MplsLabel> assignMplsLabel(MplsPathIntent intent) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 110 | // TODO: do it better... Suggestions? |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 111 | Set<LinkKey> linkRequest = Sets.newHashSetWithExpectedSize(intent.path() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 112 | .links().size() - 2); |
| 113 | for (int i = 1; i <= intent.path().links().size() - 2; i++) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 114 | LinkKey link = linkKey(intent.path().links().get(i)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 115 | linkRequest.add(link); |
| 116 | // add the inverse link. I want that the label is reserved both for |
| 117 | // the direct and inverse link |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 118 | linkRequest.add(linkKey(link.dst(), link.src())); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 121 | Map<LinkKey, MplsLabel> labels = findMplsLabels(linkRequest); |
| 122 | if (labels.isEmpty()) { |
| 123 | return Collections.emptyMap(); |
| 124 | } |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 125 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 126 | // for short term solution: same label is used for both directions |
| 127 | // TODO: introduce the concept of Tx and Rx resources of a port |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 128 | Set<Resource> resources = labels.entrySet().stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 129 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 130 | Resources.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue()) |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame] | 131 | .resource(), |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 132 | Resources.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue()) |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame] | 133 | .resource() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 134 | )) |
| 135 | .collect(Collectors.toSet()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 136 | List<org.onosproject.net.newresource.ResourceAllocation> allocations = |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 137 | resourceService.allocate(intent.id(), ImmutableList.copyOf(resources)); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 138 | if (allocations.isEmpty()) { |
| 139 | Collections.emptyMap(); |
| 140 | } |
| 141 | |
| 142 | return labels; |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 145 | private Map<LinkKey, MplsLabel> findMplsLabels(Set<LinkKey> links) { |
| 146 | Map<LinkKey, MplsLabel> labels = new HashMap<>(); |
| 147 | for (LinkKey link : links) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 148 | Set<MplsLabel> forward = findMplsLabel(link.src()); |
| 149 | Set<MplsLabel> backward = findMplsLabel(link.dst()); |
| 150 | Set<MplsLabel> common = Sets.intersection(forward, backward); |
| 151 | if (common.isEmpty()) { |
| 152 | continue; |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 153 | } |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 154 | labels.put(link, common.iterator().next()); |
Sho SHIMIZU | cc0e65d | 2015-10-28 18:38:45 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return labels; |
| 158 | } |
| 159 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 160 | private Set<MplsLabel> findMplsLabel(ConnectPoint cp) { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 161 | return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 162 | .flatMap(x -> Tools.stream(x.valueAs(MplsLabel.class))) |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 163 | .collect(Collectors.toSet()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 166 | private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) { |
| 167 | return labels.get(link); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 170 | private List<FlowRule> generateRules(MplsPathIntent intent, |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 171 | Map<LinkKey, MplsLabel> labels) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 172 | |
| 173 | Iterator<Link> links = intent.path().links().iterator(); |
| 174 | Link srcLink = links.next(); |
| 175 | ConnectPoint prev = srcLink.dst(); |
| 176 | |
| 177 | Link link = links.next(); |
| 178 | // List of flow rules to be installed |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 179 | List<FlowRule> rules = new LinkedList<>(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 180 | |
| 181 | // Ingress traffic |
| 182 | // Get the new MPLS label |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 183 | MplsLabel mpls = getMplsLabel(labels, linkKey(link)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 184 | checkNotNull(mpls); |
| 185 | MplsLabel prevLabel = mpls; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 186 | rules.add(ingressFlow(prev.port(), link, intent, mpls)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 187 | |
| 188 | prev = link.dst(); |
| 189 | |
| 190 | while (links.hasNext()) { |
| 191 | |
| 192 | link = links.next(); |
| 193 | |
| 194 | if (links.hasNext()) { |
| 195 | // Transit traffic |
| 196 | // Get the new MPLS label |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 197 | mpls = getMplsLabel(labels, linkKey(link)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 198 | checkNotNull(mpls); |
| 199 | rules.add(transitFlow(prev.port(), link, intent, |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 200 | prevLabel, mpls)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 201 | prevLabel = mpls; |
| 202 | |
| 203 | } else { |
| 204 | // Egress traffic |
| 205 | rules.add(egressFlow(prev.port(), link, intent, |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 206 | prevLabel)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | prev = link.dst(); |
| 210 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 211 | return rules; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 214 | private FlowRule ingressFlow(PortNumber inPort, Link link, |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 215 | MplsPathIntent intent, |
| 216 | MplsLabel label) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 217 | |
| 218 | TrafficSelector.Builder ingressSelector = DefaultTrafficSelector |
| 219 | .builder(intent.selector()); |
| 220 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 221 | ingressSelector.matchInPort(inPort); |
| 222 | |
| 223 | if (intent.ingressLabel().isPresent()) { |
| 224 | ingressSelector.matchEthType(Ethernet.MPLS_UNICAST) |
| 225 | .matchMplsLabel(intent.ingressLabel().get()); |
| 226 | |
| 227 | // Swap the MPLS label |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 228 | treat.setMpls(label); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 229 | } else { |
| 230 | // Push and set the MPLS label |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 231 | treat.pushMpls().setMpls(label); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 232 | } |
| 233 | // Add the output action |
| 234 | treat.setOutput(link.src().port()); |
| 235 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 236 | return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 239 | private FlowRule transitFlow(PortNumber inPort, Link link, |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 240 | MplsPathIntent intent, |
| 241 | MplsLabel prevLabel, |
| 242 | MplsLabel outLabel) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 243 | |
| 244 | // Ignore the ingress Traffic Selector and use only the MPLS label |
| 245 | // assigned in the previous link |
| 246 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 247 | selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 248 | .matchMplsLabel(prevLabel); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 249 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 250 | |
| 251 | // Set the new label only if the label on the packet is |
| 252 | // different |
Michele Santuari | 316d8cf | 2015-07-14 17:24:54 +0200 | [diff] [blame] | 253 | if (!prevLabel.equals(outLabel)) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 254 | treat.setMpls(outLabel); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | treat.setOutput(link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 258 | return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 261 | private FlowRule egressFlow(PortNumber inPort, Link link, |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 262 | MplsPathIntent intent, |
| 263 | MplsLabel prevLabel) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 264 | // egress point: either set the egress MPLS label or pop the |
| 265 | // MPLS label based on the intent annotations |
| 266 | |
| 267 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 268 | selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 269 | .matchMplsLabel(prevLabel); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 270 | |
| 271 | // apply the intent's treatments |
| 272 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent |
| 273 | .treatment()); |
| 274 | |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 275 | // check if the treatement is popVlan or setVlan (rewrite), |
| 276 | // than selector needs to match any VlanId |
| 277 | for (Instruction instruct : intent.treatment().allInstructions()) { |
| 278 | if (instruct instanceof L2ModificationInstruction) { |
| 279 | L2ModificationInstruction l2Mod = (L2ModificationInstruction) instruct; |
| 280 | if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) { |
| 281 | break; |
| 282 | } |
| 283 | if (l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP || |
| 284 | l2Mod.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) { |
| 285 | selector.matchVlanId(VlanId.ANY); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 290 | if (intent.egressLabel().isPresent()) { |
| 291 | treat.setMpls(intent.egressLabel().get()); |
| 292 | } else { |
Michele Santuari | 316d8cf | 2015-07-14 17:24:54 +0200 | [diff] [blame] | 293 | treat.popMpls(outputEthType(intent.selector())); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 294 | } |
| 295 | treat.setOutput(link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 296 | return createFlowRule(intent, link.src().deviceId(), |
| 297 | selector.build(), treat.build()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 300 | protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId, |
| 301 | TrafficSelector selector, TrafficTreatment treat) { |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 302 | return DefaultFlowRule.builder() |
| 303 | .forDevice(deviceId) |
| 304 | .withSelector(selector) |
| 305 | .withTreatment(treat) |
| 306 | .withPriority(intent.priority()) |
| 307 | .fromApp(appId) |
| 308 | .makePermanent() |
| 309 | .build(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 310 | } |
Michele Santuari | 316d8cf | 2015-07-14 17:24:54 +0200 | [diff] [blame] | 311 | |
| 312 | // if the ingress ethertype is defined, the egress traffic |
| 313 | // will be use that value, otherwise the IPv4 ethertype is used. |
| 314 | private EthType outputEthType(TrafficSelector selector) { |
| 315 | Criterion c = selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 316 | if (c != null && c instanceof EthTypeCriterion) { |
| 317 | EthTypeCriterion ethertype = (EthTypeCriterion) c; |
| 318 | return ethertype.ethType(); |
| 319 | } else { |
| 320 | return EthType.EtherType.IPV4.ethType(); |
| 321 | } |
| 322 | } |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 323 | } |