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