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