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