Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | |
| 17 | package org.onosproject.bmv2.demo.app.common; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.onosproject.app.ApplicationAdminService; |
| 31 | import org.onosproject.bmv2.api.context.Bmv2DeviceContext; |
| 32 | import org.onosproject.bmv2.api.service.Bmv2DeviceContextService; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Host; |
| 39 | import org.onosproject.net.Port; |
| 40 | import org.onosproject.net.device.DeviceEvent; |
| 41 | import org.onosproject.net.device.DeviceListener; |
| 42 | import org.onosproject.net.device.DeviceService; |
| 43 | import org.onosproject.net.flow.DefaultFlowRule; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.FlowRuleOperations; |
| 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.host.HostEvent; |
| 48 | import org.onosproject.net.host.HostListener; |
| 49 | import org.onosproject.net.host.HostService; |
| 50 | import org.onosproject.net.topology.Topology; |
| 51 | import org.onosproject.net.topology.TopologyEvent; |
| 52 | import org.onosproject.net.topology.TopologyGraph; |
| 53 | import org.onosproject.net.topology.TopologyListener; |
| 54 | import org.onosproject.net.topology.TopologyService; |
| 55 | import org.onosproject.net.topology.TopologyVertex; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | import java.util.Collection; |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
| 62 | import java.util.Set; |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 63 | import java.util.concurrent.ConcurrentMap; |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
| 66 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 67 | import java.util.concurrent.locks.Lock; |
| 68 | import java.util.concurrent.locks.ReentrantLock; |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 69 | import java.util.stream.Collectors; |
| 70 | import java.util.stream.Stream; |
| 71 | |
| 72 | import static com.google.common.base.Preconditions.checkNotNull; |
| 73 | import static java.util.stream.Collectors.toSet; |
| 74 | import static java.util.stream.Stream.concat; |
| 75 | import static org.onlab.util.Tools.groupedThreads; |
| 76 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 77 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 78 | import static org.slf4j.LoggerFactory.getLogger; |
| 79 | |
| 80 | /** |
| 81 | * Abstract implementation of an app providing fabric connectivity for a 2-stage Clos topology of BMv2 devices. |
| 82 | */ |
| 83 | @Component(immediate = true) |
| 84 | public abstract class AbstractUpgradableFabricApp { |
| 85 | |
| 86 | private static final Map<String, AbstractUpgradableFabricApp> APP_HANDLES = Maps.newConcurrentMap(); |
| 87 | |
| 88 | private static final int NUM_LEAFS = 3; |
| 89 | private static final int NUM_SPINES = 3; |
| 90 | private static final int FLOW_PRIORITY = 100; |
| 91 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 92 | private static final int CLEANUP_SLEEP = 2000; |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 93 | |
| 94 | protected final Logger log = getLogger(getClass()); |
| 95 | |
| 96 | private final TopologyListener topologyListener = new InternalTopologyListener(); |
| 97 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 98 | private final HostListener hostListener = new InternalHostListener(); |
| 99 | |
| 100 | private final ExecutorService executorService = Executors |
| 101 | .newFixedThreadPool(8, groupedThreads("onos/bmv2-demo-app", "bmv2-app-task", log)); |
| 102 | |
| 103 | private final String appName; |
| 104 | private final String configurationName; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected TopologyService topologyService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceService deviceService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | private HostService hostService; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | private FlowRuleService flowRuleService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | private ApplicationAdminService appService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | private CoreService coreService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | private Bmv2DeviceContextService bmv2ContextService; |
| 126 | |
| 127 | private boolean appActive = false; |
| 128 | private boolean appFreezed = false; |
| 129 | |
| 130 | private boolean otherAppFound = false; |
| 131 | private AbstractUpgradableFabricApp otherApp; |
| 132 | |
| 133 | private boolean flowRuleGenerated = false; |
| 134 | private ApplicationId appId; |
| 135 | |
| 136 | private Bmv2DeviceContext bmv2Context; |
| 137 | |
| 138 | private Set<DeviceId> leafSwitches; |
| 139 | private Set<DeviceId> spineSwitches; |
| 140 | |
| 141 | private Map<DeviceId, List<FlowRule>> deviceFlowRules; |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 142 | private Map<DeviceId, Bmv2DeviceContext> previousContexts; |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 143 | private Map<DeviceId, Boolean> contextFlags; |
| 144 | private Map<DeviceId, Boolean> ruleFlags; |
| 145 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 146 | private ConcurrentMap<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap(); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 149 | * Creates a new BMv2 fabric app. |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 150 | * |
| 151 | * @param appName app name |
| 152 | * @param configurationName a common name for the P4 program / BMv2 configuration used by this app |
| 153 | * @param context a BMv2 device context to be used on devices |
| 154 | */ |
| 155 | protected AbstractUpgradableFabricApp(String appName, String configurationName, Bmv2DeviceContext context) { |
| 156 | this.appName = checkNotNull(appName); |
| 157 | this.configurationName = checkNotNull(configurationName); |
| 158 | this.bmv2Context = checkNotNull(context); |
| 159 | } |
| 160 | |
| 161 | @Activate |
| 162 | public void activate() { |
| 163 | log.info("Starting..."); |
| 164 | |
| 165 | appActive = true; |
| 166 | appFreezed = false; |
| 167 | |
| 168 | if (APP_HANDLES.size() > 0) { |
| 169 | if (APP_HANDLES.size() > 1) { |
| 170 | throw new IllegalStateException("Found more than 1 active app handles"); |
| 171 | } |
| 172 | otherAppFound = true; |
| 173 | otherApp = APP_HANDLES.values().iterator().next(); |
| 174 | log.info("Found other fabric app active, signaling to freeze to {}...", otherApp.appName); |
| 175 | otherApp.setAppFreezed(true); |
| 176 | } |
| 177 | |
| 178 | APP_HANDLES.put(appName, this); |
| 179 | |
| 180 | appId = coreService.registerApplication(appName); |
| 181 | |
| 182 | topologyService.addListener(topologyListener); |
| 183 | deviceService.addListener(deviceListener); |
| 184 | hostService.addListener(hostListener); |
| 185 | |
| 186 | bmv2ContextService.registerInterpreterClassLoader(bmv2Context.interpreter().getClass(), |
| 187 | this.getClass().getClassLoader()); |
| 188 | |
| 189 | init(); |
| 190 | |
| 191 | log.info("STARTED", appId.id()); |
| 192 | } |
| 193 | |
| 194 | @Deactivate |
| 195 | public void deactivate() { |
| 196 | log.info("Stopping..."); |
| 197 | try { |
| 198 | executorService.shutdown(); |
| 199 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 200 | } catch (InterruptedException e) { |
| 201 | List<Runnable> runningTasks = executorService.shutdownNow(); |
| 202 | log.warn("Unable to stop the following tasks: {}", runningTasks); |
| 203 | } |
| 204 | deviceService.removeListener(deviceListener); |
| 205 | topologyService.removeListener(topologyListener); |
| 206 | hostService.removeListener(hostListener); |
| 207 | flowRuleService.removeFlowRulesById(appId); |
| 208 | |
| 209 | appActive = false; |
| 210 | APP_HANDLES.remove(appName); |
| 211 | |
| 212 | log.info("STOPPED"); |
| 213 | } |
| 214 | |
| 215 | private void init() { |
| 216 | |
| 217 | // Reset any previous state |
| 218 | synchronized (this) { |
| 219 | flowRuleGenerated = Boolean.FALSE; |
| 220 | leafSwitches = Sets.newHashSet(); |
| 221 | spineSwitches = Sets.newHashSet(); |
| 222 | deviceFlowRules = Maps.newConcurrentMap(); |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 223 | ruleFlags = Maps.newConcurrentMap(); |
| 224 | contextFlags = Maps.newConcurrentMap(); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Start flow rules generator... |
| 228 | spawnTask(() -> generateFlowRules(topologyService.currentTopology(), Sets.newHashSet(hostService.getHosts()))); |
| 229 | } |
| 230 | |
| 231 | private void setAppFreezed(boolean appFreezed) { |
| 232 | this.appFreezed = appFreezed; |
| 233 | if (appFreezed) { |
| 234 | log.info("Freezing..."); |
| 235 | } else { |
| 236 | log.info("Unfreezing...!"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Perform device initialization. Returns true if the operation was successful, false otherwise. |
| 242 | * |
| 243 | * @param deviceId a device id |
| 244 | * @return a boolean value |
| 245 | */ |
| 246 | public abstract boolean initDevice(DeviceId deviceId); |
| 247 | |
| 248 | /** |
| 249 | * Generates a list of flow rules for the given leaf switch, source host, destination hosts, spine switches and |
| 250 | * topology. |
| 251 | * |
| 252 | * @param leaf a leaf device id |
| 253 | * @param srcHost a source host |
| 254 | * @param dstHosts a collection of destination hosts |
| 255 | * @param spines a collection of spine device IDs |
| 256 | * @param topology a topology |
| 257 | * @return a list of flow rules |
| 258 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 259 | */ |
| 260 | public abstract List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts, |
| 261 | Collection<DeviceId> spines, Topology topology) |
| 262 | throws FlowRuleGeneratorException; |
| 263 | |
| 264 | /** |
| 265 | * Generates a list of flow rules for the given spine switch, destination hosts and topology. |
| 266 | * |
| 267 | * @param deviceId a spine device id |
| 268 | * @param dstHosts a collection of destination hosts |
| 269 | * @param topology a topology |
| 270 | * @return a list of flow rules |
| 271 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 272 | */ |
| 273 | public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topology) |
| 274 | throws FlowRuleGeneratorException; |
| 275 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 276 | private void deployAllDevices() { |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 277 | if (otherAppFound && otherApp.appActive) { |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 278 | log.info("Deactivating other app..."); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 279 | appService.deactivate(otherApp.appId); |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 280 | try { |
| 281 | Thread.sleep(CLEANUP_SLEEP); |
| 282 | } catch (InterruptedException e) { |
| 283 | log.warn("Cleanup sleep interrupted!"); |
| 284 | Thread.interrupted(); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 285 | } |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 286 | } |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 287 | |
| 288 | Stream.concat(leafSwitches.stream(), spineSwitches.stream()) |
| 289 | .map(deviceService::getDevice) |
| 290 | .forEach(device -> spawnTask(() -> deployDevice(device))); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Executes a device deploy. |
| 295 | * |
| 296 | * @param device a device |
| 297 | */ |
| 298 | public void deployDevice(Device device) { |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 299 | |
| 300 | DeviceId deviceId = device.id(); |
| 301 | |
| 302 | // Synchronize executions over the same device. |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 303 | Lock lock = deviceLocks.computeIfAbsent(deviceId, k -> new ReentrantLock()); |
| 304 | lock.lock(); |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 305 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 306 | try { |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 307 | // Set context if not already done. |
| 308 | if (!contextFlags.getOrDefault(deviceId, false)) { |
| 309 | log.info("Setting context to {} for {}...", configurationName, deviceId); |
| 310 | bmv2ContextService.setContext(deviceId, bmv2Context); |
| 311 | contextFlags.put(device.id(), true); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 314 | // Initialize device. |
| 315 | if (!initDevice(deviceId)) { |
| 316 | log.warn("Failed to initialize device {}", deviceId); |
| 317 | } |
| 318 | |
| 319 | // Install rules. |
| 320 | if (!ruleFlags.getOrDefault(deviceId, false)) { |
| 321 | List<FlowRule> rules = deviceFlowRules.getOrDefault(deviceId, Collections.emptyList()); |
| 322 | if (rules.size() > 0) { |
| 323 | log.info("Installing rules for {}...", deviceId); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 324 | installFlowRules(rules); |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 325 | ruleFlags.put(deviceId, true); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 328 | } finally { |
| 329 | lock.unlock(); |
Carmelo Cascone | c18e82c | 2016-06-16 14:22:36 -0700 | [diff] [blame] | 330 | } |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | private void spawnTask(Runnable task) { |
| 334 | executorService.execute(task); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | private void installFlowRules(Collection<FlowRule> rules) { |
| 339 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 340 | rules.forEach(opsBuilder::add); |
| 341 | flowRuleService.apply(opsBuilder.build()); |
| 342 | } |
| 343 | |
| 344 | private void removeFlowRules(Collection<FlowRule> rules) { |
| 345 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 346 | rules.forEach(opsBuilder::remove); |
| 347 | flowRuleService.apply(opsBuilder.build()); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Generates the flow rules to provide host-to-host connectivity for the given topology and hosts. |
| 352 | * |
| 353 | * @param topo a topology |
| 354 | * @param hosts a collection of hosts |
| 355 | */ |
| 356 | private synchronized void generateFlowRules(Topology topo, Collection<Host> hosts) { |
| 357 | |
| 358 | if (flowRuleGenerated) { |
| 359 | log.debug("Flow rules have been already generated, aborting..."); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | log.debug("Starting flow rules generator..."); |
| 364 | |
| 365 | TopologyGraph graph = topologyService.getGraph(topo); |
| 366 | Set<DeviceId> spines = Sets.newHashSet(); |
| 367 | Set<DeviceId> leafs = Sets.newHashSet(); |
| 368 | graph.getVertexes().stream() |
| 369 | .map(TopologyVertex::deviceId) |
| 370 | .forEach(did -> (isSpine(did, topo) ? spines : leafs).add(did)); |
| 371 | |
| 372 | if (spines.size() != NUM_SPINES || leafs.size() != NUM_LEAFS) { |
| 373 | log.info("Invalid leaf/spine switches count, aborting... > leafCount={}, spineCount={}", |
| 374 | spines.size(), leafs.size()); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | for (DeviceId did : spines) { |
| 379 | int portCount = deviceService.getPorts(did).size(); |
| 380 | // Expected port count: num leafs + 1 redundant leaf link |
| 381 | if (portCount != (NUM_LEAFS + 1)) { |
| 382 | log.info("Invalid port count for spine, aborting... > deviceId={}, portCount={}", did, portCount); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | for (DeviceId did : leafs) { |
| 387 | int portCount = deviceService.getPorts(did).size(); |
| 388 | // Expected port count: num spines + host port + 1 redundant spine link |
| 389 | if (portCount != (NUM_SPINES + 2)) { |
| 390 | log.info("Invalid port count for leaf, aborting... > deviceId={}, portCount={}", did, portCount); |
| 391 | return; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Check hosts, number and exactly one per leaf |
| 396 | Map<DeviceId, Host> hostMap = Maps.newHashMap(); |
| 397 | hosts.forEach(h -> hostMap.put(h.location().deviceId(), h)); |
| 398 | if (hosts.size() != NUM_LEAFS || !leafs.equals(hostMap.keySet())) { |
| 399 | log.info("Wrong host configuration, aborting... > hostCount={}, hostMapz={}", hosts.size(), hostMap); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | List<FlowRule> newFlowRules = Lists.newArrayList(); |
| 404 | |
| 405 | try { |
| 406 | for (DeviceId deviceId : leafs) { |
| 407 | Host srcHost = hostMap.get(deviceId); |
| 408 | Set<Host> dstHosts = hosts.stream().filter(h -> h != srcHost).collect(toSet()); |
| 409 | newFlowRules.addAll(generateLeafRules(deviceId, srcHost, dstHosts, spines, topo)); |
| 410 | } |
| 411 | for (DeviceId deviceId : spines) { |
| 412 | newFlowRules.addAll(generateSpineRules(deviceId, hosts, topo)); |
| 413 | } |
| 414 | } catch (FlowRuleGeneratorException e) { |
| 415 | log.warn("Exception while executing flow rule generator: ", e.toString()); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | if (newFlowRules.size() == 0) { |
| 420 | // Something went wrong |
| 421 | log.error("0 flow rules generated, BUG?"); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | // All good! |
| 426 | // Divide flow rules per device id... |
| 427 | ImmutableMap.Builder<DeviceId, List<FlowRule>> mapBuilder = ImmutableMap.builder(); |
| 428 | concat(spines.stream(), leafs.stream()) |
| 429 | .map(deviceId -> ImmutableList.copyOf(newFlowRules |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 430 | .stream() |
| 431 | .filter(fr -> fr.deviceId().equals(deviceId)) |
| 432 | .iterator())) |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 433 | .forEach(frs -> mapBuilder.put(frs.get(0).deviceId(), frs)); |
| 434 | this.deviceFlowRules = mapBuilder.build(); |
| 435 | |
| 436 | this.leafSwitches = ImmutableSet.copyOf(leafs); |
| 437 | this.spineSwitches = ImmutableSet.copyOf(spines); |
| 438 | |
| 439 | // Avoid other executions to modify the generated flow rules. |
| 440 | flowRuleGenerated = true; |
| 441 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 442 | log.info("Generated {} flow rules for {} devices", newFlowRules.size(), spines.size() + leafs.size()); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 443 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 444 | spawnTask(this::deployAllDevices); |
Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Returns a new, pre-configured flow rule builder. |
| 449 | * |
| 450 | * @param did a device id |
| 451 | * @param tableName a table name |
| 452 | * @return a new flow rule builder |
| 453 | */ |
| 454 | protected FlowRule.Builder flowRuleBuilder(DeviceId did, String tableName) throws FlowRuleGeneratorException { |
| 455 | Map<String, Integer> tableMap = bmv2Context.interpreter().tableIdMap().inverse(); |
| 456 | if (tableMap.get(tableName) == null) { |
| 457 | throw new FlowRuleGeneratorException("Unknown table " + tableName); |
| 458 | } |
| 459 | return DefaultFlowRule.builder() |
| 460 | .forDevice(did) |
| 461 | .forTable(tableMap.get(tableName)) |
| 462 | .fromApp(appId) |
| 463 | .withPriority(FLOW_PRIORITY) |
| 464 | .makePermanent(); |
| 465 | } |
| 466 | |
| 467 | private List<Port> getHostPorts(DeviceId deviceId, Topology topology) { |
| 468 | // Get all non-fabric ports. |
| 469 | return deviceService |
| 470 | .getPorts(deviceId) |
| 471 | .stream() |
| 472 | .filter(p -> !isFabricPort(p, topology)) |
| 473 | .collect(Collectors.toList()); |
| 474 | } |
| 475 | |
| 476 | private boolean isSpine(DeviceId deviceId, Topology topology) { |
| 477 | // True if all ports are fabric. |
| 478 | return getHostPorts(deviceId, topology).size() == 0; |
| 479 | } |
| 480 | |
| 481 | protected boolean isFabricPort(Port port, Topology topology) { |
| 482 | // True if the port connects this device to another infrastructure device. |
| 483 | return topologyService.isInfrastructure(topology, new ConnectPoint(port.element().id(), port.number())); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * A listener of topology events that executes a flow rule generation task each time a device is added. |
| 488 | */ |
| 489 | private class InternalTopologyListener implements TopologyListener { |
| 490 | |
| 491 | @Override |
| 492 | public void event(TopologyEvent event) { |
| 493 | spawnTask(() -> generateFlowRules(event.subject(), Sets.newHashSet(hostService.getHosts()))); |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public boolean isRelevant(TopologyEvent event) { |
| 498 | return !appFreezed && |
| 499 | // If at least one reason is of type DEVICE_ADDED. |
| 500 | event.reasons().stream(). |
| 501 | filter(r -> r instanceof DeviceEvent) |
| 502 | .filter(r -> ((DeviceEvent) r).type() == DEVICE_ADDED) |
| 503 | .findAny() |
| 504 | .isPresent(); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * A listener of device events that executes a device deploy task each time a device is added, updated or |
| 510 | * re-connects. |
| 511 | */ |
| 512 | private class InternalDeviceListener implements DeviceListener { |
| 513 | @Override |
| 514 | public void event(DeviceEvent event) { |
| 515 | spawnTask(() -> deployDevice(event.subject())); |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public boolean isRelevant(DeviceEvent event) { |
| 520 | return !appFreezed && |
| 521 | (event.type() == DEVICE_ADDED || |
| 522 | event.type() == DEVICE_UPDATED || |
| 523 | (event.type() == DEVICE_AVAILABILITY_CHANGED && |
| 524 | deviceService.isAvailable(event.subject().id()))); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * A listener of host events that generates flow rules each time a new host is added. |
| 530 | */ |
| 531 | private class InternalHostListener implements HostListener { |
| 532 | @Override |
| 533 | public void event(HostEvent event) { |
| 534 | spawnTask(() -> generateFlowRules(topologyService.currentTopology(), |
| 535 | Sets.newHashSet(hostService.getHosts()))); |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public boolean isRelevant(HostEvent event) { |
| 540 | return !appFreezed && event.type() == HOST_ADDED; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * An exception occurred while generating flow rules for this fabric. |
| 546 | */ |
| 547 | public class FlowRuleGeneratorException extends Exception { |
| 548 | |
| 549 | public FlowRuleGeneratorException() { |
| 550 | } |
| 551 | |
| 552 | public FlowRuleGeneratorException(String msg) { |
| 553 | super(msg); |
| 554 | } |
| 555 | |
| 556 | public FlowRuleGeneratorException(Exception cause) { |
| 557 | super(cause); |
| 558 | } |
| 559 | } |
| 560 | } |