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