blob: 549dbfe16f76ee64c70e50ac56cdd356f2c2ca7c [file] [log] [blame]
HelloONOSaf3b4282017-06-27 16:27:31 +09001/*
2 * Copyright 2017-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 */
16package org.onosproject.driver.pipeline;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.List;
21import java.util.Optional;
22
23
24import com.google.common.collect.ImmutableList;
25import org.onlab.osgi.ServiceDirectory;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Device;
28import org.onosproject.net.behaviour.NextGroup;
29import org.onosproject.net.behaviour.Pipeliner;
30import org.onosproject.net.behaviour.PipelinerContext;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleOperations;
35import org.onosproject.net.flow.FlowRuleOperationsContext;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.flowobjective.DefaultForwardingObjective;
40import org.onosproject.net.flowobjective.FilteringObjective;
41import org.onosproject.net.flowobjective.ForwardingObjective;
42import org.onosproject.net.flowobjective.NextObjective;
43import org.onosproject.net.flowobjective.Objective;
44import org.onosproject.net.flowobjective.ObjectiveError;
45import org.slf4j.Logger;
46
47
48/**
sdn3b223942018-05-30 20:37:21 +090049 * Juniper QFX5100 Series Switch single table pipeline abstraction.
HelloONOSaf3b4282017-06-27 16:27:31 +090050 */
sdn3b223942018-05-30 20:37:21 +090051public class JuniperQfx5100Pipeliner extends DefaultSingleTablePipeline implements Pipeliner {
HelloONOSaf3b4282017-06-27 16:27:31 +090052
53 private final Logger log = getLogger(getClass());
54
55 //Juniper Switch Default flow table starts from 1
56 private static final int DEFAULT_TABLE = 1;
57
58 private ServiceDirectory serviceDirectory;
59 private DeviceId deviceId;
60 private FlowRuleService flowRuleService;
HelloONOSaf3b4282017-06-27 16:27:31 +090061 protected DeviceService deviceService;
sdn35cc3e62018-06-01 18:08:41 +090062
HelloONOSaf3b4282017-06-27 16:27:31 +090063 @Override
64 public void init(DeviceId deviceId, PipelinerContext context) {
sdn35cc3e62018-06-01 18:08:41 +090065 super.init(deviceId, context);
HelloONOSaf3b4282017-06-27 16:27:31 +090066 this.deviceId = deviceId;
67 this.serviceDirectory = context.directory();
68 this.flowRuleService = serviceDirectory.get(FlowRuleService.class);
sdn35cc3e62018-06-01 18:08:41 +090069 deviceService = serviceDirectory.get(DeviceService.class);
HelloONOSaf3b4282017-06-27 16:27:31 +090070 }
71
72 @Override
73 public void filter(FilteringObjective filterObjective) {
74 //Do nothing
Ray Milkey5082e9d2017-08-15 11:32:34 -070075 log.debug("No action is needed here");
HelloONOSaf3b4282017-06-27 16:27:31 +090076 }
77
78 @Override
79 public void forward(ForwardingObjective forwardObjective) {
80 FlowRule rule;
81 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
82
83 ForwardingObjective newFwd = forwardObjective;
HelloONOSaf3b4282017-06-27 16:27:31 +090084 Device device = deviceService.getDevice(deviceId);
sdn35cc3e62018-06-01 18:08:41 +090085
HelloONOSaf3b4282017-06-27 16:27:31 +090086 if (forwardObjective.treatment() != null && forwardObjective.treatment().clearedDeferred()) {
Ray Milkey5082e9d2017-08-15 11:32:34 -070087 log.warn("Using 'clear actions' instruction which is not supported by {} {} {} Switch",
HelloONOSaf3b4282017-06-27 16:27:31 +090088 device.id(), device.manufacturer(), device.hwVersion());
89 newFwd = forwardingObjectiveWithoutCleardDef(forwardObjective).orElse(forwardObjective);
90 }
91
92 rule = processForward(newFwd);
93 switch (forwardObjective.op()) {
94 case ADD:
95 flowOpsBuilder.add(rule);
96 break;
97 case REMOVE:
98 flowOpsBuilder.remove(rule);
99 break;
100 default:
101 fail(forwardObjective, ObjectiveError.UNKNOWN);
102 log.warn("Unknown forwarding type {}", forwardObjective.op());
103 }
104
105 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
106 @Override
107 public void onSuccess(FlowRuleOperations ops) {
108 pass(forwardObjective);
109 }
110
111 @Override
112 public void onError(FlowRuleOperations ops) {
113 fail(forwardObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
114 }
115 }));
116 }
117
118 private Optional<ForwardingObjective> forwardingObjectiveWithoutCleardDef(ForwardingObjective forwardingObjective) {
119
120 TrafficTreatment treatment = trafficTreatmentWithoutCleardDeffered(forwardingObjective.treatment());
121
122 DefaultForwardingObjective.Builder foBuilder = (DefaultForwardingObjective.Builder) forwardingObjective.copy();
123 foBuilder.withTreatment(treatment);
124
125
126 switch (forwardingObjective.op()) {
127 case ADD:
128 return Optional.of(foBuilder.add(forwardingObjective.context().orElse(null)));
129 case REMOVE:
130 return Optional.of(foBuilder.remove(forwardingObjective.context().orElse(null)));
131 default:
132 log.warn("Driver Not support other operations for forwarding objective");
133 return Optional.empty();
134 }
135 }
136
137 private TrafficTreatment trafficTreatmentWithoutCleardDeffered(TrafficTreatment treatment) {
138 return DefaultTrafficTreatment.builder(treatment)
139 .notWipeDeferred()
140 .build();
141 }
142
143 private FlowRule processForward(ForwardingObjective fwd) {
144
145 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
146 .forDevice(deviceId)
147 .withSelector(fwd.selector())
148 .withTreatment(fwd.treatment())
149 .withPriority(fwd.priority())
150 .fromApp(fwd.appId())
151 .forTable(DEFAULT_TABLE);
152 if (fwd.permanent()) {
153 ruleBuilder.makePermanent();
154 } else {
155 ruleBuilder.makeTemporary(fwd.timeout());
156 }
157
158 return ruleBuilder.build();
159
160 }
161
162 @Override
163 public void next(NextObjective nextObjective) {
164 //Do nothing
Ray Milkey5082e9d2017-08-15 11:32:34 -0700165 log.debug("no action is needed here");
HelloONOSaf3b4282017-06-27 16:27:31 +0900166 }
167
168 @Override
169 public List<String> getNextMappings(NextGroup nextGroup) {
170
171 return ImmutableList.of();
172 }
173
174 private void pass(Objective obj) {
175 obj.context().ifPresent(context -> context.onSuccess(obj));
176 }
177
178 private void fail(Objective obj, ObjectiveError error) {
179 obj.context().ifPresent(context -> context.onError(obj, error));
180 }
181}