blob: 3b66f95bd9be7e631aa149dc269115573b396e77 [file] [log] [blame]
Carmelo Cascone776be382018-12-12 19:03:57 -08001/*
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
17package org.onosproject.pipelines.basic;
18
Daniele Moro06aac702021-07-19 22:39:22 +020019import org.onosproject.core.ApplicationId;
Carmelo Cascone776be382018-12-12 19:03:57 -080020import org.onosproject.net.DeviceId;
21import org.onosproject.net.behaviour.NextGroup;
22import org.onosproject.net.behaviour.Pipeliner;
23import org.onosproject.net.behaviour.PipelinerContext;
24import org.onosproject.net.driver.AbstractHandlerBehaviour;
25import org.onosproject.net.flow.DefaultFlowRule;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.flowobjective.FilteringObjective;
29import org.onosproject.net.flowobjective.ForwardingObjective;
30import org.onosproject.net.flowobjective.NextObjective;
31import org.onosproject.net.flowobjective.ObjectiveError;
32import org.slf4j.Logger;
33
34import java.util.Collections;
35import java.util.List;
36
37import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_TABLE0_CONTROL_TABLE0;
38import 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 */
44public 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 Moro06aac702021-07-19 22:39:22 +0200105 public void purgeAll(ApplicationId appId) {
106 flowRuleService.purgeFlowRules(deviceId, appId);
107 }
108
109 @Override
Carmelo Cascone776be382018-12-12 19:03:57 -0800110 public List<String> getNextMappings(NextGroup nextGroup) {
111 // We do not use nextObjectives or groups.
112 return Collections.emptyList();
113 }
114}