blob: 851669797c7e2095ef12b0e4e480b6432a0f2188 [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
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 */
16package org.onosproject.driver.pipeline;
17
18import com.google.common.util.concurrent.SettableFuture;
19import org.onlab.osgi.ServiceDirectory;
20import org.onosproject.core.DefaultGroupId;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.Pipeliner;
23import org.onosproject.net.flow.DefaultFlowRule;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleOperations;
26import org.onosproject.net.flow.FlowRuleOperationsContext;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flowobjective.FilteringObjective;
30import org.onosproject.net.flowobjective.ForwardingObjective;
31import org.slf4j.Logger;
32
33import java.util.Collection;
34import java.util.concurrent.Future;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Simple single table pipeline abstraction.
40 */
41public class DefaultSingleTablePipeline implements Pipeliner {
42
43 private final Logger log = getLogger(getClass());
44
45 private ServiceDirectory serviceDirectory;
46 private FlowRuleService flowRuleService;
47 private DeviceId deviceId;
48
49 @Override
50 public void init(DeviceId deviceId, ServiceDirectory serviceDirectory) {
51 this.serviceDirectory = serviceDirectory;
52 this.deviceId = deviceId;
53
54 flowRuleService = serviceDirectory.get(FlowRuleService.class);
55
56 }
57
58 @Override
59 public Future<Boolean> filter(Collection<FilteringObjective> filters) {
60 throw new UnsupportedOperationException("Single table does not filter.");
61 }
62
63 @Override
64 public Future<Boolean> forward(Collection<ForwardingObjective> forwardings) {
65 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
66 forwardings.forEach(fwd -> {
67 if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) {
68 throw new UnsupportedOperationException(
69 "Only VERSATILE is supported.");
70 }
71
72 TrafficSelector selector = fwd.selector();
73
74 FlowRule rule = new DefaultFlowRule(deviceId, selector,
75 fwd.treatment(),
76 fwd.priority(), fwd.appId(),
77 new DefaultGroupId(fwd.id()),
78 fwd.timeout(), fwd.permanent());
79
80 switch (fwd.op()) {
81
82 case ADD:
83 flowBuilder.add(rule);
84 break;
85 case REMOVE:
86 flowBuilder.remove(rule);
87 break;
88 default:
89 log.warn("Unknown operation {}", fwd.op());
90 }
91
92 });
93
94 SettableFuture<Boolean> future = SettableFuture.create();
95
96 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
97 @Override
98 public void onSuccess(FlowRuleOperations ops) {
99 future.set(true);
100 }
101
102 @Override
103 public void onError(FlowRuleOperations ops) {
104 future.set(false);
105 }
106 }));
107 return future;
108 }
109}