blob: c360c90deb770b7fb6908b88ea0723f052496f50 [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
52 @Override
53 public void init(DeviceId deviceId, PipelinerContext context) {
54 this.serviceDirectory = context.directory();
55 this.deviceId = deviceId;
56
57 flowRuleService = serviceDirectory.get(FlowRuleService.class);
58 }
59
60 @Override
61 public void filter(FilteringObjective filter) {
62 throw new UnsupportedOperationException("Single table does not filter.");
63 }
64
65 @Override
66 public void forward(ForwardingObjective fwd) {
67 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
68
69 if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) {
70 throw new UnsupportedOperationException(
71 "Only VERSATILE is supported.");
72 }
73
74 boolean isPunt = fwd.treatment().immediate().stream().anyMatch(i -> {
75 if (i instanceof Instructions.OutputInstruction) {
76 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
77 return out.port().equals(PortNumber.CONTROLLER);
78 }
79 return false;
80 });
81
82 if (isPunt) {
83 return;
84 }
85
86 TrafficSelector selector = fwd.selector();
87 TrafficTreatment treatment = fwd.treatment();
88 if ((fwd.treatment().deferred().size() == 0) &&
89 (fwd.treatment().immediate().size() == 0) &&
90 (fwd.treatment().tableTransition() == null) &&
91 (!fwd.treatment().clearedDeferred())) {
92 TrafficTreatment.Builder flowTreatment = DefaultTrafficTreatment.builder();
93 flowTreatment.add(Instructions.createDrop());
94 treatment = flowTreatment.build();
95 }
96
97 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
98 .forDevice(deviceId)
99 .withSelector(selector)
100 .withTreatment(fwd.treatment())
101 .fromApp(fwd.appId())
102 .withPriority(fwd.priority());
103
104 if (fwd.permanent()) {
105 ruleBuilder.makePermanent();
106 } else {
107 ruleBuilder.makeTemporary(fwd.timeout());
108 }
109
110
111 switch (fwd.op()) {
112
113 case ADD:
114 flowBuilder.add(ruleBuilder.build());
115 break;
116 case REMOVE:
117 flowBuilder.remove(ruleBuilder.build());
118 break;
119 default:
120 log.warn("Unknown operation {}", fwd.op());
121 }
122
123 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
124 @Override
125 public void onSuccess(FlowRuleOperations ops) {
126 if (fwd.context().isPresent()) {
127 fwd.context().get().onSuccess(fwd);
128 }
129 }
130
131 @Override
132 public void onError(FlowRuleOperations ops) {
133 if (fwd.context().isPresent()) {
134 fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
135 }
136 }
137 }));
138
139 }
140
141 @Override
142 public void next(NextObjective nextObjective) {
143 throw new UnsupportedOperationException("Single table does not next hop.");
144 }
145
146}