Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
| 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; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 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; |
| 42 | import org.onosproject.net.newresource.ResourceService; |
| 43 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import com.google.common.collect.ImmutableList; |
| 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 | |
| 67 | private ApplicationId appId; |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame^] | 72 | registrator.registerCompiler(PathIntent.class, this, true); |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Deactivate |
| 76 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame^] | 77 | registrator.unregisterCompiler(PathIntent.class, true); |
Ray Milkey | 661c38c | 2016-02-26 17:12:17 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public List<Intent> compile(PathIntent intent, List<Intent> installable, |
| 82 | Set<LinkResourceAllocations> resources) { |
| 83 | |
| 84 | List<Objective> objectives = new LinkedList<>(); |
| 85 | List<DeviceId> devices = new LinkedList<>(); |
| 86 | compile(this, intent, objectives, devices); |
| 87 | |
| 88 | return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources())); |
| 89 | } |
| 90 | |
| 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<Objective> objectives, |
| 106 | List<DeviceId> devices) { |
| 107 | TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector) |
| 108 | .matchInPort(ingress.port()) |
| 109 | .build(); |
| 110 | |
| 111 | TrafficTreatment.Builder treatmentBuilder; |
| 112 | if (applyTreatment) { |
| 113 | treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment); |
| 114 | } else { |
| 115 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 116 | } |
| 117 | TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build(); |
| 118 | |
| 119 | objectives.add(DefaultForwardingObjective.builder() |
| 120 | .withSelector(selector) |
| 121 | .withTreatment(treatment) |
| 122 | .withPriority(priority) |
| 123 | .fromApp(appId) |
| 124 | .makePermanent() |
| 125 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 126 | .add()); |
| 127 | devices.add(ingress.deviceId()); |
| 128 | } |
| 129 | } |