blob: 8e3774654a19a9bea5ad9b0f8da9f2f368f720ea [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
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 */
16package org.onosproject.driver.pipeline;
17
alshabibfaa1e362015-04-02 15:01:54 -070018import org.onlab.osgi.ServiceDirectory;
alshabibfaa1e362015-04-02 15:01:54 -070019import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080020import org.onosproject.net.behaviour.NextGroup;
alshabibfaa1e362015-04-02 15:01:54 -070021import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070022import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070023import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibfaa1e362015-04-02 15:01:54 -070024import org.onosproject.net.flow.DefaultFlowRule;
alshabibc61e18c2016-02-02 23:05:25 -080025import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070026import org.onosproject.net.flow.DefaultTrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070027import org.onosproject.net.flow.FlowRule;
28import org.onosproject.net.flow.FlowRuleOperations;
29import org.onosproject.net.flow.FlowRuleOperationsContext;
30import org.onosproject.net.flow.FlowRuleService;
31import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070032import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flow.instructions.Instructions;
alshabibfaa1e362015-04-02 15:01:54 -070034import org.onosproject.net.flowobjective.FilteringObjective;
35import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.flowobjective.NextObjective;
alshabibc61e18c2016-02-02 23:05:25 -080037import org.onosproject.net.flowobjective.Objective;
alshabib2a441c62015-04-13 18:39:38 -070038import org.onosproject.net.flowobjective.ObjectiveError;
alshabibfaa1e362015-04-02 15:01:54 -070039import org.slf4j.Logger;
40
alshabibfaa1e362015-04-02 15:01:54 -070041import static org.slf4j.LoggerFactory.getLogger;
42
Saurav Das24431192016-03-07 19:13:00 -080043import java.util.List;
44
alshabibfaa1e362015-04-02 15:01:54 -070045/**
46 * Simple single table pipeline abstraction.
47 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070048public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibfaa1e362015-04-02 15:01:54 -070049
50 private final Logger log = getLogger(getClass());
51
52 private ServiceDirectory serviceDirectory;
53 private FlowRuleService flowRuleService;
54 private DeviceId deviceId;
55
56 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070057 public void init(DeviceId deviceId, PipelinerContext context) {
58 this.serviceDirectory = context.directory();
alshabibfaa1e362015-04-02 15:01:54 -070059 this.deviceId = deviceId;
60
61 flowRuleService = serviceDirectory.get(FlowRuleService.class);
alshabibfaa1e362015-04-02 15:01:54 -070062 }
63
64 @Override
alshabibc61e18c2016-02-02 23:05:25 -080065 public void filter(FilteringObjective filter) {
66
67 TrafficTreatment.Builder actions;
68 switch (filter.type()) {
69 case PERMIT:
70 actions = (filter.meta() == null) ?
71 DefaultTrafficTreatment.builder().punt() :
72 DefaultTrafficTreatment.builder(filter.meta());
73 break;
74 case DENY:
75 actions = (filter.meta() == null) ?
76 DefaultTrafficTreatment.builder() :
77 DefaultTrafficTreatment.builder(filter.meta());
78 actions.drop();
79 break;
80 default:
81 log.warn("Unknown filter type: {}", filter.type());
82 actions = DefaultTrafficTreatment.builder().drop();
83 }
84
85 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
86
87 filter.conditions().stream().forEach(selector::add);
88
89 if (filter.key() != null) {
90 selector.add(filter.key());
91 }
92
93 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
94 .forDevice(deviceId)
95 .withSelector(selector.build())
96 .withTreatment(actions.build())
97 .fromApp(filter.appId())
98 .withPriority(filter.priority());
99
100 if (filter.permanent()) {
101 ruleBuilder.makePermanent();
102 } else {
103 ruleBuilder.makeTemporary(filter.timeout());
104 }
105
106 installObjective(ruleBuilder, filter);
107
108 }
alshabibfaa1e362015-04-02 15:01:54 -0700109
110 @Override
alshabib2a441c62015-04-13 18:39:38 -0700111 public void forward(ForwardingObjective fwd) {
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800112 // Deal with SPECIFIC and VERSATILE in the same manner.
alshabib2a441c62015-04-13 18:39:38 -0700113 TrafficSelector selector = fwd.selector();
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700114 TrafficTreatment treatment = fwd.treatment();
115 if ((fwd.treatment().deferred().size() == 0) &&
116 (fwd.treatment().immediate().size() == 0) &&
117 (fwd.treatment().tableTransition() == null) &&
118 (!fwd.treatment().clearedDeferred())) {
119 TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder();
Ray Milkey2be39ed2016-02-22 15:54:19 -0800120 flowTreatment.add(Instructions.createNoAction());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700121 treatment = flowTreatment.build();
122 }
alshabibfaa1e362015-04-02 15:01:54 -0700123
alshabibb452fd72015-04-22 20:46:20 -0700124 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
125 .forDevice(deviceId)
126 .withSelector(selector)
Charles M.C. Chanef3f39e2015-07-14 22:21:46 +0800127 .withTreatment(treatment)
alshabibb452fd72015-04-22 20:46:20 -0700128 .fromApp(fwd.appId())
129 .withPriority(fwd.priority());
130
131 if (fwd.permanent()) {
132 ruleBuilder.makePermanent();
133 } else {
134 ruleBuilder.makeTemporary(fwd.timeout());
135 }
136
alshabibc61e18c2016-02-02 23:05:25 -0800137 installObjective(ruleBuilder, fwd);
alshabibfaa1e362015-04-02 15:01:54 -0700138
alshabibc61e18c2016-02-02 23:05:25 -0800139 }
140
141 private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) {
142 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
143 switch (objective.op()) {
alshabibfaa1e362015-04-02 15:01:54 -0700144
alshabib2a441c62015-04-13 18:39:38 -0700145 case ADD:
alshabibb452fd72015-04-22 20:46:20 -0700146 flowBuilder.add(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700147 break;
148 case REMOVE:
alshabibb452fd72015-04-22 20:46:20 -0700149 flowBuilder.remove(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700150 break;
151 default:
alshabibc61e18c2016-02-02 23:05:25 -0800152 log.warn("Unknown operation {}", objective.op());
alshabib2a441c62015-04-13 18:39:38 -0700153 }
154
alshabibfaa1e362015-04-02 15:01:54 -0700155 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
156 @Override
157 public void onSuccess(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800158 objective.context().ifPresent(context -> context.onSuccess(objective));
alshabibfaa1e362015-04-02 15:01:54 -0700159 }
160
161 @Override
162 public void onError(FlowRuleOperations ops) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800163 objective.context()
164 .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
alshabibfaa1e362015-04-02 15:01:54 -0700165 }
166 }));
alshabibfaa1e362015-04-02 15:01:54 -0700167 }
alshabib77b88482015-04-07 15:47:50 -0700168
169 @Override
alshabibc61e18c2016-02-02 23:05:25 -0800170 public void next(NextObjective nextObjective) {
171 }
alshabib77b88482015-04-07 15:47:50 -0700172
Saurav Das24431192016-03-07 19:13:00 -0800173 @Override
174 public List<String> getNextMappings(NextGroup nextGroup) {
175 // Default single table pipeline does not use nextObjectives or groups
176 return null;
177 }
178
alshabibfaa1e362015-04-02 15:01:54 -0700179}