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 | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 24 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
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 | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 34 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Simple single table pipeline abstraction. |
| 41 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 42 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | private ServiceDirectory serviceDirectory; |
| 47 | private FlowRuleService flowRuleService; |
| 48 | private DeviceId deviceId; |
| 49 | |
| 50 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 51 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 52 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 53 | this.deviceId = deviceId; |
| 54 | |
| 55 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 59 | public void filter(FilteringObjective filter) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 60 | throw new UnsupportedOperationException("Single table does not filter."); |
| 61 | } |
| 62 | |
| 63 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 64 | public void forward(ForwardingObjective fwd) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 65 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 66 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 67 | if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) { |
| 68 | throw new UnsupportedOperationException( |
| 69 | "Only VERSATILE is supported."); |
| 70 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 71 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 72 | TrafficSelector selector = fwd.selector(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 73 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 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()); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 79 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 80 | switch (fwd.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 81 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 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 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 92 | |
| 93 | SettableFuture<Boolean> future = SettableFuture.create(); |
| 94 | |
| 95 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 96 | @Override |
| 97 | public void onSuccess(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 98 | if (fwd.context().isPresent()) { |
| 99 | fwd.context().get().onSuccess(fwd); |
| 100 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onError(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 105 | if (fwd.context().isPresent()) { |
| 106 | fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 107 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 108 | } |
| 109 | })); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 110 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 111 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 112 | |
| 113 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame^] | 114 | public void next(NextObjective nextObjective) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 115 | throw new UnsupportedOperationException("Single table does not next hop."); |
| 116 | } |
| 117 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 118 | } |