blob: 3b0195cbd87f367ec3a2d0cd328f99aed33d91b7 [file] [log] [blame]
alshabib0ccde6d2015-05-30 18:22:36 -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
18import org.onlab.osgi.ServiceDirectory;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.PortNumber;
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.DefaultTrafficTreatment;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.FlowRuleOperations;
28import org.onosproject.net.flow.FlowRuleOperationsContext;
29import org.onosproject.net.flow.FlowRuleService;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.flow.instructions.Instructions;
33import org.onosproject.net.flowobjective.FilteringObjective;
34import org.onosproject.net.flowobjective.ForwardingObjective;
35import org.onosproject.net.flowobjective.NextObjective;
36import org.onosproject.net.flowobjective.ObjectiveError;
37import org.slf4j.Logger;
38
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Simple single table pipeline abstraction.
43 */
44public class OLTPipeline extends AbstractHandlerBehaviour implements Pipeliner {
45
46 private final Logger log = getLogger(getClass());
47
48 private ServiceDirectory serviceDirectory;
49 private FlowRuleService flowRuleService;
50 private DeviceId deviceId;
51
alshabibb32cefe2015-06-08 18:15:05 -070052
alshabib0ccde6d2015-05-30 18:22:36 -070053 @Override
54 public void init(DeviceId deviceId, PipelinerContext context) {
55 this.serviceDirectory = context.directory();
56 this.deviceId = deviceId;
57
58 flowRuleService = serviceDirectory.get(FlowRuleService.class);
alshabibb32cefe2015-06-08 18:15:05 -070059
60 }
61
alshabibb32cefe2015-06-08 18:15:05 -070062
alshabib0ccde6d2015-05-30 18:22:36 -070063
64 @Override
65 public void filter(FilteringObjective filter) {
66 throw new UnsupportedOperationException("Single table does not filter.");
67 }
68
69 @Override
70 public void forward(ForwardingObjective fwd) {
alshabib0ccde6d2015-05-30 18:22:36 -070071 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
72
73 if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) {
74 throw new UnsupportedOperationException(
75 "Only VERSATILE is supported.");
76 }
77
78 boolean isPunt = fwd.treatment().immediate().stream().anyMatch(i -> {
79 if (i instanceof Instructions.OutputInstruction) {
80 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
81 return out.port().equals(PortNumber.CONTROLLER);
82 }
83 return false;
84 });
85
86 if (isPunt) {
87 return;
88 }
89
90 TrafficSelector selector = fwd.selector();
91 TrafficTreatment treatment = fwd.treatment();
92 if ((fwd.treatment().deferred().size() == 0) &&
93 (fwd.treatment().immediate().size() == 0) &&
94 (fwd.treatment().tableTransition() == null) &&
95 (!fwd.treatment().clearedDeferred())) {
96 TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder();
97 flowTreatment.add(Instructions.createDrop());
98 treatment = flowTreatment.build();
99 }
100
101 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
102 .forDevice(deviceId)
103 .withSelector(selector)
104 .withTreatment(fwd.treatment())
105 .fromApp(fwd.appId())
106 .withPriority(fwd.priority());
107
108 if (fwd.permanent()) {
109 ruleBuilder.makePermanent();
110 } else {
111 ruleBuilder.makeTemporary(fwd.timeout());
112 }
113
114
115 switch (fwd.op()) {
116
117 case ADD:
118 flowBuilder.add(ruleBuilder.build());
119 break;
120 case REMOVE:
121 flowBuilder.remove(ruleBuilder.build());
122 break;
123 default:
124 log.warn("Unknown operation {}", fwd.op());
125 }
126
127 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
128 @Override
129 public void onSuccess(FlowRuleOperations ops) {
130 if (fwd.context().isPresent()) {
131 fwd.context().get().onSuccess(fwd);
132 }
133 }
134
135 @Override
136 public void onError(FlowRuleOperations ops) {
137 if (fwd.context().isPresent()) {
138 fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
139 }
140 }
141 }));
142
143 }
144
145 @Override
146 public void next(NextObjective nextObjective) {
147 throw new UnsupportedOperationException("Single table does not next hop.");
148 }
149
150}