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 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 20 | |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.Objective; |
| 37 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.PathIntent; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.ResourceService; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import com.google.common.collect.ImmutableList; |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | @Component(immediate = true) |
| 49 | public class PathIntentFlowObjectiveCompiler |
| 50 | extends PathCompiler<Objective> |
| 51 | implements IntentCompiler<PathIntent>, |
| 52 | PathCompiler.PathCompilerCreateFlow<Objective> { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 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; |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected ResourceService resourceService; |
| 64 | |
| 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, true); |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Deactivate |
| 74 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 75 | registrator.unregisterCompiler(PathIntent.class, true); |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 79 | public List<Intent> compile(PathIntent intent, List<Intent> installable) { |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 80 | |
| 81 | List<Objective> objectives = new LinkedList<>(); |
| 82 | List<DeviceId> devices = new LinkedList<>(); |
| 83 | compile(this, intent, objectives, devices); |
| 84 | |
| 85 | return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources())); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Logger log() { |
| 90 | return log; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public ResourceService resourceService() { |
| 95 | return resourceService; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment, |
| 100 | ConnectPoint ingress, ConnectPoint egress, |
| 101 | int priority, boolean applyTreatment, |
| 102 | List<Objective> objectives, |
| 103 | List<DeviceId> devices) { |
| 104 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 105 | .matchInPort(ingress.port()) |
| 106 | .build(); |
| 107 | |
| 108 | TrafficTreatment.Builder treatmentBuilder; |
| 109 | if (applyTreatment) { |
| 110 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 111 | } else { |
| 112 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 113 | } |
| 114 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 115 | |
| 116 | objectives.add(DefaultForwardingObjective.builder() |
| 117 | .withSelector(selector) |
| 118 | .withTreatment(treatment) |
| 119 | .withPriority(priority) |
| 120 | .fromApp(appId) |
| 121 | .makePermanent() |
Thomas Vachuska | 8378ccf | 2016-03-02 18:02:17 -0800 | [diff] [blame] | 122 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 123 | .add()); |
| 124 | devices.add(ingress.deviceId()); |
| 125 | } |
| 126 | } |