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 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Maps; |
| 25 | import com.google.common.collect.Sets; |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.onosproject.app.ApplicationAdminService; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.device.DeviceEvent; |
| 40 | import org.onosproject.net.device.DeviceListener; |
| 41 | import org.onosproject.net.device.DeviceService; |
| 42 | import org.onosproject.net.flow.DefaultFlowRule; |
| 43 | import org.onosproject.net.flow.FlowRule; |
| 44 | import org.onosproject.net.flow.FlowRuleOperations; |
| 45 | import org.onosproject.net.flow.FlowRuleService; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 46 | import org.onosproject.net.host.HostService; |
| 47 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 48 | import org.onosproject.net.pi.model.PiPipeconfId; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 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; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyGraph; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 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; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
| 61 | import java.util.Set; |
| 62 | import java.util.concurrent.ConcurrentMap; |
| 63 | import java.util.concurrent.ExecutorService; |
| 64 | import java.util.concurrent.Executors; |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 65 | import java.util.concurrent.ScheduledExecutorService; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 66 | import java.util.concurrent.TimeUnit; |
| 67 | import java.util.concurrent.locks.Lock; |
| 68 | import java.util.concurrent.locks.ReentrantLock; |
| 69 | import java.util.stream.Collectors; |
| 70 | import java.util.stream.Stream; |
| 71 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 72 | import static com.google.common.base.Preconditions.checkArgument; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 74 | import static java.lang.String.format; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 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.*; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 79 | import static org.slf4j.LoggerFactory.getLogger; |
| 80 | |
| 81 | /** |
| 82 | * Abstract implementation of an app providing fabric connectivity for a 2-stage Clos topology of P4Runtime devices. |
| 83 | */ |
| 84 | @Component(immediate = true) |
| 85 | public abstract class AbstractUpgradableFabricApp { |
| 86 | |
| 87 | private static final Map<String, AbstractUpgradableFabricApp> APP_HANDLES = Maps.newConcurrentMap(); |
| 88 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 89 | // TOPO_SIZE should be the same of the --size argument when running bmv2-demo.py |
| 90 | private static final int TOPO_SIZE = 2; |
| 91 | private static final boolean WITH_IMBALANCED_STRIPING = false; |
| 92 | protected static final int HASHED_LINKS = TOPO_SIZE + (WITH_IMBALANCED_STRIPING ? 1 : 0); |
| 93 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 94 | private static final int FLOW_PRIORITY = 100; |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 95 | private static final int CHECK_TOPOLOGY_INTERVAL_SECONDS = 5; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 96 | |
| 97 | private static final int CLEANUP_SLEEP = 2000; |
| 98 | |
| 99 | protected final Logger log = getLogger(getClass()); |
| 100 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 101 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 102 | |
| 103 | private final ExecutorService executorService = Executors |
| 104 | .newFixedThreadPool(8, groupedThreads("onos/pi-demo-app", "pi-app-task", log)); |
| 105 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 106 | private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); |
| 107 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 108 | private final String appName; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected TopologyService topologyService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected DeviceService deviceService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | private HostService hostService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | private FlowRuleService flowRuleService; |
| 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | private ApplicationAdminService appService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | private CoreService coreService; |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | private PiPipeconfService piPipeconfService; |
| 130 | |
| 131 | private boolean appActive = false; |
| 132 | private boolean appFreezed = false; |
| 133 | |
| 134 | private boolean otherAppFound = false; |
| 135 | private AbstractUpgradableFabricApp otherApp; |
| 136 | |
| 137 | private boolean flowRuleGenerated = false; |
| 138 | private ApplicationId appId; |
| 139 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 140 | private Collection<PiPipeconf> appPipeconfs; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 141 | |
| 142 | private Set<DeviceId> leafSwitches; |
| 143 | private Set<DeviceId> spineSwitches; |
| 144 | |
| 145 | private Map<DeviceId, List<FlowRule>> deviceFlowRules; |
| 146 | private Map<DeviceId, Boolean> pipeconfFlags; |
| 147 | private Map<DeviceId, Boolean> ruleFlags; |
| 148 | |
| 149 | private ConcurrentMap<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap(); |
| 150 | |
| 151 | /** |
| 152 | * Creates a new PI fabric app. |
| 153 | * |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 154 | * @param appName app name |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 155 | * @param appPipeconfs collection of compatible pipeconfs |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 156 | */ |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 157 | protected AbstractUpgradableFabricApp(String appName, Collection<PiPipeconf> appPipeconfs) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 158 | this.appName = checkNotNull(appName); |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 159 | this.appPipeconfs = checkNotNull(appPipeconfs); |
| 160 | checkArgument(appPipeconfs.size() > 0, "appPipeconfs cannot have size 0"); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Activate |
| 164 | public void activate() { |
| 165 | log.info("Starting..."); |
| 166 | |
| 167 | appActive = true; |
| 168 | appFreezed = false; |
| 169 | |
| 170 | if (APP_HANDLES.size() > 0) { |
| 171 | if (APP_HANDLES.size() > 1) { |
| 172 | throw new IllegalStateException("Found more than 1 active app handles"); |
| 173 | } |
| 174 | otherAppFound = true; |
| 175 | otherApp = APP_HANDLES.values().iterator().next(); |
| 176 | log.info("Found other fabric app active, signaling to freeze to {}...", otherApp.appName); |
| 177 | otherApp.setAppFreezed(true); |
| 178 | } |
| 179 | |
| 180 | APP_HANDLES.put(appName, this); |
| 181 | |
| 182 | appId = coreService.registerApplication(appName); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 183 | deviceService.addListener(deviceListener); |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 184 | appPipeconfs.forEach(piPipeconfService::register); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 185 | |
| 186 | init(); |
| 187 | |
| 188 | log.info("STARTED", appId.id()); |
| 189 | } |
| 190 | |
| 191 | @Deactivate |
| 192 | public void deactivate() { |
| 193 | log.info("Stopping..."); |
| 194 | try { |
| 195 | executorService.shutdown(); |
| 196 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 197 | } catch (InterruptedException e) { |
| 198 | List<Runnable> runningTasks = executorService.shutdownNow(); |
| 199 | log.warn("Unable to stop the following tasks: {}", runningTasks); |
| 200 | } |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 201 | scheduledExecutorService.shutdown(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 202 | deviceService.removeListener(deviceListener); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 203 | flowRuleService.removeFlowRulesById(appId); |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 204 | appPipeconfs.stream() |
| 205 | .map(PiPipeconf::id) |
| 206 | .forEach(piPipeconfService::remove); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 207 | |
| 208 | appActive = false; |
| 209 | APP_HANDLES.remove(appName); |
| 210 | |
| 211 | log.info("STOPPED"); |
| 212 | } |
| 213 | |
| 214 | private void init() { |
| 215 | |
| 216 | // Reset any previous state |
| 217 | synchronized (this) { |
| 218 | flowRuleGenerated = Boolean.FALSE; |
| 219 | leafSwitches = Sets.newHashSet(); |
| 220 | spineSwitches = Sets.newHashSet(); |
| 221 | deviceFlowRules = Maps.newConcurrentMap(); |
| 222 | ruleFlags = Maps.newConcurrentMap(); |
| 223 | pipeconfFlags = Maps.newConcurrentMap(); |
| 224 | } |
| 225 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 226 | /* |
| 227 | Schedules a thread that periodically checks the topology, as soon as it corresponds to the expected |
| 228 | one, it generates the necessary flow rules and starts the deploy process on each device. |
| 229 | */ |
| 230 | scheduledExecutorService.scheduleAtFixedRate(this::checkTopologyAndGenerateFlowRules, |
| 231 | 0, CHECK_TOPOLOGY_INTERVAL_SECONDS, TimeUnit.SECONDS); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | private void setAppFreezed(boolean appFreezed) { |
| 235 | this.appFreezed = appFreezed; |
| 236 | if (appFreezed) { |
| 237 | log.info("Freezing..."); |
| 238 | } else { |
| 239 | log.info("Unfreezing...!"); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Perform device initialization. Returns true if the operation was successful, false otherwise. |
| 245 | * |
| 246 | * @param deviceId a device id |
| 247 | * @return a boolean value |
| 248 | */ |
| 249 | public abstract boolean initDevice(DeviceId deviceId); |
| 250 | |
| 251 | /** |
| 252 | * Generates a list of flow rules for the given leaf switch, source host, destination hosts, spine switches and |
| 253 | * topology. |
| 254 | * |
| 255 | * @param leaf a leaf device id |
| 256 | * @param srcHost a source host |
| 257 | * @param dstHosts a collection of destination hosts |
| 258 | * @param spines a collection of spine device IDs |
| 259 | * @param topology a topology |
| 260 | * @return a list of flow rules |
| 261 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 262 | */ |
| 263 | public abstract List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts, |
| 264 | Collection<DeviceId> spines, Topology topology) |
| 265 | throws FlowRuleGeneratorException; |
| 266 | |
| 267 | /** |
| 268 | * Generates a list of flow rules for the given spine switch, destination hosts and topology. |
| 269 | * |
| 270 | * @param deviceId a spine device id |
| 271 | * @param dstHosts a collection of destination hosts |
| 272 | * @param topology a topology |
| 273 | * @return a list of flow rules |
| 274 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 275 | */ |
| 276 | public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topology) |
| 277 | throws FlowRuleGeneratorException; |
| 278 | |
| 279 | private void deployAllDevices() { |
| 280 | if (otherAppFound && otherApp.appActive) { |
| 281 | log.info("Deactivating other app..."); |
| 282 | appService.deactivate(otherApp.appId); |
| 283 | try { |
| 284 | Thread.sleep(CLEANUP_SLEEP); |
| 285 | } catch (InterruptedException e) { |
| 286 | log.warn("Cleanup sleep interrupted!"); |
| 287 | Thread.interrupted(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | Stream.concat(leafSwitches.stream(), spineSwitches.stream()) |
| 292 | .map(deviceService::getDevice) |
| 293 | .forEach(device -> spawnTask(() -> deployDevice(device))); |
| 294 | } |
| 295 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 296 | private boolean matchPipeconf(PiPipeconfId piPipeconfId) { |
| 297 | return appPipeconfs.stream() |
| 298 | .anyMatch(p -> p.id().equals(piPipeconfId)); |
| 299 | } |
| 300 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 301 | /** |
| 302 | * Executes a device deploy. |
| 303 | * |
| 304 | * @param device a device |
| 305 | */ |
| 306 | public void deployDevice(Device device) { |
| 307 | |
| 308 | DeviceId deviceId = device.id(); |
| 309 | |
| 310 | // Synchronize executions over the same device. |
| 311 | Lock lock = deviceLocks.computeIfAbsent(deviceId, k -> new ReentrantLock()); |
| 312 | lock.lock(); |
| 313 | |
| 314 | try { |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 315 | // Set pipeconf flag if not already done. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 316 | if (!pipeconfFlags.getOrDefault(deviceId, false)) { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 317 | if (piPipeconfService.ofDevice(deviceId).isPresent() && |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 318 | matchPipeconf(piPipeconfService.ofDevice(deviceId).get())) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 319 | pipeconfFlags.put(device.id(), true); |
| 320 | } else { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 321 | log.warn("Wrong pipeconf for {}, expecting {}, but found {}, aborting deploy", |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 322 | deviceId, MoreObjects.toStringHelper(appPipeconfs), |
| 323 | piPipeconfService.ofDevice(deviceId).get()); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 324 | return; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | // Initialize device. |
| 329 | if (!initDevice(deviceId)) { |
| 330 | log.warn("Failed to initialize device {}", deviceId); |
| 331 | } |
| 332 | |
| 333 | // Install rules. |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 334 | if (!ruleFlags.getOrDefault(deviceId, false) && |
| 335 | deviceFlowRules.containsKey(deviceId)) { |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 336 | log.info("Installing {} rules for {}...", deviceFlowRules.get(deviceId).size(), deviceId); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 337 | installFlowRules(deviceFlowRules.get(deviceId)); |
| 338 | ruleFlags.put(deviceId, true); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 339 | } |
| 340 | } finally { |
| 341 | lock.unlock(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private void spawnTask(Runnable task) { |
| 346 | executorService.execute(task); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | private void installFlowRules(Collection<FlowRule> rules) { |
| 351 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 352 | rules.forEach(opsBuilder::add); |
| 353 | flowRuleService.apply(opsBuilder.build()); |
| 354 | } |
| 355 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 356 | /** |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 357 | * Generates flow rules to provide host-to-host connectivity for the given topology and hosts. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 358 | */ |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 359 | private synchronized void checkTopologyAndGenerateFlowRules() { |
| 360 | |
| 361 | Topology topo = topologyService.currentTopology(); |
| 362 | Set<Host> hosts = Sets.newHashSet(hostService.getHosts()); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 363 | |
| 364 | if (flowRuleGenerated) { |
| 365 | log.debug("Flow rules have been already generated, aborting..."); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | log.debug("Starting flow rules generator..."); |
| 370 | |
| 371 | TopologyGraph graph = topologyService.getGraph(topo); |
| 372 | Set<DeviceId> spines = Sets.newHashSet(); |
| 373 | Set<DeviceId> leafs = Sets.newHashSet(); |
| 374 | graph.getVertexes().stream() |
| 375 | .map(TopologyVertex::deviceId) |
| 376 | .forEach(did -> (isSpine(did, topo) ? spines : leafs).add(did)); |
| 377 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 378 | if (spines.size() != TOPO_SIZE || leafs.size() != TOPO_SIZE) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 379 | log.info("Invalid leaf/spine switches count, aborting... > leafCount={}, spineCount={}", |
| 380 | spines.size(), leafs.size()); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | for (DeviceId did : spines) { |
| 385 | int portCount = deviceService.getPorts(did).size(); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 386 | // Expected port count: num leafs + 1 redundant leaf link (if imbalanced) |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 387 | if (portCount != HASHED_LINKS) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 388 | log.info("Invalid port count for spine, aborting... > deviceId={}, portCount={}", did, portCount); |
| 389 | return; |
| 390 | } |
| 391 | } |
| 392 | for (DeviceId did : leafs) { |
| 393 | int portCount = deviceService.getPorts(did).size(); |
| 394 | // Expected port count: num spines + host port + 1 redundant spine link |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 395 | if (portCount != HASHED_LINKS + 1) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 396 | log.info("Invalid port count for leaf, aborting... > deviceId={}, portCount={}", did, portCount); |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Check hosts, number and exactly one per leaf |
| 402 | Map<DeviceId, Host> hostMap = Maps.newHashMap(); |
| 403 | hosts.forEach(h -> hostMap.put(h.location().deviceId(), h)); |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 404 | if (hosts.size() != TOPO_SIZE || !leafs.equals(hostMap.keySet())) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 405 | log.info("Wrong host configuration, aborting... > hostCount={}, hostMapz={}", hosts.size(), hostMap); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | List<FlowRule> newFlowRules = Lists.newArrayList(); |
| 410 | |
| 411 | try { |
| 412 | for (DeviceId deviceId : leafs) { |
| 413 | Host srcHost = hostMap.get(deviceId); |
| 414 | Set<Host> dstHosts = hosts.stream().filter(h -> h != srcHost).collect(toSet()); |
| 415 | newFlowRules.addAll(generateLeafRules(deviceId, srcHost, dstHosts, spines, topo)); |
| 416 | } |
| 417 | for (DeviceId deviceId : spines) { |
| 418 | newFlowRules.addAll(generateSpineRules(deviceId, hosts, topo)); |
| 419 | } |
| 420 | } catch (FlowRuleGeneratorException e) { |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 421 | log.warn("Exception while executing flow rule generator: {}", e.getMessage()); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (newFlowRules.size() == 0) { |
| 426 | // Something went wrong |
| 427 | log.error("0 flow rules generated, BUG?"); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | // All good! |
| 432 | // Divide flow rules per device id... |
| 433 | ImmutableMap.Builder<DeviceId, List<FlowRule>> mapBuilder = ImmutableMap.builder(); |
| 434 | concat(spines.stream(), leafs.stream()) |
| 435 | .map(deviceId -> ImmutableList.copyOf(newFlowRules |
| 436 | .stream() |
| 437 | .filter(fr -> fr.deviceId().equals(deviceId)) |
| 438 | .iterator())) |
| 439 | .forEach(frs -> mapBuilder.put(frs.get(0).deviceId(), frs)); |
| 440 | this.deviceFlowRules = mapBuilder.build(); |
| 441 | |
| 442 | this.leafSwitches = ImmutableSet.copyOf(leafs); |
| 443 | this.spineSwitches = ImmutableSet.copyOf(spines); |
| 444 | |
| 445 | // Avoid other executions to modify the generated flow rules. |
| 446 | flowRuleGenerated = true; |
| 447 | |
| 448 | log.info("Generated {} flow rules for {} devices", newFlowRules.size(), spines.size() + leafs.size()); |
| 449 | |
| 450 | spawnTask(this::deployAllDevices); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Returns a new, pre-configured flow rule builder. |
| 455 | * |
| 456 | * @param did a device id |
| 457 | * @param tableName a table name |
| 458 | * @return a new flow rule builder |
| 459 | */ |
| 460 | protected FlowRule.Builder flowRuleBuilder(DeviceId did, String tableName) throws FlowRuleGeneratorException { |
| 461 | |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 462 | final Device device = deviceService.getDevice(did); |
| 463 | if (!device.is(PiPipelineInterpreter.class)) { |
| 464 | throw new FlowRuleGeneratorException(format("Device %s has no PiPipelineInterpreter", did)); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 465 | } |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 466 | final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class); |
| 467 | final int flowRuleTableId; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 468 | if (interpreter.mapPiTableId(PiTableId.of(tableName)).isPresent()) { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 469 | flowRuleTableId = interpreter.mapPiTableId(PiTableId.of(tableName)).get(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 470 | } else { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 471 | throw new FlowRuleGeneratorException(format("Unknown table %s in interpreter", tableName)); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 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 | /** |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 502 | * A listener of device events that executes a device deploy task each time a device is added, updated or |
| 503 | * re-connects. |
| 504 | */ |
| 505 | private class InternalDeviceListener implements DeviceListener { |
| 506 | @Override |
| 507 | public void event(DeviceEvent event) { |
| 508 | spawnTask(() -> deployDevice(event.subject())); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public boolean isRelevant(DeviceEvent event) { |
| 513 | return !appFreezed && |
| 514 | (event.type() == DEVICE_ADDED || |
| 515 | event.type() == DEVICE_UPDATED || |
| 516 | (event.type() == DEVICE_AVAILABILITY_CHANGED && |
| 517 | deviceService.isAvailable(event.subject().id()))); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /** |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 522 | * An exception occurred while generating flow rules for this fabric. |
| 523 | */ |
| 524 | public class FlowRuleGeneratorException extends Exception { |
| 525 | |
| 526 | public FlowRuleGeneratorException() { |
| 527 | } |
| 528 | |
| 529 | public FlowRuleGeneratorException(String msg) { |
| 530 | super(msg); |
| 531 | } |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 532 | } |
| 533 | } |