blob: 730889804015c54f41a111c01f7e8bce9f7bc980 [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;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070023import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070024import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibfaa1e362015-04-02 15:01:54 -070025import org.onosproject.net.flow.DefaultFlowRule;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.FlowRuleOperations;
28import org.onosproject.net.flow.FlowRuleOperationsContext;
29import org.onosproject.net.flow.FlowRuleService;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flowobjective.FilteringObjective;
32import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib77b88482015-04-07 15:47:50 -070033import org.onosproject.net.flowobjective.NextObjective;
alshabib2a441c62015-04-13 18:39:38 -070034import org.onosproject.net.flowobjective.ObjectiveError;
alshabibfaa1e362015-04-02 15:01:54 -070035import org.slf4j.Logger;
36
alshabibfaa1e362015-04-02 15:01:54 -070037import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Simple single table pipeline abstraction.
41 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070042public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibfaa1e362015-04-02 15:01:54 -070043
44 private final Logger log = getLogger(getClass());
45
46 private ServiceDirectory serviceDirectory;
47 private FlowRuleService flowRuleService;
48 private DeviceId deviceId;
49
50 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070051 public void init(DeviceId deviceId, PipelinerContext context) {
52 this.serviceDirectory = context.directory();
alshabibfaa1e362015-04-02 15:01:54 -070053 this.deviceId = deviceId;
54
55 flowRuleService = serviceDirectory.get(FlowRuleService.class);
alshabibfaa1e362015-04-02 15:01:54 -070056 }
57
58 @Override
alshabib2a441c62015-04-13 18:39:38 -070059 public void filter(FilteringObjective filter) {
alshabibfaa1e362015-04-02 15:01:54 -070060 throw new UnsupportedOperationException("Single table does not filter.");
61 }
62
63 @Override
alshabib2a441c62015-04-13 18:39:38 -070064 public void forward(ForwardingObjective fwd) {
alshabibfaa1e362015-04-02 15:01:54 -070065 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
alshabibfaa1e362015-04-02 15:01:54 -070066
alshabib2a441c62015-04-13 18:39:38 -070067 if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) {
68 throw new UnsupportedOperationException(
69 "Only VERSATILE is supported.");
70 }
alshabibfaa1e362015-04-02 15:01:54 -070071
alshabib2a441c62015-04-13 18:39:38 -070072 TrafficSelector selector = fwd.selector();
alshabibfaa1e362015-04-02 15:01:54 -070073
alshabib2a441c62015-04-13 18:39:38 -070074 FlowRule rule = new DefaultFlowRule(deviceId, selector,
75 fwd.treatment(),
76 fwd.priority(), fwd.appId(),
77 new DefaultGroupId(fwd.id()),
78 fwd.timeout(), fwd.permanent());
alshabibfaa1e362015-04-02 15:01:54 -070079
alshabib2a441c62015-04-13 18:39:38 -070080 switch (fwd.op()) {
alshabibfaa1e362015-04-02 15:01:54 -070081
alshabib2a441c62015-04-13 18:39:38 -070082 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
alshabibfaa1e362015-04-02 15:01:54 -070092
93 SettableFuture<Boolean> future = SettableFuture.create();
94
95 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
96 @Override
97 public void onSuccess(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -070098 if (fwd.context().isPresent()) {
99 fwd.context().get().onSuccess(fwd);
100 }
alshabibfaa1e362015-04-02 15:01:54 -0700101 }
102
103 @Override
104 public void onError(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -0700105 if (fwd.context().isPresent()) {
106 fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
107 }
alshabibfaa1e362015-04-02 15:01:54 -0700108 }
109 }));
alshabib2a441c62015-04-13 18:39:38 -0700110
alshabibfaa1e362015-04-02 15:01:54 -0700111 }
alshabib77b88482015-04-07 15:47:50 -0700112
113 @Override
alshabib2a441c62015-04-13 18:39:38 -0700114 public void next(NextObjective nextObjective) {
alshabib77b88482015-04-07 15:47:50 -0700115 throw new UnsupportedOperationException("Single table does not next hop.");
116 }
117
alshabibfaa1e362015-04-02 15:01:54 -0700118}