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