blob: 9fc532bd8e1b693f0ff571a4a36b405cbc633209 [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
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.behaviour.NextGroup;
21import org.onosproject.net.behaviour.Pipeliner;
22import org.onosproject.net.behaviour.PipelinerContext;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.net.flow.DefaultFlowRule;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.FlowRuleService;
27import org.onosproject.net.flowobjective.FilteringObjective;
28import org.onosproject.net.flowobjective.ForwardingObjective;
29import org.onosproject.net.flowobjective.NextObjective;
30import org.onosproject.net.flowobjective.ObjectiveError;
31import org.slf4j.Logger;
32
33import java.util.Collections;
34import java.util.List;
35
36import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_TABLE0_CONTROL_TABLE0;
37import 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 */
43public 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}