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