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