alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.driver.pipeline; |
| 17 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.ServiceDirectory; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 21 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.DefaultFlowRule; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.FlowRule; |
| 27 | import org.onosproject.net.flow.FlowRuleOperations; |
| 28 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.flow.instructions.Instructions; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 34 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Simple single table pipeline abstraction. |
| 44 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 45 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 46 | |
| 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | private ServiceDirectory serviceDirectory; |
| 50 | private FlowRuleService flowRuleService; |
| 51 | private DeviceId deviceId; |
| 52 | |
| 53 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 54 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 55 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 56 | this.deviceId = deviceId; |
| 57 | |
| 58 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 62 | public void filter(FilteringObjective filter) { |
| 63 | |
| 64 | TrafficTreatment.Builder actions; |
| 65 | switch (filter.type()) { |
| 66 | case PERMIT: |
| 67 | actions = (filter.meta() == null) ? |
| 68 | DefaultTrafficTreatment.builder().punt() : |
| 69 | DefaultTrafficTreatment.builder(filter.meta()); |
| 70 | break; |
| 71 | case DENY: |
| 72 | actions = (filter.meta() == null) ? |
| 73 | DefaultTrafficTreatment.builder() : |
| 74 | DefaultTrafficTreatment.builder(filter.meta()); |
| 75 | actions.drop(); |
| 76 | break; |
| 77 | default: |
| 78 | log.warn("Unknown filter type: {}", filter.type()); |
| 79 | actions = DefaultTrafficTreatment.builder().drop(); |
| 80 | } |
| 81 | |
| 82 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 83 | |
| 84 | filter.conditions().stream().forEach(selector::add); |
| 85 | |
| 86 | if (filter.key() != null) { |
| 87 | selector.add(filter.key()); |
| 88 | } |
| 89 | |
| 90 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 91 | .forDevice(deviceId) |
| 92 | .withSelector(selector.build()) |
| 93 | .withTreatment(actions.build()) |
| 94 | .fromApp(filter.appId()) |
| 95 | .withPriority(filter.priority()); |
| 96 | |
| 97 | if (filter.permanent()) { |
| 98 | ruleBuilder.makePermanent(); |
| 99 | } else { |
| 100 | ruleBuilder.makeTemporary(filter.timeout()); |
| 101 | } |
| 102 | |
| 103 | installObjective(ruleBuilder, filter); |
| 104 | |
| 105 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 106 | |
| 107 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 108 | public void forward(ForwardingObjective fwd) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 109 | if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) { |
| 110 | throw new UnsupportedOperationException( |
| 111 | "Only VERSATILE is supported."); |
| 112 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 113 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 114 | TrafficSelector selector = fwd.selector(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 115 | TrafficTreatment treatment = fwd.treatment(); |
| 116 | if ((fwd.treatment().deferred().size() == 0) && |
| 117 | (fwd.treatment().immediate().size() == 0) && |
| 118 | (fwd.treatment().tableTransition() == null) && |
| 119 | (!fwd.treatment().clearedDeferred())) { |
| 120 | TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder(); |
Ray Milkey | 2be39ed | 2016-02-22 15:54:19 -0800 | [diff] [blame^] | 121 | flowTreatment.add(Instructions.createNoAction()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 122 | treatment = flowTreatment.build(); |
| 123 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 124 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 125 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 126 | .forDevice(deviceId) |
| 127 | .withSelector(selector) |
Charles M.C. Chan | ef3f39e | 2015-07-14 22:21:46 +0800 | [diff] [blame] | 128 | .withTreatment(treatment) |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 129 | .fromApp(fwd.appId()) |
| 130 | .withPriority(fwd.priority()); |
| 131 | |
| 132 | if (fwd.permanent()) { |
| 133 | ruleBuilder.makePermanent(); |
| 134 | } else { |
| 135 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 136 | } |
| 137 | |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 138 | installObjective(ruleBuilder, fwd); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 139 | |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 143 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 144 | switch (objective.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 145 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 146 | case ADD: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 147 | flowBuilder.add(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 148 | break; |
| 149 | case REMOVE: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 150 | flowBuilder.remove(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 151 | break; |
| 152 | default: |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 153 | log.warn("Unknown operation {}", objective.op()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 154 | } |
| 155 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 156 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 157 | @Override |
| 158 | public void onSuccess(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 159 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void onError(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 164 | objective.context() |
| 165 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 166 | } |
| 167 | })); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 168 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 169 | |
| 170 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 171 | public void next(NextObjective nextObjective) { |
| 172 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 173 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 174 | } |