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; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame^] | 19 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceDirectory; |
| 21 | import org.onosproject.core.DefaultGroupId; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultFlowRule; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleOperations; |
| 30 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 31 | import org.onosproject.net.flow.FlowRuleService; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame^] | 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.instructions.Instructions; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Simple single table pipeline abstraction. |
| 45 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 46 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private ServiceDirectory serviceDirectory; |
| 51 | private FlowRuleService flowRuleService; |
| 52 | private DeviceId deviceId; |
| 53 | |
| 54 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 55 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 56 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 57 | this.deviceId = deviceId; |
| 58 | |
| 59 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 63 | public void filter(FilteringObjective filter) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 64 | throw new UnsupportedOperationException("Single table does not filter."); |
| 65 | } |
| 66 | |
| 67 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 68 | public void forward(ForwardingObjective fwd) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 69 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 70 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 71 | if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) { |
| 72 | throw new UnsupportedOperationException( |
| 73 | "Only VERSATILE is supported."); |
| 74 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 75 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 76 | TrafficSelector selector = fwd.selector(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame^] | 77 | TrafficTreatment treatment = fwd.treatment(); |
| 78 | if ((fwd.treatment().deferred().size() == 0) && |
| 79 | (fwd.treatment().immediate().size() == 0) && |
| 80 | (fwd.treatment().tableTransition() == null) && |
| 81 | (!fwd.treatment().clearedDeferred())) { |
| 82 | TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder(); |
| 83 | flowTreatment.add(Instructions.createDrop()); |
| 84 | treatment = flowTreatment.build(); |
| 85 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 86 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 87 | FlowRule rule = new DefaultFlowRule(deviceId, selector, |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame^] | 88 | treatment, |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 89 | fwd.priority(), fwd.appId(), |
| 90 | new DefaultGroupId(fwd.id()), |
| 91 | fwd.timeout(), fwd.permanent()); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 92 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 93 | switch (fwd.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 94 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 95 | case ADD: |
| 96 | flowBuilder.add(rule); |
| 97 | break; |
| 98 | case REMOVE: |
| 99 | flowBuilder.remove(rule); |
| 100 | break; |
| 101 | default: |
| 102 | log.warn("Unknown operation {}", fwd.op()); |
| 103 | } |
| 104 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 105 | |
| 106 | SettableFuture<Boolean> future = SettableFuture.create(); |
| 107 | |
| 108 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 109 | @Override |
| 110 | public void onSuccess(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 111 | if (fwd.context().isPresent()) { |
| 112 | fwd.context().get().onSuccess(fwd); |
| 113 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onError(FlowRuleOperations ops) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 118 | if (fwd.context().isPresent()) { |
| 119 | fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 120 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 121 | } |
| 122 | })); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 123 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 124 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 125 | |
| 126 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 127 | public void next(NextObjective nextObjective) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 128 | throw new UnsupportedOperationException("Single table does not next hop."); |
| 129 | } |
| 130 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 131 | } |