HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.List; |
| 21 | import java.util.Optional; |
| 22 | |
| 23 | |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import org.onlab.osgi.ServiceDirectory; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.behaviour.NextGroup; |
| 29 | import org.onosproject.net.behaviour.Pipeliner; |
| 30 | import org.onosproject.net.behaviour.PipelinerContext; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.flow.DefaultFlowRule; |
| 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleOperations; |
| 35 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 40 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 41 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 42 | import org.onosproject.net.flowobjective.NextObjective; |
| 43 | import org.onosproject.net.flowobjective.Objective; |
| 44 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | |
| 48 | /** |
sdn | 3b22394 | 2018-05-30 20:37:21 +0900 | [diff] [blame] | 49 | * Juniper QFX5100 Series Switch single table pipeline abstraction. |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 50 | */ |
sdn | 3b22394 | 2018-05-30 20:37:21 +0900 | [diff] [blame] | 51 | public class JuniperQfx5100Pipeliner extends DefaultSingleTablePipeline implements Pipeliner { |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 52 | |
| 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; |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 61 | protected DeviceService deviceService; |
sdn | 35cc3e6 | 2018-06-01 18:08:41 +0900 | [diff] [blame] | 62 | |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 63 | @Override |
| 64 | public void init(DeviceId deviceId, PipelinerContext context) { |
sdn | 35cc3e6 | 2018-06-01 18:08:41 +0900 | [diff] [blame] | 65 | super.init(deviceId, context); |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 66 | this.deviceId = deviceId; |
| 67 | this.serviceDirectory = context.directory(); |
| 68 | this.flowRuleService = serviceDirectory.get(FlowRuleService.class); |
sdn | 35cc3e6 | 2018-06-01 18:08:41 +0900 | [diff] [blame] | 69 | deviceService = serviceDirectory.get(DeviceService.class); |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void filter(FilteringObjective filterObjective) { |
| 74 | //Do nothing |
Ray Milkey | 5082e9d | 2017-08-15 11:32:34 -0700 | [diff] [blame] | 75 | log.debug("No action is needed here"); |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void forward(ForwardingObjective forwardObjective) { |
| 80 | FlowRule rule; |
| 81 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 82 | |
| 83 | ForwardingObjective newFwd = forwardObjective; |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 84 | Device device = deviceService.getDevice(deviceId); |
sdn | 35cc3e6 | 2018-06-01 18:08:41 +0900 | [diff] [blame] | 85 | |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 86 | if (forwardObjective.treatment() != null && forwardObjective.treatment().clearedDeferred()) { |
Ray Milkey | 5082e9d | 2017-08-15 11:32:34 -0700 | [diff] [blame] | 87 | log.warn("Using 'clear actions' instruction which is not supported by {} {} {} Switch", |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 88 | 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 Milkey | 5082e9d | 2017-08-15 11:32:34 -0700 | [diff] [blame] | 165 | log.debug("no action is needed here"); |
HelloONOS | af3b428 | 2017-06-27 16:27:31 +0900 | [diff] [blame] | 166 | } |
| 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 | } |