blob: d42650f55d3b7e73a8f7c35f7aea97b86c98be05 [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;
20import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070021import org.onosproject.net.behaviour.PipelinerContext;
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070022import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabibfaa1e362015-04-02 15:01:54 -070023import org.onosproject.net.flow.DefaultFlowRule;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070024import org.onosproject.net.flow.DefaultTrafficTreatment;
alshabibfaa1e362015-04-02 15:01:54 -070025import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.FlowRuleOperations;
27import org.onosproject.net.flow.FlowRuleOperationsContext;
28import org.onosproject.net.flow.FlowRuleService;
29import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070030import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.flow.instructions.Instructions;
alshabibfaa1e362015-04-02 15:01:54 -070032import org.onosproject.net.flowobjective.FilteringObjective;
33import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib77b88482015-04-07 15:47:50 -070034import org.onosproject.net.flowobjective.NextObjective;
alshabib2a441c62015-04-13 18:39:38 -070035import org.onosproject.net.flowobjective.ObjectiveError;
alshabibfaa1e362015-04-02 15:01:54 -070036import org.slf4j.Logger;
37
alshabibfaa1e362015-04-02 15:01:54 -070038import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Simple single table pipeline abstraction.
42 */
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070043public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
alshabibfaa1e362015-04-02 15:01:54 -070044
45 private final Logger log = getLogger(getClass());
46
47 private ServiceDirectory serviceDirectory;
48 private FlowRuleService flowRuleService;
49 private DeviceId deviceId;
50
51 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070052 public void init(DeviceId deviceId, PipelinerContext context) {
53 this.serviceDirectory = context.directory();
alshabibfaa1e362015-04-02 15:01:54 -070054 this.deviceId = deviceId;
55
56 flowRuleService = serviceDirectory.get(FlowRuleService.class);
alshabibfaa1e362015-04-02 15:01:54 -070057 }
58
59 @Override
alshabib6c277242015-07-01 08:46:48 -070060 public void filter(FilteringObjective filter) {}
alshabibfaa1e362015-04-02 15:01:54 -070061
62 @Override
alshabib2a441c62015-04-13 18:39:38 -070063 public void forward(ForwardingObjective fwd) {
alshabibfaa1e362015-04-02 15:01:54 -070064 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
alshabibfaa1e362015-04-02 15:01:54 -070065
alshabib2a441c62015-04-13 18:39:38 -070066 if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) {
67 throw new UnsupportedOperationException(
68 "Only VERSATILE is supported.");
69 }
alshabibfaa1e362015-04-02 15:01:54 -070070
alshabib2a441c62015-04-13 18:39:38 -070071 TrafficSelector selector = fwd.selector();
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070072 TrafficTreatment treatment = fwd.treatment();
73 if ((fwd.treatment().deferred().size() == 0) &&
74 (fwd.treatment().immediate().size() == 0) &&
75 (fwd.treatment().tableTransition() == null) &&
76 (!fwd.treatment().clearedDeferred())) {
77 TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder();
78 flowTreatment.add(Instructions.createDrop());
79 treatment = flowTreatment.build();
80 }
alshabibfaa1e362015-04-02 15:01:54 -070081
alshabibb452fd72015-04-22 20:46:20 -070082 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
83 .forDevice(deviceId)
84 .withSelector(selector)
Charles M.C. Chanef3f39e2015-07-14 22:21:46 +080085 .withTreatment(treatment)
alshabibb452fd72015-04-22 20:46:20 -070086 .fromApp(fwd.appId())
87 .withPriority(fwd.priority());
88
89 if (fwd.permanent()) {
90 ruleBuilder.makePermanent();
91 } else {
92 ruleBuilder.makeTemporary(fwd.timeout());
93 }
94
alshabibfaa1e362015-04-02 15:01:54 -070095
alshabib2a441c62015-04-13 18:39:38 -070096 switch (fwd.op()) {
alshabibfaa1e362015-04-02 15:01:54 -070097
alshabib2a441c62015-04-13 18:39:38 -070098 case ADD:
alshabibb452fd72015-04-22 20:46:20 -070099 flowBuilder.add(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700100 break;
101 case REMOVE:
alshabibb452fd72015-04-22 20:46:20 -0700102 flowBuilder.remove(ruleBuilder.build());
alshabib2a441c62015-04-13 18:39:38 -0700103 break;
104 default:
105 log.warn("Unknown operation {}", fwd.op());
106 }
107
alshabibfaa1e362015-04-02 15:01:54 -0700108 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
109 @Override
110 public void onSuccess(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -0700111 if (fwd.context().isPresent()) {
112 fwd.context().get().onSuccess(fwd);
113 }
alshabibfaa1e362015-04-02 15:01:54 -0700114 }
115
116 @Override
117 public void onError(FlowRuleOperations ops) {
alshabib2a441c62015-04-13 18:39:38 -0700118 if (fwd.context().isPresent()) {
119 fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
120 }
alshabibfaa1e362015-04-02 15:01:54 -0700121 }
122 }));
alshabib2a441c62015-04-13 18:39:38 -0700123
alshabibfaa1e362015-04-02 15:01:54 -0700124 }
alshabib77b88482015-04-07 15:47:50 -0700125
126 @Override
alshabib6c277242015-07-01 08:46:48 -0700127 public void next(NextObjective nextObjective) {}
alshabib77b88482015-04-07 15:47:50 -0700128
alshabibfaa1e362015-04-02 15:01:54 -0700129}