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