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; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultFlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.FlowRule; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 37 | import org.onosproject.net.flow.criteria.Criteria; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 38 | import org.onosproject.net.flow.criteria.Criterion; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentCompiler; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentExtensionService; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 43 | import org.onosproject.net.intent.MplsPathIntent; |
| 44 | import org.onosproject.net.link.LinkStore; |
| 45 | import org.onosproject.net.resource.DefaultLinkResourceRequest; |
| 46 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 47 | import org.onosproject.net.resource.LinkResourceRequest; |
| 48 | import org.onosproject.net.resource.LinkResourceService; |
| 49 | import org.onosproject.net.resource.MplsLabel; |
| 50 | import org.onosproject.net.resource.MplsLabelResourceAllocation; |
| 51 | import org.onosproject.net.resource.ResourceAllocation; |
| 52 | import org.onosproject.net.resource.ResourceType; |
| 53 | import org.slf4j.Logger; |
| 54 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 55 | import java.util.Arrays; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 56 | import java.util.Iterator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 57 | import java.util.LinkedList; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 58 | import java.util.List; |
| 59 | import java.util.Set; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 60 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 61 | import static com.google.common.base.Preconditions.checkNotNull; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 64 | @Component(immediate = true) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 65 | public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 70 | protected IntentExtensionService intentExtensionService; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected CoreService coreService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected LinkResourceService resourceService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected LinkStore linkStore; |
| 80 | |
| 81 | protected ApplicationId appId; |
| 82 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 83 | @Override |
| 84 | public List<Intent> compile(MplsPathIntent intent, List<Intent> installable, |
| 85 | Set<LinkResourceAllocations> resources) { |
| 86 | LinkResourceAllocations allocations = assignMplsLabel(intent); |
| 87 | List<FlowRule> rules = generateRules(intent, allocations); |
| 88 | |
| 89 | return Arrays.asList(new FlowRuleIntent(appId, rules)); |
| 90 | } |
| 91 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
| 94 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 95 | intentExtensionService.registerCompiler(MplsPathIntent.class, this); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 100 | intentExtensionService.unregisterCompiler(MplsPathIntent.class); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private LinkResourceAllocations assignMplsLabel(MplsPathIntent intent) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 104 | // TODO: do it better... Suggestions? |
| 105 | Set<Link> linkRequest = Sets.newHashSetWithExpectedSize(intent.path() |
| 106 | .links().size() - 2); |
| 107 | for (int i = 1; i <= intent.path().links().size() - 2; i++) { |
| 108 | Link link = intent.path().links().get(i); |
| 109 | linkRequest.add(link); |
| 110 | // add the inverse link. I want that the label is reserved both for |
| 111 | // the direct and inverse link |
| 112 | linkRequest.add(linkStore.getLink(link.dst(), link.src())); |
| 113 | } |
| 114 | |
| 115 | LinkResourceRequest.Builder request = DefaultLinkResourceRequest |
| 116 | .builder(intent.id(), linkRequest).addMplsRequest(); |
| 117 | LinkResourceAllocations reqMpls = resourceService |
| 118 | .requestResources(request.build()); |
| 119 | return reqMpls; |
| 120 | } |
| 121 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 122 | private MplsLabel getMplsLabel(LinkResourceAllocations allocations, Link link) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 123 | for (ResourceAllocation allocation : allocations |
| 124 | .getResourceAllocation(link)) { |
| 125 | if (allocation.type() == ResourceType.MPLS_LABEL) { |
| 126 | return ((MplsLabelResourceAllocation) allocation).mplsLabel(); |
| 127 | |
| 128 | } |
| 129 | } |
| 130 | log.warn("MPLS label was not assigned successfully"); |
| 131 | return null; |
| 132 | } |
| 133 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 134 | private List<FlowRule> generateRules(MplsPathIntent intent, |
| 135 | LinkResourceAllocations allocations) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 136 | |
| 137 | Iterator<Link> links = intent.path().links().iterator(); |
| 138 | Link srcLink = links.next(); |
| 139 | ConnectPoint prev = srcLink.dst(); |
| 140 | |
| 141 | Link link = links.next(); |
| 142 | // List of flow rules to be installed |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 143 | List<FlowRule> rules = new LinkedList<>(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 144 | |
| 145 | // Ingress traffic |
| 146 | // Get the new MPLS label |
| 147 | MplsLabel mpls = getMplsLabel(allocations, link); |
| 148 | checkNotNull(mpls); |
| 149 | MplsLabel prevLabel = mpls; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 150 | rules.add(ingressFlow(prev.port(), link, intent, mpls)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 151 | |
| 152 | prev = link.dst(); |
| 153 | |
| 154 | while (links.hasNext()) { |
| 155 | |
| 156 | link = links.next(); |
| 157 | |
| 158 | if (links.hasNext()) { |
| 159 | // Transit traffic |
| 160 | // Get the new MPLS label |
| 161 | mpls = getMplsLabel(allocations, link); |
| 162 | checkNotNull(mpls); |
| 163 | rules.add(transitFlow(prev.port(), link, intent, |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 164 | prevLabel, mpls)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 165 | prevLabel = mpls; |
| 166 | |
| 167 | } else { |
| 168 | // Egress traffic |
| 169 | rules.add(egressFlow(prev.port(), link, intent, |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 170 | prevLabel)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | prev = link.dst(); |
| 174 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 175 | return rules; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 178 | private FlowRule ingressFlow(PortNumber inPort, Link link, |
| 179 | MplsPathIntent intent, MplsLabel label) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 180 | |
| 181 | TrafficSelector.Builder ingressSelector = DefaultTrafficSelector |
| 182 | .builder(intent.selector()); |
| 183 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 184 | ingressSelector.matchInPort(inPort); |
| 185 | |
| 186 | if (intent.ingressLabel().isPresent()) { |
| 187 | ingressSelector.matchEthType(Ethernet.MPLS_UNICAST) |
| 188 | .matchMplsLabel(intent.ingressLabel().get()); |
| 189 | |
| 190 | // Swap the MPLS label |
| 191 | treat.setMpls(label.label()); |
| 192 | } else { |
| 193 | // Push and set the MPLS label |
| 194 | treat.pushMpls().setMpls(label.label()); |
| 195 | } |
| 196 | // Add the output action |
| 197 | treat.setOutput(link.src().port()); |
| 198 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 199 | return createFlowRule(intent, link.src().deviceId(), ingressSelector.build(), treat.build()); |
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 transitFlow(PortNumber inPort, Link link, |
| 203 | MplsPathIntent intent, |
| 204 | MplsLabel prevLabel, |
| 205 | MplsLabel outLabel) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 206 | |
| 207 | // Ignore the ingress Traffic Selector and use only the MPLS label |
| 208 | // assigned in the previous link |
| 209 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 210 | selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST) |
| 211 | .matchMplsLabel(prevLabel.label()); |
| 212 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 213 | |
| 214 | // Set the new label only if the label on the packet is |
| 215 | // different |
| 216 | if (prevLabel.equals(outLabel)) { |
| 217 | treat.setMpls(outLabel.label()); |
| 218 | } |
| 219 | |
| 220 | treat.setOutput(link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 221 | return createFlowRule(intent, link.src().deviceId(), selector.build(), treat.build()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 224 | private FlowRule egressFlow(PortNumber inPort, Link link, |
| 225 | MplsPathIntent intent, |
| 226 | MplsLabel prevLabel) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 227 | // egress point: either set the egress MPLS label or pop the |
| 228 | // MPLS label based on the intent annotations |
| 229 | |
| 230 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 231 | selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST) |
| 232 | .matchMplsLabel(prevLabel.label()); |
| 233 | |
| 234 | // apply the intent's treatments |
| 235 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent |
| 236 | .treatment()); |
| 237 | |
| 238 | if (intent.egressLabel().isPresent()) { |
| 239 | treat.setMpls(intent.egressLabel().get()); |
| 240 | } else { |
| 241 | // if the ingress ethertype is defined, the egress traffic |
| 242 | // will be use that value, otherwise the IPv4 ethertype is used. |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 243 | Criterion c = intent.selector().getCriterion(Criterion.Type.ETH_TYPE); |
| 244 | if (c != null && c instanceof Criteria.EthTypeCriterion) { |
| 245 | Criteria.EthTypeCriterion ethertype = (Criteria.EthTypeCriterion) c; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 246 | treat.popMpls((short) ethertype.ethType()); |
| 247 | } else { |
| 248 | treat.popMpls(Ethernet.TYPE_IPV4); |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | treat.setOutput(link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 253 | return createFlowRule(intent, link.src().deviceId(), |
| 254 | selector.build(), treat.build()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame^] | 257 | protected FlowRule createFlowRule(MplsPathIntent intent, DeviceId deviceId, |
| 258 | TrafficSelector selector, TrafficTreatment treat) { |
| 259 | return new DefaultFlowRule(deviceId, selector, treat, intent.priority(), appId, 0, true); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 260 | } |
| 261 | } |