Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.flow.impl; |
| 18 | |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 19 | import static com.google.common.collect.Lists.newArrayList; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.LinkedListMultimap; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Sets; |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 24 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.mastership.MastershipService; |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.TableStatisticsDiscovery; |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 30 | import org.onosproject.net.device.DeviceEvent; |
| 31 | import org.onosproject.net.device.DeviceListener; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 34 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 36 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowRuleProgrammable; |
| 38 | import org.onosproject.net.flow.FlowRuleProvider; |
| 39 | import org.onosproject.net.flow.FlowRuleProviderService; |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.TableStatisticsEntry; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 41 | import org.onosproject.net.provider.AbstractProvider; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | import java.util.Collection; |
| 47 | import java.util.Set; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 48 | import java.util.concurrent.ScheduledExecutorService; |
| 49 | import java.util.concurrent.ScheduledFuture; |
| 50 | import java.util.concurrent.TimeUnit; |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 51 | import java.util.List; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 52 | |
| 53 | import static com.google.common.collect.ImmutableSet.copyOf; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 54 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 55 | import static org.onlab.util.Tools.groupedThreads; |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 56 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
Ray Milkey | 7bf273c | 2017-09-27 16:15:15 -0700 | [diff] [blame] | 57 | import static org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation.*; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Driver-based flow rule provider. |
| 61 | */ |
| 62 | class FlowRuleDriverProvider extends AbstractProvider implements FlowRuleProvider { |
| 63 | |
| 64 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
| 66 | // Perhaps to be extracted for better reuse as we deal with other. |
| 67 | public static final String SCHEME = "default"; |
| 68 | public static final String PROVIDER_NAME = "org.onosproject.provider"; |
| 69 | |
| 70 | FlowRuleProviderService providerService; |
| 71 | private DeviceService deviceService; |
| 72 | private MastershipService mastershipService; |
| 73 | |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 74 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 75 | private ScheduledExecutorService executor |
| 76 | = newSingleThreadScheduledExecutor(groupedThreads("FlowRuleDriverProvider", "%d", log)); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 77 | private ScheduledFuture<?> poller = null; |
| 78 | |
| 79 | /** |
| 80 | * Creates a new fallback flow rule provider. |
| 81 | */ |
| 82 | FlowRuleDriverProvider() { |
| 83 | super(new ProviderId(SCHEME, PROVIDER_NAME)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Initializes the provider with necessary supporting services. |
| 88 | * |
| 89 | * @param providerService flow rule provider service |
| 90 | * @param deviceService device service |
| 91 | * @param mastershipService mastership service |
| 92 | * @param pollFrequency flow entry poll frequency |
| 93 | */ |
| 94 | void init(FlowRuleProviderService providerService, |
| 95 | DeviceService deviceService, MastershipService mastershipService, |
| 96 | int pollFrequency) { |
| 97 | this.providerService = providerService; |
| 98 | this.deviceService = deviceService; |
| 99 | this.mastershipService = mastershipService; |
| 100 | |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 101 | deviceService.addListener(deviceListener); |
| 102 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 103 | if (poller != null && !poller.isCancelled()) { |
| 104 | poller.cancel(false); |
| 105 | } |
| 106 | |
| 107 | poller = executor.scheduleAtFixedRate(this::pollFlowEntries, pollFrequency, |
| 108 | pollFrequency, TimeUnit.SECONDS); |
| 109 | } |
| 110 | |
Andrea Campanella | 5a3c09c | 2017-12-01 13:57:48 +0100 | [diff] [blame] | 111 | void terminate() { |
| 112 | deviceService.removeListener(deviceListener); |
| 113 | deviceService = null; |
| 114 | providerService = null; |
| 115 | mastershipService = null; |
| 116 | poller.cancel(true); |
| 117 | executor.shutdown(); |
| 118 | } |
| 119 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public void applyFlowRule(FlowRule... flowRules) { |
| 122 | rulesByDevice(flowRules).asMap().forEach(this::applyFlowRules); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removeFlowRule(FlowRule... flowRules) { |
| 127 | rulesByDevice(flowRules).asMap().forEach(this::removeFlowRules); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 132 | removeFlowRule(flowRules); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void executeBatch(FlowRuleBatchOperation batch) { |
| 137 | ImmutableList.Builder<FlowRule> toAdd = ImmutableList.builder(); |
| 138 | ImmutableList.Builder<FlowRule> toRemove = ImmutableList.builder(); |
| 139 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 140 | if (fbe.operator() == ADD || fbe.operator() == MODIFY) { |
| 141 | toAdd.add(fbe.target()); |
| 142 | } else if (fbe.operator() == REMOVE) { |
| 143 | toRemove.add(fbe.target()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | ImmutableList<FlowRule> rulesToAdd = toAdd.build(); |
| 148 | ImmutableList<FlowRule> rulesToRemove = toRemove.build(); |
| 149 | |
Yuta HIGUCHI | 660aedf | 2017-05-01 20:23:25 -0700 | [diff] [blame] | 150 | Collection<FlowRule> added = ImmutableList.of(); |
| 151 | if (!rulesToAdd.isEmpty()) { |
| 152 | added = applyFlowRules(batch.deviceId(), rulesToAdd); |
| 153 | } |
| 154 | Collection<FlowRule> removed = ImmutableList.of(); |
| 155 | if (!rulesToRemove.isEmpty()) { |
| 156 | removed = removeFlowRules(batch.deviceId(), rulesToRemove); |
| 157 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 158 | |
| 159 | Set<FlowRule> failedRules = Sets.union(Sets.difference(copyOf(rulesToAdd), copyOf(added)), |
| 160 | Sets.difference(copyOf(rulesToRemove), copyOf(removed))); |
| 161 | CompletedBatchOperation status = |
| 162 | new CompletedBatchOperation(failedRules.isEmpty(), failedRules, batch.deviceId()); |
| 163 | providerService.batchOperationCompleted(batch.id(), status); |
| 164 | } |
| 165 | |
| 166 | private Multimap<DeviceId, FlowRule> rulesByDevice(FlowRule[] flowRules) { |
| 167 | // Sort the flow rules by device id |
| 168 | Multimap<DeviceId, FlowRule> rulesByDevice = LinkedListMultimap.create(); |
| 169 | for (FlowRule rule : flowRules) { |
| 170 | rulesByDevice.put(rule.deviceId(), rule); |
| 171 | } |
| 172 | return rulesByDevice; |
| 173 | } |
| 174 | |
| 175 | private Collection<FlowRule> applyFlowRules(DeviceId deviceId, Collection<FlowRule> flowRules) { |
| 176 | FlowRuleProgrammable programmer = getFlowRuleProgrammable(deviceId); |
| 177 | return programmer != null ? programmer.applyFlowRules(flowRules) : ImmutableList.of(); |
| 178 | } |
| 179 | |
| 180 | private Collection<FlowRule> removeFlowRules(DeviceId deviceId, Collection<FlowRule> flowRules) { |
| 181 | FlowRuleProgrammable programmer = getFlowRuleProgrammable(deviceId); |
| 182 | return programmer != null ? programmer.removeFlowRules(flowRules) : ImmutableList.of(); |
| 183 | } |
| 184 | |
| 185 | private FlowRuleProgrammable getFlowRuleProgrammable(DeviceId deviceId) { |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 186 | Device device = deviceService.getDevice(deviceId); |
| 187 | if (device.is(FlowRuleProgrammable.class)) { |
| 188 | return device.as(FlowRuleProgrammable.class); |
| 189 | } else { |
Andrea Campanella | fe1d473 | 2016-02-08 16:45:27 -0800 | [diff] [blame] | 190 | log.debug("Device {} is not flow rule programmable", deviceId); |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 191 | return null; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 192 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 195 | private void pollDeviceFlowEntries(Device device) { |
Yuta HIGUCHI | d4e08c4 | 2017-03-09 13:24:25 -0800 | [diff] [blame] | 196 | try { |
| 197 | providerService.pushFlowMetrics(device.id(), device.as(FlowRuleProgrammable.class).getFlowEntries()); |
| 198 | } catch (Exception e) { |
| 199 | log.warn("Exception thrown while polling {}", device.id(), e); |
| 200 | } |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 201 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 202 | |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 203 | private void pollTableStatistics(Device device) { |
| 204 | try { |
| 205 | List<TableStatisticsEntry> tableStatsList = newArrayList(device.as(TableStatisticsDiscovery.class) |
| 206 | .getTableStatistics()); |
| 207 | providerService.pushTableStatistics(device.id(), tableStatsList); |
| 208 | } catch (Exception e) { |
| 209 | log.warn("Exception thrown while polling table statistics for {}", device.id(), e); |
| 210 | } |
| 211 | } |
| 212 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 213 | private void pollFlowEntries() { |
Yuta HIGUCHI | d4e08c4 | 2017-03-09 13:24:25 -0800 | [diff] [blame] | 214 | try { |
| 215 | deviceService.getAvailableDevices().forEach(device -> { |
| 216 | if (mastershipService.isLocalMaster(device.id()) && device.is(FlowRuleProgrammable.class)) { |
| 217 | pollDeviceFlowEntries(device); |
| 218 | } |
hjtsao | 1e8a1bd | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 219 | if (mastershipService.isLocalMaster(device.id()) && device.is(TableStatisticsDiscovery.class)) { |
| 220 | pollTableStatistics(device); |
| 221 | } |
Yuta HIGUCHI | d4e08c4 | 2017-03-09 13:24:25 -0800 | [diff] [blame] | 222 | }); |
| 223 | } catch (Exception e) { |
| 224 | log.warn("Exception thrown while polling flows", e); |
| 225 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Yuta HIGUCHI | 076a788 | 2017-04-21 16:38:51 -0700 | [diff] [blame] | 228 | // potentially positive device event |
| 229 | private static final Set<DeviceEvent.Type> POSITIVE_DEVICE_EVENT = |
| 230 | Sets.immutableEnumSet(DEVICE_ADDED, |
| 231 | DEVICE_AVAILABILITY_CHANGED); |
| 232 | |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 233 | private class InternalDeviceListener implements DeviceListener { |
| 234 | |
| 235 | @Override |
| 236 | public void event(DeviceEvent event) { |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 237 | executor.execute(() -> handleEvent(event)); |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Yuta HIGUCHI | 076a788 | 2017-04-21 16:38:51 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public boolean isRelevant(DeviceEvent event) { |
| 242 | Device device = event.subject(); |
| 243 | return POSITIVE_DEVICE_EVENT.contains(event.type()) && |
| 244 | device.is(FlowRuleProgrammable.class); |
| 245 | } |
| 246 | |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 247 | private void handleEvent(DeviceEvent event) { |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 248 | Device device = event.subject(); |
Yuta HIGUCHI | 076a788 | 2017-04-21 16:38:51 -0700 | [diff] [blame] | 249 | boolean isRelevant = mastershipService.isLocalMaster(device.id()) && |
| 250 | deviceService.isAvailable(device.id()); |
| 251 | |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 252 | if (isRelevant) { |
Yuta HIGUCHI | 076a788 | 2017-04-21 16:38:51 -0700 | [diff] [blame] | 253 | pollDeviceFlowEntries(device); |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 254 | } |
Carmelo Cascone | f4363a0 | 2016-06-02 09:45:47 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 258 | } |