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 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.intent.FlowRuleIntent; |
| 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
| 40 | import org.onosproject.net.intent.IntentExtensionService; |
| 41 | import org.onosproject.net.intent.PathIntent; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 42 | import org.onosproject.net.newresource.ResourceService; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 43 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 44 | import org.slf4j.Logger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 45 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 46 | import com.google.common.collect.ImmutableList; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 47 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | |
| 50 | @Component(immediate = true) |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 51 | public class PathIntentCompiler |
| 52 | extends PathCompiler<FlowRule> |
| 53 | implements IntentCompiler<PathIntent>, |
| 54 | PathCompiler.PathCompilerCreateFlow<FlowRule> { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 55 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IntentExtensionService intentManager; |
| 63 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected ResourceService resourceService; |
| 66 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 67 | private ApplicationId appId; |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 72 | intentManager.registerCompiler(PathIntent.class, this); |
| 73 | } |
| 74 | |
| 75 | @Deactivate |
| 76 | public void deactivate() { |
| 77 | intentManager.unregisterCompiler(PathIntent.class); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public List<Intent> compile(PathIntent intent, List<Intent> installable, |
| 82 | Set<LinkResourceAllocations> resources) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 84 | List<FlowRule> rules = new LinkedList<>(); |
| 85 | List<DeviceId> devices = new LinkedList<>(); |
| 86 | compile(this, intent, rules, devices); |
Sho SHIMIZU | e8f656c | 2016-02-24 14:05:17 -0800 | [diff] [blame] | 87 | |
| 88 | return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 91 | @Override |
| 92 | public Logger log() { |
| 93 | return log; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public ResourceService resourceService() { |
| 98 | return resourceService; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment, |
| 103 | ConnectPoint ingress, ConnectPoint egress, |
| 104 | int priority, boolean applyTreatment, |
| 105 | List<FlowRule> rules, |
| 106 | List<DeviceId> devices) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 107 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 108 | .matchInPort(ingress.port()) |
| 109 | .build(); |
| 110 | |
| 111 | TrafficTreatment.Builder treatmentBuilder; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 112 | if (applyTreatment) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 113 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 114 | } else { |
| 115 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 116 | } |
| 117 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 118 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 119 | rules.add(DefaultFlowRule.builder() |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 120 | .forDevice(ingress.deviceId()) |
| 121 | .withSelector(selector) |
| 122 | .withTreatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 123 | .withPriority(priority) |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 124 | .fromApp(appId) |
| 125 | .makePermanent() |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 126 | .build()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 127 | } |
| 128 | } |