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