Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 19 | import java.util.List; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.Map.Entry; |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 24 | import java.util.stream.Collectors; |
| 25 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.HostLocation; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.link.LinkService; |
| 33 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 34 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 35 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 37 | import org.onosproject.store.service.WallClockTimestamp; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 41 | import com.google.common.collect.ImmutableList; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 42 | import com.google.common.collect.ImmutableMap; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 43 | import com.google.common.collect.Sets; |
| 44 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 45 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 46 | public class LinkHandler { |
| 47 | private static final Logger log = LoggerFactory.getLogger(LinkHandler.class); |
| 48 | protected final SegmentRoutingManager srManager; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 49 | |
| 50 | // Local store for all links seen and their present status, used for |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 51 | // optimized routing. The existence of the link in the keys is enough to know |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 52 | // if the link has been "seen-before" by this instance of the controller. |
| 53 | // The boolean value indicates if the link is currently up or not. |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 54 | // Currently the optimized routing logic depends on "forgetting" a link |
| 55 | // when a switch goes down, but "remembering" it when only the link goes down. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 56 | private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>(); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 57 | private Set<Link> seenBefore = Sets.newConcurrentHashSet(); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 58 | private EventuallyConsistentMap<DeviceId, Set<PortNumber>> downedPortStore = null; |
| 59 | |
| 60 | /** |
| 61 | * Constructs the LinkHandler. |
| 62 | * |
| 63 | * @param srManager Segment Routing manager |
| 64 | */ |
| 65 | LinkHandler(SegmentRoutingManager srManager) { |
| 66 | this.srManager = srManager; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 67 | log.debug("Creating EC map downedportstore"); |
| 68 | EventuallyConsistentMapBuilder<DeviceId, Set<PortNumber>> downedPortsMapBuilder |
| 69 | = srManager.storageService.eventuallyConsistentMapBuilder(); |
| 70 | downedPortStore = downedPortsMapBuilder.withName("downedportstore") |
| 71 | .withSerializer(srManager.createSerializer()) |
| 72 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 73 | .build(); |
| 74 | log.trace("Current size {}", downedPortStore.size()); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Constructs the LinkHandler for unit-testing. |
| 79 | * |
| 80 | * @param srManager SegmentRoutingManager |
| 81 | * @param linkService LinkService |
| 82 | */ |
| 83 | LinkHandler(SegmentRoutingManager srManager, LinkService linkService) { |
| 84 | this.srManager = srManager; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 88 | * Initialize LinkHandler. |
| 89 | */ |
Charles Chan | c12c3d0 | 2018-03-09 15:53:44 -0800 | [diff] [blame] | 90 | void init() { |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 91 | log.info("Loading stored links"); |
Charles Chan | c12c3d0 | 2018-03-09 15:53:44 -0800 | [diff] [blame] | 92 | srManager.linkService.getActiveLinks().forEach(this::processLinkAdded); |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 96 | * Preprocessing of added link before being sent for route-path handling. |
| 97 | * Also performs post processing of link. |
| 98 | * |
| 99 | * @param link the link to be processed |
| 100 | */ |
| 101 | void processLinkAdded(Link link) { |
| 102 | log.info("** LINK ADDED {}", link.toString()); |
| 103 | if (!isLinkValid(link)) { |
| 104 | return; |
| 105 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 106 | // Irrespective of whether the local is a MASTER or not for this device, |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 107 | // create group handler instance and push default TTP flow rules if needed, |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 108 | // as in a multi-instance setup, instances can initiate groups for any |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 109 | // device. Also update local groupHandler stores. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 110 | DefaultGroupHandler groupHandler = srManager.groupHandlerMap |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 111 | .get(link.src().deviceId()); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 112 | if (groupHandler == null) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 113 | Device device = srManager.deviceService.getDevice(link.src().deviceId()); |
| 114 | if (device != null) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 115 | log.warn("processLinkAdded: Link Added notification without " |
| 116 | + "Device Added event, still handling it"); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 117 | srManager.processDeviceAdded(device); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 120 | |
| 121 | if (isSeenLink(link)) { |
| 122 | // temp store retains previous state, just before the state is updated in |
| 123 | // seenLinks; previous state is necessary when processing the |
| 124 | // linkupdate in defaultRoutingHandler |
| 125 | seenBefore.add(link); |
| 126 | } |
| 127 | updateSeenLink(link, true); |
| 128 | |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 129 | if (srManager.deviceConfiguration == null || |
| 130 | !srManager.deviceConfiguration.isConfigured(link.src().deviceId())) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 131 | log.warn("Source device of this link is not configured.. " |
| 132 | + "not processing further"); |
| 133 | return; |
| 134 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 135 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 136 | // process link only if it is bidirectional |
| 137 | if (!isBidirectionalLinkUp(link)) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 138 | log.debug("Link not bidirectional.. waiting for other direction " + |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 139 | "src {} --> dst {} ", link.dst(), link.src()); |
| 140 | return; |
| 141 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 142 | |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 143 | log.info("processing bidi links {} <--> {} UP", link.src(), link.dst()); |
| 144 | // update groupHandler internal port state for both directions |
| 145 | List<Link> ulinks = getBidiComponentLinks(link); |
| 146 | for (Link ulink : ulinks) { |
| 147 | DefaultGroupHandler gh = srManager.groupHandlerMap |
| 148 | .get(ulink.src().deviceId()); |
| 149 | if (gh != null) { |
| 150 | gh.portUpForLink(ulink); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | for (Link ulink : ulinks) { |
| 155 | log.info("-- Starting optimized route-path processing for component " |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 156 | + "unidirectional link {} --> {} UP", ulink.src(), ulink.dst()); |
pierventre | 6e184b9 | 2021-05-28 12:21:19 +0200 | [diff] [blame] | 157 | // Performs the seenBefore optimization iff we have seen before both links in that case |
| 158 | // we have programmed the group (unless there were major issues in the system) |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 159 | srManager.defaultRoutingHandler |
| 160 | .populateRoutingRulesForLinkStatusChange(null, ulink, null, |
pierventre | 6e184b9 | 2021-05-28 12:21:19 +0200 | [diff] [blame] | 161 | (seenBefore.contains(ulink) && seenBefore.contains(getReverseLink(ulink)))); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 162 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 163 | if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())) { |
| 164 | // handle edge-ports for dual-homed hosts |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 165 | updateHostPorts(ulink, true); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 166 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 167 | // It's possible that linkUp causes no route-path change as ECMP graph does |
| 168 | // not change if the link is a parallel link (same src-dst as |
| 169 | // another link). However we still need to update ECMP hash |
| 170 | // groups to include new buckets for the link that has come up. |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 171 | DefaultGroupHandler gh = srManager.groupHandlerMap |
| 172 | .get(ulink.src().deviceId()); |
| 173 | if (gh != null) { |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 174 | if (!seenBefore.contains(ulink) && isParallelLink(ulink)) { |
| 175 | // if link seen first time, we need to ensure hash-groups have |
| 176 | // all ports |
| 177 | log.debug("Attempting retryHash for paralled first-time link {}", |
| 178 | ulink); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 179 | gh.retryHash(ulink, false, true); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 180 | } else { |
| 181 | // seen before-link |
| 182 | if (isParallelLink(ulink)) { |
| 183 | log.debug("Attempting retryHash for paralled seen-before " |
| 184 | + "link {}", ulink); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 185 | gh.retryHash(ulink, false, false); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 186 | } |
Ray Milkey | 43969b9 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 187 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 190 | //clean up temp state |
| 191 | seenBefore.remove(ulink); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 192 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 193 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Preprocessing of removed link before being sent for route-path handling. |
| 198 | * Also performs post processing of link. |
| 199 | * |
| 200 | * @param link the link to be processed |
| 201 | */ |
| 202 | void processLinkRemoved(Link link) { |
| 203 | log.info("** LINK REMOVED {}", link.toString()); |
| 204 | if (!isLinkValid(link)) { |
| 205 | return; |
| 206 | } |
| 207 | // when removing links, update seen links first, before doing route-path |
pierventre | bd6eefa | 2021-05-14 22:06:50 +0200 | [diff] [blame] | 208 | // changes. Link should be already there because was up before. If not, |
| 209 | // we should not update the store. This could be a LINK DOWN happening |
| 210 | // after a DEVICE DOWN event. |
| 211 | if (isSeenLink(link)) { |
| 212 | updateSeenLink(link, false); |
| 213 | } else { |
| 214 | // Exiting here may not be needed since other checks later should fail |
| 215 | log.warn("received a link down for the link {} which is not in the store", link); |
| 216 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 217 | // handle edge-ports for dual-homed hosts |
| 218 | if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 219 | updateHostPorts(link, false); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 220 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 221 | |
| 222 | // device availability check helps to ensure that multiple link-removed |
| 223 | // events are actually treated as a single switch removed event. |
| 224 | // purgeSeenLink is necessary so we do rerouting (instead of rehashing) |
| 225 | // when switch comes back. |
| 226 | if (link.src().elementId() instanceof DeviceId |
| 227 | && !srManager.deviceService.isAvailable(link.src().deviceId())) { |
| 228 | purgeSeenLink(link); |
| 229 | return; |
| 230 | } |
| 231 | if (link.dst().elementId() instanceof DeviceId |
| 232 | && !srManager.deviceService.isAvailable(link.dst().deviceId())) { |
| 233 | purgeSeenLink(link); |
| 234 | return; |
| 235 | } |
| 236 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 237 | // process link only if it is bidirectional |
| 238 | if (!isBidirectionalLinkDown(link)) { |
| 239 | log.debug("Link not bidirectional.. waiting for other direction " + |
| 240 | "src {} --> dst {} ", link.dst(), link.src()); |
| 241 | return; |
| 242 | } |
| 243 | log.info("processing bidi links {} <--> {} DOWN", link.src(), link.dst()); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 244 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 245 | for (Link ulink : getBidiComponentLinks(link)) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 246 | log.info("-- Starting optimized route-path processing for component " |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 247 | + "unidirectional link {} --> {} DOWN", ulink.src(), ulink.dst()); |
| 248 | srManager.defaultRoutingHandler |
| 249 | .populateRoutingRulesForLinkStatusChange(ulink, null, null, true); |
| 250 | |
| 251 | // attempt rehashing for parallel links |
| 252 | DefaultGroupHandler groupHandler = srManager.groupHandlerMap |
| 253 | .get(ulink.src().deviceId()); |
| 254 | if (groupHandler != null) { |
| 255 | if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId()) |
| 256 | && isParallelLink(ulink)) { |
| 257 | log.debug("* retrying hash for parallel link removed:{}", ulink); |
| 258 | groupHandler.retryHash(ulink, true, false); |
| 259 | } else { |
| 260 | log.debug("Not attempting retry-hash for link removed: {} .. {}", |
| 261 | ulink, |
| 262 | (srManager.mastershipService.isLocalMaster(ulink |
| 263 | .src().deviceId())) ? "not parallel" |
| 264 | : "not master"); |
| 265 | } |
| 266 | // ensure local stores are updated after all rerouting or rehashing |
| 267 | groupHandler.portDownForLink(ulink); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 268 | } else { |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 269 | log.warn("group handler not found for dev:{} when removing link: {}", |
| 270 | ulink.src().deviceId(), ulink); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 271 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 272 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Checks validity of link. Examples of invalid links include |
| 277 | * indirect-links, links between ports on the same switch, and more. |
| 278 | * |
| 279 | * @param link the link to be processed |
| 280 | * @return true if valid link |
| 281 | */ |
Pier Luigi | d8a1516 | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 282 | boolean isLinkValid(Link link) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 283 | if (link.type() != Link.Type.DIRECT) { |
| 284 | // NOTE: A DIRECT link might be transiently marked as INDIRECT |
| 285 | // if BDDP is received before LLDP. We can safely ignore that |
| 286 | // until the LLDP is received and the link is marked as DIRECT. |
| 287 | log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.", |
| 288 | link.src(), link.dst(), link.type()); |
| 289 | return false; |
| 290 | } |
| 291 | DeviceId srcId = link.src().deviceId(); |
| 292 | DeviceId dstId = link.dst().deviceId(); |
| 293 | if (srcId.equals(dstId)) { |
| 294 | log.warn("Links between ports on the same switch are not " |
| 295 | + "allowed .. ignoring link {}", link); |
| 296 | return false; |
| 297 | } |
| 298 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 299 | if (devConfig == null) { |
| 300 | log.warn("Cannot check validity of link without device config"); |
| 301 | return true; |
| 302 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 303 | try { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 304 | /*if (!devConfig.isEdgeDevice(srcId) |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 305 | && !devConfig.isEdgeDevice(dstId)) { |
| 306 | // ignore links between spines |
| 307 | // XXX revisit when handling multi-stage fabrics |
| 308 | log.warn("Links between spines not allowed...ignoring " |
| 309 | + "link {}", link); |
| 310 | return false; |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 311 | }*/ |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 312 | if (devConfig.isEdgeDevice(srcId) |
| 313 | && devConfig.isEdgeDevice(dstId)) { |
| 314 | // ignore links between leaves if they are not pair-links |
| 315 | // XXX revisit if removing pair-link config or allowing more than |
| 316 | // one pair-link |
| 317 | if (devConfig.getPairDeviceId(srcId).equals(dstId) |
| 318 | && devConfig.getPairLocalPort(srcId) |
| 319 | .equals(link.src().port()) |
| 320 | && devConfig.getPairLocalPort(dstId) |
| 321 | .equals(link.dst().port())) { |
| 322 | // found pair link - allow it |
| 323 | return true; |
| 324 | } else { |
| 325 | log.warn("Links between leaves other than pair-links are " |
| 326 | + "not allowed...ignoring link {}", link); |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | } catch (DeviceConfigNotFoundException e) { |
| 331 | // We still want to count the links in seenLinks even though there |
| 332 | // is no config. So we let it return true |
| 333 | log.warn("Could not check validity of link {} as subtending devices " |
| 334 | + "are not yet configured", link); |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Administratively enables or disables edge ports if the link that was |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 341 | * added or removed was the only uplink port from an edge device. Edge ports |
| 342 | * that belong to dual-homed hosts are always processed. In addition, |
| 343 | * single-homed host ports are optionally processed depending on the |
| 344 | * singleHomedDown property. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 345 | * |
| 346 | * @param link the link to be processed |
| 347 | * @param added true if link was added, false if link was removed |
| 348 | */ |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 349 | private void updateHostPorts(Link link, boolean added) { |
Charles Chan | a240145 | 2021-06-03 15:57:48 -0700 | [diff] [blame^] | 350 | // Topology has only a single pair of leaves |
| 351 | if (srManager.getInfraDeviceIds().isEmpty()) { |
| 352 | log.debug("No spine configured. Not updating edge port for {} {}", link, added ? "add" : "remove"); |
| 353 | return; |
| 354 | } |
| 355 | |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 356 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 357 | if (added) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 358 | try { |
| 359 | if (!devConfig.isEdgeDevice(link.src().deviceId()) |
| 360 | || devConfig.isEdgeDevice(link.dst().deviceId())) { |
| 361 | return; |
| 362 | } |
| 363 | } catch (DeviceConfigNotFoundException e) { |
| 364 | log.warn("Unable to determine if link is a valid uplink" |
| 365 | + e.getMessage()); |
| 366 | } |
| 367 | // re-enable previously disabled ports on this edge-device if any |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 368 | Set<PortNumber> p = downedPortStore.remove(link.src().deviceId()); |
| 369 | if (p != null) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 370 | log.warn("Link src {} --> dst {} added is an edge-device uplink, " |
| 371 | + "enabling dual homed ports if any: {}", link.src().deviceId(), |
| 372 | link.dst().deviceId(), (p.isEmpty()) ? "no ports" : p); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 373 | p.forEach(pnum -> srManager.deviceAdminService |
| 374 | .changePortState(link.src().deviceId(), pnum, true)); |
| 375 | } |
| 376 | } else { |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 377 | // If the device does not have a pair device - skip |
| 378 | DeviceId dev = link.src().deviceId(); |
| 379 | if (getPairDeviceIdOrNull(dev) == null) { |
| 380 | log.info("Device {} does not have pair device " + |
| 381 | "not disabling access port", dev); |
| 382 | return; |
| 383 | } |
| 384 | // Verify if last uplink |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 385 | if (!lastUplink(link)) { |
| 386 | return; |
| 387 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 388 | // find dual homed hosts on this dev to disable |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 389 | Set<PortNumber> dp = srManager.hostHandler |
| 390 | .getDualHomedHostPorts(dev); |
| 391 | log.warn("Link src {} --> dst {} removed was the last uplink, " |
| 392 | + "disabling dual homed ports: {}", dev, |
| 393 | link.dst().deviceId(), (dp.isEmpty()) ? "no ports" : dp); |
| 394 | dp.forEach(pnum -> srManager.deviceAdminService |
| 395 | .changePortState(dev, pnum, false)); |
| 396 | if (srManager.singleHomedDown) { |
| 397 | // get all configured ports and down them if they haven't already |
| 398 | // been downed |
| 399 | srManager.deviceService.getPorts(dev).stream() |
| 400 | .filter(p -> p.isEnabled() && !dp.contains(p.number())) |
| 401 | .filter(p -> srManager.interfaceService |
| 402 | .isConfigured(new ConnectPoint(dev, p.number()))) |
| 403 | .filter(p -> !srManager.deviceConfiguration |
| 404 | .isPairLocalPort(dev, p.number())) |
| 405 | .forEach(p -> { |
| 406 | log.warn("Last uplink gone src {} -> dst {} .. removing " |
| 407 | + "configured port {}", p.number()); |
| 408 | srManager.deviceAdminService |
| 409 | .changePortState(dev, p.number(), false); |
| 410 | dp.add(p.number()); |
| 411 | }); |
| 412 | } |
| 413 | if (!dp.isEmpty()) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 414 | // update global store |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 415 | Set<PortNumber> p = downedPortStore.get(dev); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 416 | if (p == null) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 417 | p = dp; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 418 | } else { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 419 | p.addAll(dp); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 420 | } |
| 421 | downedPortStore.put(link.src().deviceId(), p); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 427 | * Returns true if given link was the last active uplink from src-device of |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 428 | * link. An uplink is defined as a unidirectional link with src as |
| 429 | * edgeRouter and dst as non-edgeRouter. |
| 430 | * |
| 431 | * @param link |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 432 | * @return true if given link was the last uplink from the src device |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 433 | */ |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 434 | private boolean lastUplink(Link link) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 435 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
| 436 | try { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 437 | if (!devConfig.isEdgeDevice(link.src().deviceId()) |
| 438 | || devConfig.isEdgeDevice(link.dst().deviceId())) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 439 | return false; |
| 440 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 441 | // note that using linkservice here would cause race conditions as |
| 442 | // more links can show up while the app is still processing the first one |
| 443 | Set<Link> devLinks = seenLinks.entrySet().stream() |
| 444 | .filter(entry -> entry.getKey().src().deviceId() |
| 445 | .equals(link.src().deviceId())) |
| 446 | .filter(entry -> entry.getValue()) |
| 447 | .filter(entry -> !entry.getKey().equals(link)) |
| 448 | .map(entry -> entry.getKey()) |
| 449 | .collect(Collectors.toSet()); |
| 450 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 451 | for (Link l : devLinks) { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 452 | if (devConfig.isEdgeDevice(l.dst().deviceId())) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 453 | continue; |
| 454 | } |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 455 | log.debug("Found another active uplink {}", l); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 456 | return false; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 457 | } |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 458 | log.debug("No active uplink found"); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 459 | return true; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 460 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 461 | log.warn("Unable to determine if link was the last uplink" |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 462 | + e.getMessage()); |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Returns true if this controller instance has seen this link before. The |
| 469 | * link may not be currently up, but as long as the link had been seen |
| 470 | * before this method will return true. The one exception is when the link |
| 471 | * was indeed seen before, but this controller instance was forced to forget |
| 472 | * it by a call to purgeSeenLink method. |
| 473 | * |
| 474 | * @param link the infrastructure link being queried |
| 475 | * @return true if this controller instance has seen this link before |
| 476 | */ |
| 477 | boolean isSeenLink(Link link) { |
| 478 | return seenLinks.containsKey(link); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Updates the seen link store. Updates can be for links that are currently |
| 483 | * available or not. |
| 484 | * |
| 485 | * @param link the link to update in the seen-link local store |
| 486 | * @param up the status of the link, true if up, false if down |
| 487 | */ |
| 488 | void updateSeenLink(Link link, boolean up) { |
| 489 | seenLinks.put(link, up); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Returns the status of a seen-link (up or down). If the link has not been |
| 494 | * seen-before, a null object is returned. |
| 495 | * |
| 496 | * @param link the infrastructure link being queried |
| 497 | * @return null if the link was not seen-before; true if the seen-link is |
| 498 | * up; false if the seen-link is down |
| 499 | */ |
| 500 | private Boolean isSeenLinkUp(Link link) { |
| 501 | return seenLinks.get(link); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Makes this controller instance forget a previously seen before link. |
| 506 | * |
| 507 | * @param link the infrastructure link to purge |
| 508 | */ |
| 509 | private void purgeSeenLink(Link link) { |
| 510 | seenLinks.remove(link); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 511 | seenBefore.remove(link); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Returns the status of a link as parallel link. A parallel link is defined |
| 516 | * as a link which has common src and dst switches as another seen-link that |
| 517 | * is currently enabled. It is not necessary for the link being queried to |
| 518 | * be a seen-link. |
| 519 | * |
| 520 | * @param link the infrastructure link being queried |
| 521 | * @return true if a seen-link exists that is up, and shares the same src |
| 522 | * and dst switches as the link being queried |
| 523 | */ |
| 524 | private boolean isParallelLink(Link link) { |
| 525 | for (Entry<Link, Boolean> seen : seenLinks.entrySet()) { |
| 526 | Link seenLink = seen.getKey(); |
| 527 | if (seenLink.equals(link)) { |
| 528 | continue; |
| 529 | } |
| 530 | if (seenLink.src().deviceId().equals(link.src().deviceId()) |
| 531 | && seenLink.dst().deviceId().equals(link.dst().deviceId()) |
| 532 | && seen.getValue()) { |
| 533 | return true; |
| 534 | } |
| 535 | } |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | /** |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 540 | * Returns true if the link being queried is a bidirectional link that is |
| 541 | * up. A bidi-link is defined as a component unidirectional link, whose |
| 542 | * reverse link - ie. the component unidirectional link in the reverse |
| 543 | * direction - has been seen-before and is up. It is NOT necessary for the |
| 544 | * link being queried to be a previously seen-link. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 545 | * |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 546 | * @param link the infrastructure (unidirectional) link being queried |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 547 | * @return true if another unidirectional link exists in the reverse |
| 548 | * direction, has been seen-before and is up |
| 549 | */ |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 550 | boolean isBidirectionalLinkUp(Link link) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 551 | // cannot call linkService as link may be gone |
| 552 | Link reverseLink = getReverseLink(link); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 553 | if (reverseLink == null) { |
| 554 | return false; |
| 555 | } |
| 556 | Boolean result = isSeenLinkUp(reverseLink); |
| 557 | if (result == null) { |
| 558 | return false; |
| 559 | } |
| 560 | return result.booleanValue(); |
| 561 | } |
| 562 | |
| 563 | /** |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 564 | * Returns true if the link being queried is a bidirectional link that is |
| 565 | * down. A bidi-link is defined as a component unidirectional link, whose |
| 566 | * reverse link - i.e the component unidirectional link in the reverse |
| 567 | * direction - has been seen before and is down. It is necessary for the |
| 568 | * reverse-link to have been previously seen. |
| 569 | * |
| 570 | * @param link the infrastructure (unidirectional) link being queried |
| 571 | * @return true if another unidirectional link exists in the reverse |
| 572 | * direction, has been seen-before and is down |
| 573 | */ |
| 574 | boolean isBidirectionalLinkDown(Link link) { |
| 575 | // cannot call linkService as link may be gone |
| 576 | Link reverseLink = getReverseLink(link); |
| 577 | if (reverseLink == null) { |
| 578 | log.warn("Query for bidi-link down but reverse-link not found " |
| 579 | + "for link {}", link); |
| 580 | return false; |
| 581 | } |
| 582 | Boolean result = seenLinks.get(reverseLink); |
| 583 | if (result == null) { |
| 584 | return false; |
| 585 | } |
| 586 | // if reverse link is seen UP (true), then its not bidi yet |
| 587 | return !result.booleanValue(); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Returns the link in the reverse direction from the given link, by |
| 592 | * consulting the seen-links store. |
| 593 | * |
| 594 | * @param link the given link |
| 595 | * @return the reverse link or null |
| 596 | */ |
| 597 | Link getReverseLink(Link link) { |
| 598 | return seenLinks.keySet().stream() |
| 599 | .filter(l -> l.src().equals(link.dst()) && l.dst().equals(link.src())) |
| 600 | .findAny() |
| 601 | .orElse(null); |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Returns the component unidirectional links of a declared bidirectional |
| 606 | * link, by consulting the seen-links store. Caller is responsible for |
| 607 | * previously verifying bidirectionality. Returned list may be empty if |
| 608 | * errors are encountered. |
| 609 | * |
| 610 | * @param link the declared bidirectional link |
| 611 | * @return list of component unidirectional links |
| 612 | */ |
| 613 | List<Link> getBidiComponentLinks(Link link) { |
| 614 | Link reverseLink = getReverseLink(link); |
| 615 | List<Link> componentLinks; |
| 616 | if (reverseLink == null) { // really should not happen if link is bidi |
| 617 | log.error("cannot find reverse link for given link: {} ... is it " |
| 618 | + "bi-directional?", link); |
| 619 | componentLinks = ImmutableList.of(); |
| 620 | } else { |
| 621 | componentLinks = ImmutableList.of(reverseLink, link); |
| 622 | } |
| 623 | return componentLinks; |
| 624 | } |
| 625 | |
| 626 | /** |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 627 | * Determines if the given link should be avoided in routing calculations by |
| 628 | * policy or design. |
| 629 | * |
| 630 | * @param link the infrastructure link being queried |
| 631 | * @return true if link should be avoided |
| 632 | */ |
| 633 | boolean avoidLink(Link link) { |
| 634 | // XXX currently only avoids all pair-links. In the future can be |
| 635 | // extended to avoid any generic link |
| 636 | DeviceId src = link.src().deviceId(); |
| 637 | PortNumber srcPort = link.src().port(); |
| 638 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
| 639 | if (devConfig == null || !devConfig.isConfigured(src)) { |
| 640 | log.warn("Device {} not configured..cannot avoid link {}", src, |
| 641 | link); |
| 642 | return false; |
| 643 | } |
| 644 | DeviceId pairDev; |
| 645 | PortNumber pairLocalPort, pairRemotePort = null; |
| 646 | try { |
| 647 | pairDev = devConfig.getPairDeviceId(src); |
| 648 | pairLocalPort = devConfig.getPairLocalPort(src); |
| 649 | if (pairDev != null) { |
| 650 | pairRemotePort = devConfig |
| 651 | .getPairLocalPort(pairDev); |
| 652 | } |
| 653 | } catch (DeviceConfigNotFoundException e) { |
| 654 | log.warn("Pair dev for dev {} not configured..cannot avoid link {}", |
| 655 | src, link); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | return srcPort.equals(pairLocalPort) |
| 660 | && link.dst().deviceId().equals(pairDev) |
| 661 | && link.dst().port().equals(pairRemotePort); |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Cleans up internal LinkHandler stores. |
| 666 | * |
| 667 | * @param device the device that has been removed |
| 668 | */ |
| 669 | void processDeviceRemoved(Device device) { |
| 670 | seenLinks.keySet() |
| 671 | .removeIf(key -> key.src().deviceId().equals(device.id()) |
| 672 | || key.dst().deviceId().equals(device.id())); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Administratively disables the host location switchport if the edge device |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 677 | * has no viable uplinks. The caller needs to determine if such behavior is |
| 678 | * desired for the single or dual-homed host. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 679 | * |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 680 | * @param loc the host location |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 681 | */ |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 682 | void checkUplinksForHost(HostLocation loc) { |
Charles Chan | a240145 | 2021-06-03 15:57:48 -0700 | [diff] [blame^] | 683 | // Topology has only a single pair of leaves |
| 684 | if (srManager.getInfraDeviceIds().isEmpty()) { |
| 685 | log.debug("No spine configured. Not disabling access port for {}", loc); |
| 686 | return; |
| 687 | } |
| 688 | |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 689 | // If the device does not have a pair device - return |
| 690 | if (getPairDeviceIdOrNull(loc.deviceId()) == null) { |
| 691 | log.info("Device {} does not have pair device " + |
| 692 | "not disabling access port", loc.deviceId()); |
| 693 | return; |
| 694 | } |
Charles Chan | a240145 | 2021-06-03 15:57:48 -0700 | [diff] [blame^] | 695 | |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 696 | // Verify link validity |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 697 | try { |
| 698 | for (Link l : srManager.linkService.getDeviceLinks(loc.deviceId())) { |
| 699 | if (srManager.deviceConfiguration.isEdgeDevice(l.dst().deviceId()) |
| 700 | || l.state() == Link.State.INACTIVE) { |
| 701 | continue; |
| 702 | } |
| 703 | // found valid uplink - so, nothing to do |
| 704 | return; |
| 705 | } |
| 706 | } catch (DeviceConfigNotFoundException e) { |
| 707 | log.warn("Could not check for valid uplinks due to missing device" |
| 708 | + "config " + e.getMessage()); |
| 709 | return; |
| 710 | } |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 711 | log.warn("Host location {} has no valid uplinks disabling port", loc); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 712 | srManager.deviceAdminService.changePortState(loc.deviceId(), loc.port(), |
| 713 | false); |
| 714 | Set<PortNumber> p = downedPortStore.get(loc.deviceId()); |
| 715 | if (p == null) { |
| 716 | p = Sets.newHashSet(loc.port()); |
| 717 | } else { |
| 718 | p.add(loc.port()); |
| 719 | } |
| 720 | downedPortStore.put(loc.deviceId(), p); |
| 721 | } |
| 722 | |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 723 | private DeviceId getPairDeviceIdOrNull(DeviceId deviceId) { |
| 724 | DeviceId pairDev; |
| 725 | try { |
| 726 | pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId); |
| 727 | } catch (DeviceConfigNotFoundException e) { |
| 728 | pairDev = null; |
| 729 | } |
| 730 | return pairDev; |
| 731 | } |
| 732 | |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 733 | ImmutableMap<Link, Boolean> getSeenLinks() { |
| 734 | return ImmutableMap.copyOf(seenLinks); |
| 735 | } |
| 736 | |
| 737 | ImmutableMap<DeviceId, Set<PortNumber>> getDownedPorts() { |
| 738 | return ImmutableMap.copyOf(downedPortStore.entrySet()); |
| 739 | } |
| 740 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 741 | /** |
| 742 | * Returns all links that egress from given device that are UP in the |
| 743 | * seenLinks store. The returned links are also confirmed to be |
| 744 | * bidirectional. |
| 745 | * |
| 746 | * @param deviceId the device identifier |
| 747 | * @return set of egress links from the device |
| 748 | */ |
| 749 | Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 750 | return seenLinks.keySet().stream() |
| 751 | .filter(link -> link.src().deviceId().equals(deviceId)) |
| 752 | .filter(link -> seenLinks.get(link)) |
| 753 | .filter(link -> isBidirectionalLinkUp(link)) |
| 754 | .collect(Collectors.toSet()); |
| 755 | } |
| 756 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 757 | } |