Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [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.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.DefaultFlowRule; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.intent.FlowRuleIntent; |
| 35 | import org.onosproject.net.intent.Intent; |
| 36 | import org.onosproject.net.intent.IntentCompiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.PathIntent; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 38 | import org.onosproject.net.resource.ResourceService; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 39 | import org.onosproject.net.resource.impl.LabelAllocator; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 40 | import org.slf4j.Logger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 41 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 42 | import java.util.LinkedList; |
| 43 | import java.util.List; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 44 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 45 | import static org.slf4j.LoggerFactory.getLogger; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 46 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 47 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | @Component(immediate = true) |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 49 | public class PathIntentCompiler |
| 50 | extends PathCompiler<FlowRule> |
| 51 | implements IntentCompiler<PathIntent>, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 52 | PathCompiler.PathCompilerCreateFlow<FlowRule> { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 53 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 60 | protected IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 61 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected ResourceService resourceService; |
| 64 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 65 | private ApplicationId appId; |
| 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 70 | registrator.registerCompiler(PathIntent.class, this, false); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 71 | labelAllocator = new LabelAllocator(resourceService); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 76 | registrator.unregisterCompiler(PathIntent.class, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 80 | public List<Intent> compile(PathIntent intent, List<Intent> installable) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 81 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 82 | List<FlowRule> rules = new LinkedList<>(); |
| 83 | List<DeviceId> devices = new LinkedList<>(); |
| 84 | compile(this, intent, rules, devices); |
Sho SHIMIZU | e8f656c | 2016-02-24 14:05:17 -0800 | [diff] [blame] | 85 | |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 86 | |
| 87 | return ImmutableList.of(new FlowRuleIntent(appId, |
| 88 | intent.key(), |
| 89 | rules, |
| 90 | intent.resources(), |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame^] | 91 | intent.type(), |
| 92 | intent.resourceGroup() |
| 93 | )); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public Logger log() { |
| 98 | return log; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public ResourceService resourceService() { |
| 103 | return resourceService; |
| 104 | } |
| 105 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 106 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 107 | @Override |
| 108 | public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment, |
| 109 | ConnectPoint ingress, ConnectPoint egress, |
| 110 | int priority, boolean applyTreatment, |
| 111 | List<FlowRule> rules, |
| 112 | List<DeviceId> devices) { |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 113 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 114 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 115 | .matchInPort(ingress.port()) |
| 116 | .build(); |
| 117 | |
| 118 | TrafficTreatment.Builder treatmentBuilder; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 119 | if (applyTreatment) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 120 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 121 | } else { |
| 122 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 123 | } |
| 124 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 125 | |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 126 | rules.add(DefaultFlowRule.builder() |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 127 | .forDevice(ingress.deviceId()) |
| 128 | .withSelector(selector) |
| 129 | .withTreatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 130 | .withPriority(priority) |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 131 | .fromApp(appId) |
| 132 | .makePermanent() |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 133 | .build()); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 134 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 135 | } |
| 136 | } |