Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | package org.onosproject.segmentrouting.xconnect.impl; |
| 17 | |
Charles Chan | d5814aa | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.codec.CodecService; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.config.NetworkConfigService; |
| 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.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.flow.criteria.Criteria; |
| 46 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 47 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 49 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 50 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 51 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 52 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 53 | import org.onosproject.net.flowobjective.NextObjective; |
| 54 | import org.onosproject.net.flowobjective.Objective; |
| 55 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 56 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 57 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 58 | import org.onosproject.segmentrouting.xconnect.api.XconnectCodec; |
| 59 | import org.onosproject.segmentrouting.xconnect.api.XconnectDesc; |
| 60 | import org.onosproject.segmentrouting.xconnect.api.XconnectKey; |
| 61 | import org.onosproject.segmentrouting.xconnect.api.XconnectService; |
| 62 | import org.onosproject.store.serializers.KryoNamespaces; |
| 63 | import org.onosproject.store.service.ConsistentMap; |
| 64 | import org.onosproject.store.service.MapEvent; |
| 65 | import org.onosproject.store.service.MapEventListener; |
| 66 | import org.onosproject.store.service.Serializer; |
| 67 | import org.onosproject.store.service.StorageService; |
| 68 | import org.onosproject.store.service.Versioned; |
| 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
| 72 | import java.io.Serializable; |
| 73 | import java.util.Set; |
| 74 | import java.util.concurrent.CompletableFuture; |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 77 | import java.util.function.BiConsumer; |
| 78 | import java.util.function.Consumer; |
| 79 | import java.util.stream.Collectors; |
| 80 | |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 81 | import static org.onlab.util.Tools.groupedThreads; |
| 82 | |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 83 | @Service |
| 84 | @Component(immediate = true) |
| 85 | public class XconnectManager implements XconnectService { |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | private CoreService coreService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | private CodecService codecService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | private StorageService storageService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | public NetworkConfigService netCfgService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | public DeviceService deviceService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | public FlowObjectiveService flowObjectiveService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | public MastershipService mastershipService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) |
| 108 | public SegmentRoutingService srService; |
| 109 | |
| 110 | private static final String APP_NAME = "org.onosproject.xconnect"; |
| 111 | private static final String ERROR_NOT_MASTER = "Not master controller"; |
| 112 | |
| 113 | private static Logger log = LoggerFactory.getLogger(XconnectManager.class); |
| 114 | |
| 115 | private ApplicationId appId; |
| 116 | private ConsistentMap<XconnectKey, Set<PortNumber>> xconnectStore; |
| 117 | private ConsistentMap<XconnectKey, NextObjective> xconnectNextObjStore; |
| 118 | |
| 119 | private final MapEventListener<XconnectKey, Set<PortNumber>> xconnectListener = new XconnectMapListener(); |
| 120 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 121 | |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 122 | private ExecutorService deviceEventExecutor; |
| 123 | |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 124 | @Activate |
| 125 | void activate() { |
| 126 | appId = coreService.registerApplication(APP_NAME); |
| 127 | codecService.registerCodec(XconnectDesc.class, new XconnectCodec()); |
| 128 | |
| 129 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 130 | .register(KryoNamespaces.API) |
Charles Chan | 871d918 | 2018-07-23 12:53:16 -0700 | [diff] [blame] | 131 | .register(XconnectManager.class) |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 132 | .register(XconnectKey.class); |
| 133 | |
| 134 | xconnectStore = storageService.<XconnectKey, Set<PortNumber>>consistentMapBuilder() |
| 135 | .withName("onos-sr-xconnect") |
| 136 | .withRelaxedReadConsistency() |
| 137 | .withSerializer(Serializer.using(serializer.build())) |
| 138 | .build(); |
| 139 | xconnectStore.addListener(xconnectListener); |
| 140 | |
| 141 | xconnectNextObjStore = storageService.<XconnectKey, NextObjective>consistentMapBuilder() |
| 142 | .withName("onos-sr-xconnect-next") |
| 143 | .withRelaxedReadConsistency() |
| 144 | .withSerializer(Serializer.using(serializer.build())) |
| 145 | .build(); |
| 146 | |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 147 | deviceEventExecutor = Executors.newSingleThreadScheduledExecutor( |
| 148 | groupedThreads("sr-xconnect-device-event", "%d", log)); |
| 149 | |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 150 | deviceService.addListener(deviceListener); |
| 151 | |
| 152 | log.info("Started"); |
| 153 | } |
| 154 | |
| 155 | @Deactivate |
| 156 | void deactivate() { |
| 157 | xconnectStore.removeListener(xconnectListener); |
| 158 | deviceService.removeListener(deviceListener); |
| 159 | codecService.unregisterCodec(XconnectDesc.class); |
| 160 | |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 161 | deviceEventExecutor.shutdown(); |
| 162 | |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 163 | log.info("Stopped"); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void addOrUpdateXconnect(DeviceId deviceId, VlanId vlanId, Set<PortNumber> ports) { |
| 168 | log.info("Adding or updating xconnect. deviceId={}, vlanId={}, ports={}", |
| 169 | deviceId, vlanId, ports); |
| 170 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 171 | xconnectStore.put(key, ports); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void removeXonnect(DeviceId deviceId, VlanId vlanId) { |
| 176 | log.info("Removing xconnect. deviceId={}, vlanId={}", |
| 177 | deviceId, vlanId); |
| 178 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 179 | xconnectStore.remove(key); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public Set<XconnectDesc> getXconnects() { |
| 184 | return xconnectStore.asJavaMap().entrySet().stream() |
| 185 | .map(e -> new XconnectDesc(e.getKey(), e.getValue())) |
| 186 | .collect(Collectors.toSet()); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean hasXconnect(ConnectPoint cp) { |
| 191 | return getXconnects().stream().anyMatch(desc -> |
| 192 | desc.key().deviceId().equals(cp.deviceId()) && desc.ports().contains(cp.port()) |
| 193 | ); |
| 194 | } |
| 195 | |
Charles Chan | d5814aa | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 196 | @Override |
| 197 | public ImmutableMap<XconnectKey, NextObjective> getNext() { |
| 198 | if (xconnectNextObjStore != null) { |
| 199 | return ImmutableMap.copyOf(xconnectNextObjStore.asJavaMap()); |
| 200 | } else { |
| 201 | return ImmutableMap.of(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void removeNextId(int nextId) { |
| 207 | xconnectNextObjStore.entrySet().forEach(e -> { |
| 208 | if (e.getValue().value().id() == nextId) { |
| 209 | xconnectNextObjStore.remove(e.getKey()); |
| 210 | } |
| 211 | }); |
| 212 | } |
| 213 | |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 214 | private class XconnectMapListener implements MapEventListener<XconnectKey, Set<PortNumber>> { |
| 215 | @Override |
| 216 | public void event(MapEvent<XconnectKey, Set<PortNumber>> event) { |
| 217 | XconnectKey key = event.key(); |
| 218 | Versioned<Set<PortNumber>> ports = event.newValue(); |
| 219 | Versioned<Set<PortNumber>> oldPorts = event.oldValue(); |
| 220 | |
| 221 | switch (event.type()) { |
| 222 | case INSERT: |
| 223 | populateXConnect(key, ports.value()); |
| 224 | break; |
| 225 | case UPDATE: |
| 226 | updateXConnect(key, oldPorts.value(), ports.value()); |
| 227 | break; |
| 228 | case REMOVE: |
| 229 | revokeXConnect(key, oldPorts.value()); |
| 230 | break; |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private class InternalDeviceListener implements DeviceListener { |
| 238 | @Override |
| 239 | public void event(DeviceEvent event) { |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 240 | deviceEventExecutor.execute(() -> { |
| 241 | DeviceId deviceId = event.subject().id(); |
| 242 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 243 | return; |
| 244 | } |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 245 | |
Charles Chan | 168111e | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 246 | switch (event.type()) { |
| 247 | case DEVICE_ADDED: |
| 248 | case DEVICE_AVAILABILITY_CHANGED: |
| 249 | case DEVICE_UPDATED: |
| 250 | if (deviceService.isAvailable(deviceId)) { |
| 251 | init(deviceId); |
| 252 | } else { |
| 253 | cleanup(deviceId); |
| 254 | } |
| 255 | break; |
| 256 | default: |
| 257 | break; |
| 258 | } |
| 259 | }); |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | void init(DeviceId deviceId) { |
| 264 | getXconnects().stream() |
| 265 | .filter(desc -> desc.key().deviceId().equals(deviceId)) |
| 266 | .forEach(desc -> populateXConnect(desc.key(), desc.ports())); |
| 267 | } |
| 268 | |
| 269 | void cleanup(DeviceId deviceId) { |
| 270 | xconnectNextObjStore.entrySet().stream() |
| 271 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 272 | .forEach(entry -> xconnectNextObjStore.remove(entry.getKey())); |
| 273 | log.debug("{} is removed from xConnectNextObjStore", deviceId); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Populates XConnect groups and flows for given key. |
| 278 | * |
| 279 | * @param key XConnect key |
| 280 | * @param ports a set of ports to be cross-connected |
| 281 | */ |
| 282 | private void populateXConnect(XconnectKey key, Set<PortNumber> ports) { |
| 283 | if (!mastershipService.isLocalMaster(key.deviceId())) { |
| 284 | log.info("Abort populating XConnect {}: {}", key, ERROR_NOT_MASTER); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | ports = addPairPort(key.deviceId(), ports); |
| 289 | populateFilter(key, ports); |
| 290 | populateFwd(key, populateNext(key, ports)); |
| 291 | populateAcl(key); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Populates filtering objectives for given XConnect. |
| 296 | * |
| 297 | * @param key XConnect store key |
| 298 | * @param ports XConnect ports |
| 299 | */ |
| 300 | private void populateFilter(XconnectKey key, Set<PortNumber> ports) { |
| 301 | ports.forEach(port -> { |
| 302 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 303 | ObjectiveContext context = new DefaultObjectiveContext( |
| 304 | (objective) -> log.debug("XConnect FilterObj for {} on port {} populated", |
| 305 | key, port), |
| 306 | (objective, error) -> |
| 307 | log.warn("Failed to populate XConnect FilterObj for {} on port {}: {}", |
| 308 | key, port, error)); |
| 309 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.add(context)); |
| 310 | }); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Populates next objectives for given XConnect. |
| 315 | * |
| 316 | * @param key XConnect store key |
| 317 | * @param ports XConnect ports |
| 318 | */ |
| 319 | private NextObjective populateNext(XconnectKey key, Set<PortNumber> ports) { |
| 320 | NextObjective nextObj; |
| 321 | if (xconnectNextObjStore.containsKey(key)) { |
| 322 | nextObj = xconnectNextObjStore.get(key).value(); |
| 323 | log.debug("NextObj for {} found, id={}", key, nextObj.id()); |
| 324 | } else { |
| 325 | NextObjective.Builder nextObjBuilder = nextObjBuilder(key, ports); |
| 326 | ObjectiveContext nextContext = new DefaultObjectiveContext( |
| 327 | // To serialize this with kryo |
| 328 | (Serializable & Consumer<Objective>) (objective) -> |
| 329 | log.debug("XConnect NextObj for {} added", key), |
Charles Chan | 55b806f | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 330 | (Serializable & BiConsumer<Objective, ObjectiveError>) (objective, error) -> { |
| 331 | log.warn("Failed to add XConnect NextObj for {}: {}", key, error); |
| 332 | srService.invalidateNextObj(objective.id()); |
| 333 | }); |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 334 | nextObj = nextObjBuilder.add(nextContext); |
| 335 | flowObjectiveService.next(key.deviceId(), nextObj); |
| 336 | xconnectNextObjStore.put(key, nextObj); |
| 337 | log.debug("NextObj for {} not found. Creating new NextObj with id={}", key, nextObj.id()); |
| 338 | } |
| 339 | return nextObj; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Populates bridging forwarding objectives for given XConnect. |
| 344 | * |
| 345 | * @param key XConnect store key |
| 346 | * @param nextObj next objective |
| 347 | */ |
| 348 | private void populateFwd(XconnectKey key, NextObjective nextObj) { |
| 349 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextObj.id()); |
| 350 | ObjectiveContext fwdContext = new DefaultObjectiveContext( |
| 351 | (objective) -> log.debug("XConnect FwdObj for {} populated", key), |
| 352 | (objective, error) -> |
| 353 | log.warn("Failed to populate XConnect FwdObj for {}: {}", key, error)); |
| 354 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.add(fwdContext)); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Populates ACL forwarding objectives for given XConnect. |
| 359 | * |
| 360 | * @param key XConnect store key |
| 361 | */ |
| 362 | private void populateAcl(XconnectKey key) { |
| 363 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 364 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 365 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 366 | (objective, error) -> |
| 367 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 368 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.add(aclContext)); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Revokes XConnect groups and flows for given key. |
| 373 | * |
| 374 | * @param key XConnect key |
| 375 | * @param ports XConnect ports |
| 376 | */ |
| 377 | private void revokeXConnect(XconnectKey key, Set<PortNumber> ports) { |
| 378 | if (!mastershipService.isLocalMaster(key.deviceId())) { |
| 379 | log.info("Abort populating XConnect {}: {}", key, ERROR_NOT_MASTER); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | ports = addPairPort(key.deviceId(), ports); |
| 384 | revokeFilter(key, ports); |
| 385 | if (xconnectNextObjStore.containsKey(key)) { |
| 386 | NextObjective nextObj = xconnectNextObjStore.get(key).value(); |
| 387 | revokeFwd(key, nextObj, null); |
| 388 | revokeNext(key, nextObj, null); |
| 389 | } else { |
| 390 | log.warn("NextObj for {} does not exist in the store.", key); |
| 391 | } |
| 392 | revokeAcl(key); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Revokes filtering objectives for given XConnect. |
| 397 | * |
| 398 | * @param key XConnect store key |
| 399 | * @param ports XConnect ports |
| 400 | */ |
| 401 | private void revokeFilter(XconnectKey key, Set<PortNumber> ports) { |
| 402 | ports.forEach(port -> { |
| 403 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 404 | ObjectiveContext context = new DefaultObjectiveContext( |
| 405 | (objective) -> log.debug("XConnect FilterObj for {} on port {} revoked", |
| 406 | key, port), |
| 407 | (objective, error) -> |
| 408 | log.warn("Failed to revoke XConnect FilterObj for {} on port {}: {}", |
| 409 | key, port, error)); |
| 410 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.remove(context)); |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Revokes next objectives for given XConnect. |
| 416 | * |
| 417 | * @param key XConnect store key |
| 418 | * @param nextObj next objective |
| 419 | * @param nextFuture completable future for this next objective operation |
| 420 | */ |
| 421 | private void revokeNext(XconnectKey key, NextObjective nextObj, |
| 422 | CompletableFuture<ObjectiveError> nextFuture) { |
| 423 | ObjectiveContext context = new ObjectiveContext() { |
| 424 | @Override |
| 425 | public void onSuccess(Objective objective) { |
| 426 | log.debug("Previous NextObj for {} removed", key); |
| 427 | if (nextFuture != null) { |
| 428 | nextFuture.complete(null); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void onError(Objective objective, ObjectiveError error) { |
| 434 | log.warn("Failed to remove previous NextObj for {}: {}", key, error); |
| 435 | if (nextFuture != null) { |
| 436 | nextFuture.complete(error); |
| 437 | } |
Charles Chan | 55b806f | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 438 | srService.invalidateNextObj(objective.id()); |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 439 | } |
| 440 | }; |
Charles Chan | 55b806f | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 441 | flowObjectiveService.next(key.deviceId(), nextObj.copy().remove(context)); |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 442 | xconnectNextObjStore.remove(key); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Revokes bridging forwarding objectives for given XConnect. |
| 447 | * |
| 448 | * @param key XConnect store key |
| 449 | * @param nextObj next objective |
| 450 | * @param fwdFuture completable future for this forwarding objective operation |
| 451 | */ |
| 452 | private void revokeFwd(XconnectKey key, NextObjective nextObj, |
| 453 | CompletableFuture<ObjectiveError> fwdFuture) { |
| 454 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextObj.id()); |
| 455 | ObjectiveContext context = new ObjectiveContext() { |
| 456 | @Override |
| 457 | public void onSuccess(Objective objective) { |
| 458 | log.debug("Previous FwdObj for {} removed", key); |
| 459 | if (fwdFuture != null) { |
| 460 | fwdFuture.complete(null); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public void onError(Objective objective, ObjectiveError error) { |
| 466 | log.warn("Failed to remove previous FwdObj for {}: {}", key, error); |
| 467 | if (fwdFuture != null) { |
| 468 | fwdFuture.complete(error); |
| 469 | } |
| 470 | } |
| 471 | }; |
| 472 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.remove(context)); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Revokes ACL forwarding objectives for given XConnect. |
| 477 | * |
| 478 | * @param key XConnect store key |
| 479 | */ |
| 480 | private void revokeAcl(XconnectKey key) { |
| 481 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 482 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 483 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 484 | (objective, error) -> |
| 485 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 486 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.remove(aclContext)); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Updates XConnect groups and flows for given key. |
| 491 | * |
| 492 | * @param key XConnect key |
| 493 | * @param prevPorts previous XConnect ports |
| 494 | * @param ports new XConnect ports |
| 495 | */ |
| 496 | private void updateXConnect(XconnectKey key, Set<PortNumber> prevPorts, |
| 497 | Set<PortNumber> ports) { |
| 498 | // NOTE: ACL flow doesn't include port information. No need to update it. |
| 499 | // Pair port is built-in and thus not going to change. No need to update it. |
| 500 | |
| 501 | // remove old filter |
| 502 | prevPorts.stream().filter(port -> !ports.contains(port)).forEach(port -> |
| 503 | revokeFilter(key, ImmutableSet.of(port))); |
| 504 | // install new filter |
| 505 | ports.stream().filter(port -> !prevPorts.contains(port)).forEach(port -> |
| 506 | populateFilter(key, ImmutableSet.of(port))); |
| 507 | |
| 508 | CompletableFuture<ObjectiveError> fwdFuture = new CompletableFuture<>(); |
| 509 | CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>(); |
| 510 | |
| 511 | if (xconnectNextObjStore.containsKey(key)) { |
| 512 | NextObjective nextObj = xconnectNextObjStore.get(key).value(); |
| 513 | revokeFwd(key, nextObj, fwdFuture); |
| 514 | |
| 515 | fwdFuture.thenAcceptAsync(fwdStatus -> { |
| 516 | if (fwdStatus == null) { |
| 517 | log.debug("Fwd removed. Now remove group {}", key); |
| 518 | revokeNext(key, nextObj, nextFuture); |
| 519 | } |
| 520 | }); |
| 521 | |
| 522 | nextFuture.thenAcceptAsync(nextStatus -> { |
| 523 | if (nextStatus == null) { |
| 524 | log.debug("Installing new group and flow for {}", key); |
| 525 | populateFwd(key, populateNext(key, ports)); |
| 526 | } |
| 527 | }); |
| 528 | } else { |
| 529 | log.warn("NextObj for {} does not exist in the store.", key); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Creates a next objective builder for XConnect. |
| 535 | * |
| 536 | * @param key XConnect key |
| 537 | * @param ports set of XConnect ports |
| 538 | * @return next objective builder |
| 539 | */ |
| 540 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports) { |
| 541 | int nextId = flowObjectiveService.allocateNextId(); |
| 542 | TrafficSelector metadata = |
| 543 | DefaultTrafficSelector.builder().matchVlanId(key.vlanId()).build(); |
| 544 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 545 | .builder().withId(nextId) |
| 546 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 547 | .withMeta(metadata); |
| 548 | ports.forEach(port -> { |
| 549 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 550 | tBuilder.setOutput(port); |
| 551 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 552 | }); |
| 553 | return nextObjBuilder; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Creates a bridging forwarding objective builder for XConnect. |
| 558 | * |
| 559 | * @param key XConnect key |
| 560 | * @param nextId next ID of the broadcast group for this XConnect key |
| 561 | * @return forwarding objective builder |
| 562 | */ |
| 563 | private ForwardingObjective.Builder fwdObjBuilder(XconnectKey key, int nextId) { |
| 564 | /* |
| 565 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 566 | * as the VLAN cross-connect broadcast rules |
| 567 | */ |
| 568 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 569 | sbuilder.matchVlanId(key.vlanId()); |
| 570 | sbuilder.matchEthDst(MacAddress.NONE); |
| 571 | |
| 572 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 573 | fob.withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 574 | .withSelector(sbuilder.build()) |
| 575 | .nextStep(nextId) |
| 576 | .withPriority(XCONNECT_PRIORITY) |
| 577 | .fromApp(appId) |
| 578 | .makePermanent(); |
| 579 | return fob; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Creates an ACL forwarding objective builder for XConnect. |
| 584 | * |
| 585 | * @param vlanId cross connect VLAN id |
| 586 | * @return forwarding objective builder |
| 587 | */ |
| 588 | private ForwardingObjective.Builder aclObjBuilder(VlanId vlanId) { |
| 589 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 590 | sbuilder.matchVlanId(vlanId); |
| 591 | |
| 592 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 593 | |
| 594 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 595 | fob.withFlag(ForwardingObjective.Flag.VERSATILE) |
| 596 | .withSelector(sbuilder.build()) |
| 597 | .withTreatment(tbuilder.build()) |
| 598 | .withPriority(XCONNECT_ACL_PRIORITY) |
| 599 | .fromApp(appId) |
| 600 | .makePermanent(); |
| 601 | return fob; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Creates a filtering objective builder for XConnect. |
| 606 | * |
| 607 | * @param key XConnect key |
| 608 | * @param port XConnect ports |
| 609 | * @return next objective builder |
| 610 | */ |
| 611 | private FilteringObjective.Builder filterObjBuilder(XconnectKey key, PortNumber port) { |
| 612 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 613 | fob.withKey(Criteria.matchInPort(port)) |
| 614 | .addCondition(Criteria.matchVlanId(key.vlanId())) |
| 615 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
| 616 | .withPriority(XCONNECT_PRIORITY); |
| 617 | return fob.permit().fromApp(appId); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Add pair port to the given set of port. |
| 622 | * |
| 623 | * @param deviceId device Id |
| 624 | * @param ports ports specified in the xconnect config |
| 625 | * @return port specified in the xconnect config plus the pair port (if configured) |
| 626 | */ |
| 627 | private Set<PortNumber> addPairPort(DeviceId deviceId, Set<PortNumber> ports) { |
| 628 | if (srService == null) { |
| 629 | return ports; |
| 630 | } |
| 631 | Set<PortNumber> newPorts = Sets.newHashSet(ports); |
| 632 | srService.getPairLocalPort(deviceId).ifPresent(newPorts::add); |
| 633 | return newPorts; |
| 634 | } |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 635 | } |