alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [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.driver.pipeline; |
| 17 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 23 | import org.onlab.util.KryoNamespace; |
Daniele Moro | 607fd0b | 2021-07-19 22:39:22 +0200 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultFlowRule; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleOperations; |
| 35 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.FilteringObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.ObjectiveError; |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 45 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 48 | import java.util.Collection; |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 49 | import java.util.Collections; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 50 | import java.util.List; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 51 | import java.util.concurrent.TimeUnit; |
| 52 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 53 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 54 | import static org.slf4j.LoggerFactory.getLogger; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 55 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Simple single table pipeline abstraction. |
| 58 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 59 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | private ServiceDirectory serviceDirectory; |
| 64 | private FlowRuleService flowRuleService; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 65 | private FlowObjectiveStore flowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 66 | private DeviceId deviceId; |
| 67 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 68 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 69 | .register(KryoNamespaces.API) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 70 | .register(SingleGroup.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 71 | .build("DefaultSingleTablePipeline"); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 72 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 73 | // Fast path for the installation. If we don't find the nextobjective in |
| 74 | // the cache, as fallback mechanism we will try to retrieve the treatments |
| 75 | // from the store. It is safe to use this cache for the addition, while it |
| 76 | // should be avoided for the removal. This cache from Guava does not offer |
| 77 | // thread-safe read (the read does not take the lock). |
| 78 | private Cache<Integer, NextObjective> pendingAddNext = CacheBuilder.newBuilder() |
| 79 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 80 | .removalListener((RemovalNotification<Integer, NextObjective> notification) -> { |
| 81 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 82 | notification.getValue().context() |
| 83 | .ifPresent(c -> c.onError(notification.getValue(), |
| 84 | ObjectiveError.FLOWINSTALLATIONFAILED)); |
| 85 | } |
| 86 | }).build(); |
| 87 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 88 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 89 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 90 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 91 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 92 | this.deviceId = deviceId; |
| 93 | |
| 94 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 95 | flowObjectiveStore = serviceDirectory.get(FlowObjectiveStore.class); |
| 96 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 100 | public void filter(FilteringObjective filter) { |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 101 | TrafficTreatment.Builder actions; |
| 102 | switch (filter.type()) { |
| 103 | case PERMIT: |
| 104 | actions = (filter.meta() == null) ? |
| 105 | DefaultTrafficTreatment.builder().punt() : |
| 106 | DefaultTrafficTreatment.builder(filter.meta()); |
| 107 | break; |
| 108 | case DENY: |
| 109 | actions = (filter.meta() == null) ? |
| 110 | DefaultTrafficTreatment.builder() : |
| 111 | DefaultTrafficTreatment.builder(filter.meta()); |
| 112 | actions.drop(); |
| 113 | break; |
| 114 | default: |
| 115 | log.warn("Unknown filter type: {}", filter.type()); |
| 116 | actions = DefaultTrafficTreatment.builder().drop(); |
| 117 | } |
| 118 | |
| 119 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 120 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 121 | filter.conditions().forEach(selector::add); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 122 | |
| 123 | if (filter.key() != null) { |
| 124 | selector.add(filter.key()); |
| 125 | } |
| 126 | |
| 127 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 128 | .forDevice(deviceId) |
| 129 | .withSelector(selector.build()) |
| 130 | .withTreatment(actions.build()) |
| 131 | .fromApp(filter.appId()) |
| 132 | .withPriority(filter.priority()); |
| 133 | |
| 134 | if (filter.permanent()) { |
| 135 | ruleBuilder.makePermanent(); |
| 136 | } else { |
| 137 | ruleBuilder.makeTemporary(filter.timeout()); |
| 138 | } |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 139 | installObjective(ruleBuilder, filter); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 140 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 141 | |
| 142 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 143 | public void forward(ForwardingObjective fwd) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 144 | TrafficSelector selector = fwd.selector(); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 145 | if (fwd.treatment() != null) { |
| 146 | // Deal with SPECIFIC and VERSATILE in the same manner. |
| 147 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 148 | .forDevice(deviceId) |
| 149 | .withSelector(selector) |
| 150 | .fromApp(fwd.appId()) |
| 151 | .withPriority(fwd.priority()) |
| 152 | .withTreatment(fwd.treatment()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 153 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 154 | if (fwd.permanent()) { |
| 155 | ruleBuilder.makePermanent(); |
| 156 | } else { |
| 157 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 158 | } |
| 159 | installObjective(ruleBuilder, fwd); |
| 160 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 161 | } else { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 162 | NextObjective nextObjective; |
| 163 | NextGroup next; |
| 164 | TrafficTreatment treatment; |
| 165 | if (fwd.op() == ADD) { |
| 166 | // Give a try to the cache. Doing an operation |
| 167 | // on the store seems to be very expensive. |
| 168 | nextObjective = pendingAddNext.getIfPresent(fwd.nextId()); |
| 169 | // If the next objective is not present |
| 170 | // We will try with the store |
| 171 | if (nextObjective == null) { |
| 172 | next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 173 | // We verify that next was in the store and then de-serialize |
| 174 | // the treatment in order to re-build the flow rule. |
| 175 | if (next == null) { |
| 176 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 177 | return; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 178 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 179 | treatment = appKryo.deserialize(next.data()); |
| 180 | } else { |
| 181 | pendingAddNext.invalidate(fwd.nextId()); |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 182 | treatment = getTreatment(nextObjective); |
| 183 | if (treatment == null) { |
| 184 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.UNSUPPORTED)); |
| 185 | return; |
| 186 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 187 | } |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 188 | } else { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 189 | // We get the NextGroup from the remove operation. |
| 190 | // Doing an operation on the store seems to be very expensive. |
Yi Tseng | 32c053c | 2017-05-15 14:17:34 -0700 | [diff] [blame] | 191 | next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 192 | treatment = (next != null) ? appKryo.deserialize(next.data()) : null; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 193 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 194 | // If the treatment is null we cannot re-build the original flow |
| 195 | if (treatment == null) { |
| 196 | fwd.context().ifPresent(c -> c.onError(fwd, ObjectiveError.GROUPMISSING)); |
| 197 | return; |
| 198 | } |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 199 | // Finally we build the flow rule and push to the flow rule subsystem. |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 200 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 201 | .forDevice(deviceId) |
| 202 | .withSelector(selector) |
| 203 | .fromApp(fwd.appId()) |
| 204 | .withPriority(fwd.priority()) |
| 205 | .withTreatment(treatment); |
| 206 | if (fwd.permanent()) { |
| 207 | ruleBuilder.makePermanent(); |
| 208 | } else { |
| 209 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 210 | } |
| 211 | installObjective(ruleBuilder, fwd); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 212 | } |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 216 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 217 | switch (objective.op()) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 218 | case ADD: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 219 | flowBuilder.add(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 220 | break; |
| 221 | case REMOVE: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 222 | flowBuilder.remove(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 223 | break; |
| 224 | default: |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 225 | log.warn("Unknown operation {}", objective.op()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 226 | } |
| 227 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 228 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 229 | @Override |
| 230 | public void onSuccess(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 231 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onError(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 236 | objective.context() |
| 237 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 238 | } |
| 239 | })); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 240 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 241 | |
| 242 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 243 | public void next(NextObjective nextObjective) { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 244 | switch (nextObjective.op()) { |
| 245 | case ADD: |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 246 | // Check next objective |
| 247 | TrafficTreatment treatment = getTreatment(nextObjective); |
| 248 | if (treatment == null) { |
| 249 | // unsupported next objective |
| 250 | nextObjective.context().ifPresent(context -> context.onError(nextObjective, |
| 251 | ObjectiveError.UNSUPPORTED)); |
| 252 | return; |
| 253 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 254 | // We insert the value in the cache |
| 255 | pendingAddNext.put(nextObjective.id(), nextObjective); |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 256 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 257 | // Then in the store, this will unblock the queued fwd obj |
| 258 | flowObjectiveStore.putNextGroup( |
| 259 | nextObjective.id(), |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 260 | new SingleGroup(treatment) |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 261 | ); |
| 262 | break; |
| 263 | case REMOVE: |
Yi Tseng | 32c053c | 2017-05-15 14:17:34 -0700 | [diff] [blame] | 264 | NextGroup next = flowObjectiveStore.removeNextGroup(nextObjective.id()); |
| 265 | if (next == null) { |
| 266 | nextObjective.context().ifPresent(context -> context.onError(nextObjective, |
| 267 | ObjectiveError.GROUPMISSING)); |
| 268 | return; |
| 269 | } |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 270 | break; |
| 271 | default: |
| 272 | log.warn("Unsupported operation {}", nextObjective.op()); |
| 273 | } |
Ray Milkey | ce7db1b | 2016-05-24 13:33:48 -0700 | [diff] [blame] | 274 | nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective)); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 275 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 276 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 277 | @Override |
Daniele Moro | 607fd0b | 2021-07-19 22:39:22 +0200 | [diff] [blame] | 278 | public void purgeAll(ApplicationId appId) { |
| 279 | flowRuleService.purgeFlowRules(deviceId, appId); |
| 280 | } |
| 281 | |
| 282 | @Override |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 283 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 284 | // Default single table pipeline does not use nextObjectives or groups |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 285 | return Collections.emptyList(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Yi Tseng | e935a64 | 2017-06-19 15:19:07 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Gets traffic treatment from a next objective. |
| 290 | * Merge traffic treatments from next objective if the next objective is |
| 291 | * BROADCAST type and contains multiple traffic treatments. |
| 292 | * Returns first treatment from next objective if the next objective is |
| 293 | * SIMPLE type and it contains only one treatment. |
| 294 | * |
| 295 | * @param nextObjective the next objective |
| 296 | * @return the treatment from next objective; null if not supported |
| 297 | */ |
| 298 | private TrafficTreatment getTreatment(NextObjective nextObjective) { |
| 299 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 300 | switch (nextObjective.type()) { |
| 301 | case SIMPLE: |
| 302 | if (treatments.size() != 1) { |
| 303 | log.error("Next Objectives of type SIMPLE should have only " + |
| 304 | "one traffic treatment. NexObjective: {}", |
| 305 | nextObjective.toString()); |
| 306 | return null; |
| 307 | } |
| 308 | return treatments.iterator().next(); |
| 309 | case BROADCAST: |
| 310 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 311 | treatments.forEach(builder::addTreatment); |
| 312 | return builder.build(); |
| 313 | default: |
| 314 | log.error("Unsupported next objective type {}.", nextObjective.type()); |
| 315 | return null; |
| 316 | } |
| 317 | } |
| 318 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 319 | private class SingleGroup implements NextGroup { |
| 320 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 321 | private TrafficTreatment nextActions; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 322 | |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 323 | SingleGroup(TrafficTreatment next) { |
| 324 | this.nextActions = next; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public byte[] data() { |
Pier Luigi | b69b6cc | 2017-01-10 15:13:14 -0800 | [diff] [blame] | 329 | return appKryo.serialize(nextActions); |
| 330 | } |
| 331 | |
| 332 | public TrafficTreatment treatment() { |
| 333 | return nextActions; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 337 | } |