blob: 3fceb5db6823f49a2aaebce5f8dbb8b8037feef0 [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.pipelines.fabric.pipeliner;
18
19import com.google.common.collect.ImmutableList;
Yi Tseng1b154bd2017-11-20 17:48:19 -080020import com.google.common.collect.ImmutableMap;
Yi Tseng0b809722017-11-03 10:23:26 -070021import com.google.common.collect.Lists;
22import org.onlab.util.KryoNamespace;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.behaviour.NextGroup;
26import org.onosproject.net.behaviour.Pipeliner;
27import org.onosproject.net.behaviour.PipelinerContext;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.FlowRuleOperations;
31import org.onosproject.net.flow.FlowRuleOperationsContext;
32import org.onosproject.net.flow.FlowRuleService;
33import org.onosproject.net.flow.instructions.Instruction;
34import org.onosproject.net.flow.instructions.Instructions;
35import org.onosproject.net.flowobjective.FilteringObjective;
36import org.onosproject.net.flowobjective.FlowObjectiveStore;
37import org.onosproject.net.flowobjective.ForwardingObjective;
38import org.onosproject.net.flowobjective.NextObjective;
39import org.onosproject.net.flowobjective.Objective;
40import org.onosproject.net.flowobjective.ObjectiveError;
Yi Tseng1b154bd2017-11-20 17:48:19 -080041import org.onosproject.net.group.Group;
Yi Tseng0b809722017-11-03 10:23:26 -070042import org.onosproject.net.group.GroupDescription;
43import org.onosproject.net.group.GroupEvent;
44import org.onosproject.net.group.GroupListener;
45import org.onosproject.net.group.GroupService;
46import org.onosproject.store.serializers.KryoNamespaces;
47import org.slf4j.Logger;
48
49import java.util.Collection;
50import java.util.List;
Yi Tseng1b154bd2017-11-20 17:48:19 -080051import java.util.Map;
Yi Tseng0b809722017-11-03 10:23:26 -070052import java.util.concurrent.CompletableFuture;
53import java.util.concurrent.ExecutionException;
54import java.util.concurrent.TimeUnit;
55import java.util.concurrent.TimeoutException;
56import java.util.concurrent.atomic.AtomicInteger;
57import java.util.function.Consumer;
Yi Tseng1b154bd2017-11-20 17:48:19 -080058import java.util.stream.Collectors;
Yi Tseng0b809722017-11-03 10:23:26 -070059
60import static org.slf4j.LoggerFactory.getLogger;
61
62/**
63 * Pipeliner for fabric pipeline.
64 */
65public class FabricPipeliner extends AbstractHandlerBehaviour implements Pipeliner {
66 private static final Logger log = getLogger(FabricPipeliner.class);
67
68 protected static final KryoNamespace KRYO = new KryoNamespace.Builder()
69 .register(KryoNamespaces.API)
70 .register(FabricNextGroup.class)
71 .build("FabricPipeliner");
72
73 // TODO: make this configurable
Yi Tseng1b154bd2017-11-20 17:48:19 -080074 private static final long DEFAULT_INSTALLATION_TIME_OUT = 40;
75 private static final Map<Objective.Operation, GroupEvent.Type> OBJ_OP_TO_GRP_EVENT_TYPE =
76 ImmutableMap.<Objective.Operation, GroupEvent.Type>builder()
77 .put(Objective.Operation.ADD, GroupEvent.Type.GROUP_ADDED)
78 .put(Objective.Operation.ADD_TO_EXISTING, GroupEvent.Type.GROUP_UPDATED)
79 .put(Objective.Operation.REMOVE, GroupEvent.Type.GROUP_REMOVED)
80 .put(Objective.Operation.REMOVE_FROM_EXISTING, GroupEvent.Type.GROUP_UPDATED)
81 .build();
Yi Tseng0b809722017-11-03 10:23:26 -070082
83 protected DeviceId deviceId;
84 protected FlowRuleService flowRuleService;
85 protected GroupService groupService;
86 protected FlowObjectiveStore flowObjectiveStore;
87 protected FabricFilteringPipeliner pipelinerFilter;
88 protected FabricForwardingPipeliner pipelinerForward;
89 protected FabricNextPipeliner pipelinerNext;
90
91
92 @Override
93 public void init(DeviceId deviceId, PipelinerContext context) {
94 this.deviceId = deviceId;
95 this.flowRuleService = context.directory().get(FlowRuleService.class);
96 this.groupService = context.directory().get(GroupService.class);
97 this.flowObjectiveStore = context.directory().get(FlowObjectiveStore.class);
98 this.pipelinerFilter = new FabricFilteringPipeliner(deviceId);
99 this.pipelinerForward = new FabricForwardingPipeliner(deviceId);
100 this.pipelinerNext = new FabricNextPipeliner(deviceId);
101 }
102
103 @Override
104 public void filter(FilteringObjective filterObjective) {
105 PipelinerTranslationResult result = pipelinerFilter.filter(filterObjective);
106 if (result.error().isPresent()) {
107 fail(filterObjective, result.error().get());
108 return;
109 }
110
111 applyTranslationResult(filterObjective, result, success -> {
112 if (success) {
113 success(filterObjective);
114 } else {
115 fail(filterObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
116 }
117 });
118 }
119
120 @Override
121 public void forward(ForwardingObjective forwardObjective) {
122 PipelinerTranslationResult result = pipelinerForward.forward(forwardObjective);
123 if (result.error().isPresent()) {
124 fail(forwardObjective, result.error().get());
125 return;
126 }
127
128 applyTranslationResult(forwardObjective, result, success -> {
129 if (success) {
130 success(forwardObjective);
131 } else {
132 fail(forwardObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
133 }
134 });
135 }
136
137 @Override
138 public void next(NextObjective nextObjective) {
139 PipelinerTranslationResult result = pipelinerNext.next(nextObjective);
140
141 if (result.error().isPresent()) {
142 fail(nextObjective, result.error().get());
143 return;
144 }
145
Yi Tseng1b154bd2017-11-20 17:48:19 -0800146 if (nextObjective.op() == Objective.Operation.VERIFY) {
147 // TODO: support VERIFY operation
148 log.debug("Currently we don't support VERIFY operation, return success directly to the context");
149 success(nextObjective);
150 return;
151 }
152
Yi Tseng0b809722017-11-03 10:23:26 -0700153 applyTranslationResult(nextObjective, result, success -> {
154 if (!success) {
155 fail(nextObjective, ObjectiveError.GROUPINSTALLATIONFAILED);
156 return;
157 }
158
159 // Success, put next group to objective store
160 List<PortNumber> portNumbers = Lists.newArrayList();
161 nextObjective.next().forEach(treatment -> {
162 Instructions.OutputInstruction outputInst = treatment.allInstructions()
163 .stream()
164 .filter(inst -> inst.type() == Instruction.Type.OUTPUT)
165 .map(inst -> (Instructions.OutputInstruction) inst)
166 .findFirst()
167 .orElse(null);
168
169 if (outputInst != null) {
170 portNumbers.add(outputInst.port());
171 }
172 });
173 FabricNextGroup nextGroup = new FabricNextGroup(nextObjective.type(),
174 portNumbers);
175 flowObjectiveStore.putNextGroup(nextObjective.id(), nextGroup);
176 success(nextObjective);
177 });
178 }
179
180 @Override
181 public List<String> getNextMappings(NextGroup nextGroup) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800182 FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data());
183 NextObjective.Type type = fabricNextGroup.type();
184 Collection<PortNumber> outputPorts = fabricNextGroup.outputPorts();
185
186 return outputPorts.stream()
187 .map(port -> String.format("%s -> %s", type, port))
188 .collect(Collectors.toList());
Yi Tseng0b809722017-11-03 10:23:26 -0700189 }
190
191 private void applyTranslationResult(Objective objective,
192 PipelinerTranslationResult result,
193 Consumer<Boolean> callback) {
194 Collection<GroupDescription> groups = result.groups();
195 Collection<FlowRule> flowRules = result.flowRules();
196 CompletableFuture.supplyAsync(() -> installGroups(objective, groups))
197 .thenApplyAsync(groupSuccess -> groupSuccess && installFlows(objective, flowRules))
198 .thenAcceptAsync(callback)
199 .exceptionally((ex) -> {
200 log.warn("Got unexpected exception while applying translation result {}",
201 result);
202 fail(objective, ObjectiveError.UNKNOWN);
203 return null;
204 });
205 }
206
207 private boolean installFlows(Objective objective, Collection<FlowRule> flowRules) {
208 if (flowRules.isEmpty()) {
209 return true;
210 }
211 CompletableFuture<Boolean> flowInstallFuture = new CompletableFuture<>();
212 FlowRuleOperationsContext ctx = new FlowRuleOperationsContext() {
213 @Override
214 public void onSuccess(FlowRuleOperations ops) {
215 flowInstallFuture.complete(true);
216 }
217
218 @Override
219 public void onError(FlowRuleOperations ops) {
220 log.warn("Failed to install flow rules: {}", flowRules);
221 flowInstallFuture.complete(false);
222 }
223 };
224
225 FlowRuleOperations ops = buildFlowRuleOps(objective, flowRules, ctx);
226 flowRuleService.apply(ops);
227
228 try {
229 return flowInstallFuture.get(DEFAULT_INSTALLATION_TIME_OUT, TimeUnit.SECONDS);
230 } catch (InterruptedException | ExecutionException | TimeoutException e) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800231 log.warn("Got exception while installing flows:{}", e.getMessage());
Yi Tseng0b809722017-11-03 10:23:26 -0700232 return false;
233 }
234 }
235
236 private boolean installGroups(Objective objective, Collection<GroupDescription> groups) {
237 if (groups.isEmpty()) {
238 return true;
239 }
Yi Tseng1b154bd2017-11-20 17:48:19 -0800240 Collection<Integer> groupIds = groups.stream()
241 .map(GroupDescription::givenGroupId)
242 .collect(Collectors.toSet());
243
Yi Tseng0b809722017-11-03 10:23:26 -0700244 int numGroupsToBeInstalled = groups.size();
245 CompletableFuture<Boolean> groupInstallFuture = new CompletableFuture<>();
246 AtomicInteger numGroupsInstalled = new AtomicInteger(0);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800247
Yi Tseng0b809722017-11-03 10:23:26 -0700248 GroupListener listener = new GroupListener() {
249 @Override
250 public void event(GroupEvent event) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800251 log.debug("Receive group event for group {}", event.subject());
Yi Tseng0b809722017-11-03 10:23:26 -0700252 int currentNumGroupInstalled = numGroupsInstalled.incrementAndGet();
253 if (currentNumGroupInstalled == numGroupsToBeInstalled) {
254 // install completed
255 groupService.removeListener(this);
256 groupInstallFuture.complete(true);
257 }
258 }
259 @Override
260 public boolean isRelevant(GroupEvent event) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800261 Group group = event.subject();
262 return groupIds.contains(group.givenGroupId());
Yi Tseng0b809722017-11-03 10:23:26 -0700263 }
264 };
Yi Tseng1b154bd2017-11-20 17:48:19 -0800265
Yi Tseng0b809722017-11-03 10:23:26 -0700266 groupService.addListener(listener);
267
268 switch (objective.op()) {
269 case ADD:
270 groups.forEach(groupService::addGroup);
271 break;
272 case REMOVE:
273 groups.forEach(group -> groupService.removeGroup(deviceId, group.appCookie(), objective.appId()));
274 break;
Yi Tseng1b154bd2017-11-20 17:48:19 -0800275 case ADD_TO_EXISTING:
276 groups.forEach(group -> {
277 groupService.addBucketsToGroup(deviceId, group.appCookie(),
278 group.buckets(),
279 group.appCookie(),
280 group.appId());
281 });
282 break;
283 case REMOVE_FROM_EXISTING:
284 groups.forEach(group -> {
285 groupService.removeBucketsFromGroup(deviceId, group.appCookie(),
286 group.buckets(),
287 group.appCookie(),
288 group.appId());
289 });
290 break;
Yi Tseng0b809722017-11-03 10:23:26 -0700291 default:
292 log.warn("Unsupported objective operation {}", objective.op());
293 groupService.removeListener(listener);
294 }
295 try {
296 return groupInstallFuture.get(DEFAULT_INSTALLATION_TIME_OUT, TimeUnit.SECONDS);
297 } catch (InterruptedException | ExecutionException | TimeoutException e) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800298 groupService.removeListener(listener);
299 log.warn("Got exception while installing groups: {}", e.getMessage());
Yi Tseng0b809722017-11-03 10:23:26 -0700300 return false;
301 }
302 }
303
304 static void fail(Objective objective, ObjectiveError error) {
305 objective.context().ifPresent(ctx -> ctx.onError(objective, error));
306 }
307
308 static void success(Objective objective) {
309 objective.context().ifPresent(ctx -> ctx.onSuccess(objective));
310 }
311
312 static FlowRuleOperations buildFlowRuleOps(Objective objective, Collection<FlowRule> flowRules,
313 FlowRuleOperationsContext ctx) {
314 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
315 switch (objective.op()) {
316 case ADD:
317 flowRules.forEach(ops::add);
318 break;
319 case REMOVE:
320 flowRules.forEach(ops::remove);
321 break;
322 default:
323 log.warn("Unsupported op {} for {}", objective);
324 fail(objective, ObjectiveError.BADPARAMS);
325 return null;
326 }
327 return ops.build(ctx);
328 }
329
330 class FabricNextGroup implements NextGroup {
331 private NextObjective.Type type;
332 private Collection<PortNumber> outputPorts;
333
334 public FabricNextGroup(NextObjective.Type type, Collection<PortNumber> outputPorts) {
335 this.type = type;
336 this.outputPorts = ImmutableList.copyOf(outputPorts);
337 }
338
339 public NextObjective.Type type() {
340 return type;
341 }
342
343 public Collection<PortNumber> outputPorts() {
344 return outputPorts;
345 }
346
347 @Override
348 public byte[] data() {
349 return KRYO.serialize(this);
350 }
351 }
Yi Tseng1b154bd2017-11-20 17:48:19 -0800352
Yi Tseng0b809722017-11-03 10:23:26 -0700353}