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