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