Carmelo Cascone | 776be38 | 2018-12-12 19:03:57 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.pipelines.basic; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.NextGroup; |
| 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.FlowRule; |
| 26 | import org.onosproject.net.flow.FlowRuleService; |
| 27 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 28 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 29 | import org.onosproject.net.flowobjective.NextObjective; |
| 30 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.util.Collections; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_TABLE0_CONTROL_TABLE0; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Pipeliner implementation for basic.p4 that maps all forwarding objectives to |
| 41 | * table0. All other types of objectives are not supported. |
| 42 | */ |
| 43 | public class BasicPipelinerImpl extends AbstractHandlerBehaviour implements Pipeliner { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | private FlowRuleService flowRuleService; |
| 48 | private DeviceId deviceId; |
| 49 | |
| 50 | |
| 51 | @Override |
| 52 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 53 | this.deviceId = deviceId; |
| 54 | this.flowRuleService = context.directory().get(FlowRuleService.class); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void filter(FilteringObjective obj) { |
| 60 | obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNSUPPORTED)); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void forward(ForwardingObjective obj) { |
| 65 | if (obj.treatment() == null) { |
| 66 | obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNSUPPORTED)); |
| 67 | } |
| 68 | |
| 69 | // Simply create an equivalent FlowRule for table 0. |
| 70 | final FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 71 | .forTable(INGRESS_TABLE0_CONTROL_TABLE0) |
| 72 | .forDevice(deviceId) |
| 73 | .withSelector(obj.selector()) |
| 74 | .fromApp(obj.appId()) |
| 75 | .withPriority(obj.priority()) |
| 76 | .withTreatment(obj.treatment()); |
| 77 | |
| 78 | if (obj.permanent()) { |
| 79 | ruleBuilder.makePermanent(); |
| 80 | } else { |
| 81 | ruleBuilder.makeTemporary(obj.timeout()); |
| 82 | } |
| 83 | |
| 84 | switch (obj.op()) { |
| 85 | case ADD: |
| 86 | flowRuleService.applyFlowRules(ruleBuilder.build()); |
| 87 | break; |
| 88 | case REMOVE: |
| 89 | flowRuleService.removeFlowRules(ruleBuilder.build()); |
| 90 | break; |
| 91 | default: |
| 92 | log.warn("Unknown operation {}", obj.op()); |
| 93 | } |
| 94 | |
| 95 | obj.context().ifPresent(c -> c.onSuccess(obj)); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void next(NextObjective obj) { |
| 100 | obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNSUPPORTED)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 105 | // We do not use nextObjectives or groups. |
| 106 | return Collections.emptyList(); |
| 107 | } |
| 108 | } |