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