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; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.intent.FlowRuleIntent; |
| 34 | import org.onosproject.net.intent.Intent; |
| 35 | import org.onosproject.net.intent.IntentCompiler; |
| 36 | import org.onosproject.net.intent.IntentExtensionService; |
| 37 | import org.onosproject.net.intent.PathIntent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 38 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 41 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | @Component(immediate = true) |
| 46 | public class PathIntentCompiler implements IntentCompiler<PathIntent> { |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected CoreService coreService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected IntentExtensionService intentManager; |
| 53 | |
| 54 | private ApplicationId appId; |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 59 | intentManager.registerCompiler(PathIntent.class, this); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
| 64 | intentManager.unregisterCompiler(PathIntent.class); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public List<Intent> compile(PathIntent intent, List<Intent> installable, |
| 69 | Set<LinkResourceAllocations> resources) { |
| 70 | // Note: right now recompile is not considered |
| 71 | // TODO: implement recompile behavior |
| 72 | |
| 73 | List<Link> links = intent.path().links(); |
| 74 | List<FlowRule> rules = new ArrayList<>(links.size() - 1); |
| 75 | |
| 76 | for (int i = 0; i < links.size() - 1; i++) { |
| 77 | ConnectPoint ingress = links.get(i).dst(); |
| 78 | ConnectPoint egress = links.get(i + 1).src(); |
| 79 | FlowRule rule = createFlowRule(intent.selector(), intent.treatment(), ingress, egress, isLast(links, i)); |
| 80 | rules.add(rule); |
| 81 | } |
| 82 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 83 | return Collections.singletonList(new FlowRuleIntent(appId, null, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private FlowRule createFlowRule(TrafficSelector originalSelector, TrafficTreatment originalTreatment, |
| 87 | ConnectPoint ingress, ConnectPoint egress, boolean last) { |
| 88 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 89 | .matchInPort(ingress.port()) |
| 90 | .build(); |
| 91 | |
| 92 | TrafficTreatment.Builder treatmentBuilder; |
| 93 | if (last) { |
| 94 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 95 | } else { |
| 96 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 97 | } |
| 98 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 99 | |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 100 | return DefaultFlowRule.builder() |
| 101 | .forDevice(ingress.deviceId()) |
| 102 | .withSelector(selector) |
| 103 | .withTreatment(treatment) |
| 104 | .withPriority(123) |
| 105 | .fromApp(appId) |
| 106 | .makePermanent() |
| 107 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private boolean isLast(List<Link> links, int i) { |
| 111 | return i == links.size() - 2; |
| 112 | } |
| 113 | } |