Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 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.ospf.controller.impl; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.util.Bandwidth; |
| 20 | import org.onosproject.ospf.controller.DeviceInformation; |
| 21 | import org.onosproject.ospf.controller.LinkInformation; |
| 22 | import org.onosproject.ospf.controller.OspfArea; |
| 23 | import org.onosproject.ospf.controller.OspfInterface; |
| 24 | import org.onosproject.ospf.controller.OspfLinkTed; |
| 25 | import org.onosproject.ospf.controller.OspfLsa; |
| 26 | import org.onosproject.ospf.controller.OspfLsaType; |
| 27 | import org.onosproject.ospf.controller.TopologyForDeviceAndLink; |
| 28 | import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubType; |
| 29 | import org.onosproject.ospf.protocol.lsa.linksubtype.LocalInterfaceIpAddress; |
| 30 | import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumBandwidth; |
| 31 | import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumReservableBandwidth; |
| 32 | import org.onosproject.ospf.protocol.lsa.linksubtype.RemoteInterfaceIpAddress; |
| 33 | import org.onosproject.ospf.protocol.lsa.linksubtype.TrafficEngineeringMetric; |
| 34 | import org.onosproject.ospf.protocol.lsa.linksubtype.UnreservedBandwidth; |
| 35 | import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink; |
| 36 | import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv; |
| 37 | import org.onosproject.ospf.protocol.lsa.types.NetworkLsa; |
| 38 | import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10; |
| 39 | import org.onosproject.ospf.protocol.lsa.types.RouterLsa; |
| 40 | import org.onosproject.ospf.protocol.lsa.types.TopLevelTlv; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.HashMap; |
| 46 | import java.util.HashSet; |
| 47 | import java.util.Iterator; |
| 48 | import java.util.LinkedHashMap; |
| 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Set; |
| 52 | |
| 53 | /** |
| 54 | * Represents device and link topology information. |
| 55 | */ |
| 56 | public class TopologyForDeviceAndLinkImpl implements TopologyForDeviceAndLink { |
| 57 | |
| 58 | private static final Logger log = LoggerFactory.getLogger(TopologyForDeviceAndLinkImpl.class); |
| 59 | private Map<String, DeviceInformation> deviceInformationMap = new LinkedHashMap(); |
| 60 | private Map<String, DeviceInformation> deviceInformationMapToDelete = new LinkedHashMap(); |
| 61 | private HashMap<String, Set<OspfLsaLink>> deviceAndLinkInformation = new HashMap(); |
| 62 | private HashMap<String, OspfLinkTed> ospfLinkTedHashMap = new LinkedHashMap(); |
| 63 | private Ip4Address drRouter = Ip4Address.valueOf("0.0.0.0"); |
| 64 | private Ip4Address drRouterOld = Ip4Address.valueOf("0.0.0.0"); |
| 65 | private Ip4Address adRouterId = Ip4Address.valueOf("0.0.0.0"); |
| 66 | private Map<String, LinkInformation> linkInformationMap = new LinkedHashMap(); |
| 67 | private List<String> toRemove = new ArrayList<>(); |
| 68 | |
| 69 | /** |
| 70 | * Gets device information. |
| 71 | * |
| 72 | * @return device information |
| 73 | */ |
| 74 | public Map<String, DeviceInformation> deviceInformationMap() { |
| 75 | return deviceInformationMap; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets device information. |
| 80 | * |
| 81 | * @param key key used to add in map |
| 82 | * @param deviceInformationMap device information instance |
| 83 | */ |
| 84 | public void setDeviceInformationMap(String key, DeviceInformation deviceInformationMap) { |
| 85 | if (deviceInformationMap != null) { |
| 86 | this.deviceInformationMap.put(key, deviceInformationMap); |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Gets device information. |
| 93 | * |
| 94 | * @return device information to delete from core |
| 95 | */ |
| 96 | public Map<String, DeviceInformation> deviceInformationMapToDelete() { |
| 97 | return deviceInformationMapToDelete; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets device information for removal. |
| 102 | * |
| 103 | * @param key ket used to add in map |
| 104 | * @param deviceInformationMapToDelete map from device information to remove |
| 105 | */ |
| 106 | public void setDeviceInformationMapToDelete(String key, |
| 107 | DeviceInformation deviceInformationMapToDelete) { |
| 108 | if (deviceInformationMapToDelete != null) { |
| 109 | this.deviceInformationMapToDelete.put(key, deviceInformationMapToDelete); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Removes Device Information. |
| 115 | * |
| 116 | * @param key ket used to remove from map |
| 117 | */ |
| 118 | public void removeDeviceInformationMapFromDeleteMap(String key) { |
| 119 | removeDeviceInformationMap(key); |
| 120 | if (this.deviceInformationMapToDelete.containsKey(key)) { |
| 121 | this.deviceInformationMapToDelete.remove(key); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Gets Device Information. |
| 127 | * |
| 128 | * @param key key to store in map |
| 129 | * @return Device Information |
| 130 | */ |
| 131 | public DeviceInformation deviceInformation(String key) { |
| 132 | DeviceInformation deviceInformation = this.deviceInformationMap.get(key); |
| 133 | return deviceInformation; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Removes Device Information from map. |
| 138 | * |
| 139 | * @param key key used to remove from map |
| 140 | */ |
| 141 | public void removeDeviceInformationMap(String key) { |
| 142 | if (this.deviceInformationMap.containsKey(key)) { |
| 143 | this.deviceInformationMap.remove(key); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Gets link information as map. |
| 149 | * |
| 150 | * @return link information as map |
| 151 | */ |
| 152 | public Map<String, LinkInformation> linkInformationMap() { |
| 153 | return linkInformationMap; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets link information in map. |
| 158 | * |
| 159 | * @param key key used to add in map |
| 160 | * @param linkInformationMap link information instance |
| 161 | */ |
| 162 | public void setLinkInformationMap(String key, LinkInformation linkInformationMap) { |
| 163 | if (!this.linkInformationMap.containsKey(key)) { |
| 164 | this.linkInformationMap.put(key, linkInformationMap); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Removes Link Information from map. |
| 170 | * |
| 171 | * @param key key used to remove from map |
| 172 | */ |
| 173 | public void removeLinkInformationMap(String key) { |
| 174 | if (this.linkInformationMap.containsKey(key)) { |
| 175 | this.linkInformationMap.remove(key); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Gets OSPF Link TED details from the map. |
| 182 | * |
| 183 | * @param key key used to retreive from map |
| 184 | * @return OSPF link ted instance |
| 185 | */ |
| 186 | public OspfLinkTed getOspfLinkTedHashMap(String key) { |
| 187 | OspfLinkTed ospfLinkTed = ospfLinkTedHashMap.get(key); |
| 188 | return ospfLinkTed; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Adds device information to map. |
| 193 | * |
| 194 | * @param ospfLsa OSPF LSA instance |
| 195 | * @param ospfInterface OSPF interface instance |
| 196 | * @param ospfArea OSPF area instance |
| 197 | */ |
| 198 | public void addLocalDevice(OspfLsa ospfLsa, OspfInterface ospfInterface, OspfArea ospfArea) { |
| 199 | if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) { |
| 200 | createDeviceAndLinkFromRouterLsa(ospfLsa, ospfArea); |
| 201 | } else if (ospfLsa.getOspfLsaType().equals(OspfLsaType.NETWORK)) { |
| 202 | createDeviceAndLinkFromNetworkLsa(ospfLsa, ospfArea); |
| 203 | } else if (ospfLsa.getOspfLsaType().equals(OspfLsaType.AREA_LOCAL_OPAQUE_LSA)) { |
| 204 | createDeviceAndLinkFromOpaqueLsa(ospfLsa, ospfArea); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Creates device object from parameters. |
| 210 | * |
| 211 | * @param alreadyCreated device already created or not |
| 212 | * @param deviceId device id |
| 213 | * @param neighborId neighbor's id |
| 214 | * @param routerId router's id |
| 215 | * @param interfaceId interface id |
| 216 | * @param areaId area id |
| 217 | * @param isDr true if router is DR else false |
| 218 | */ |
| 219 | private DeviceInformation createDeviceInformation(boolean alreadyCreated, Ip4Address deviceId, |
| 220 | Ip4Address neighborId, Ip4Address routerId, |
| 221 | Ip4Address interfaceId, Ip4Address areaId, |
| 222 | boolean isDr) { |
| 223 | DeviceInformation deviceInformation = new DeviceInformationImpl(); |
| 224 | deviceInformation.setAlreadyCreated(alreadyCreated); |
| 225 | deviceInformation.setDeviceId(deviceId); |
| 226 | deviceInformation.setNeighborId(neighborId); |
| 227 | deviceInformation.setRouterId(routerId); |
| 228 | deviceInformation.addInterfaceId(interfaceId); |
| 229 | deviceInformation.setAreaId(areaId); |
| 230 | deviceInformation.setDr(isDr); |
| 231 | return deviceInformation; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Creates Device and Link instance from the RouterLsa parameters. |
| 236 | * |
| 237 | * @param ospfLsa OSPF LSA instance |
| 238 | * @param ospfArea OSPF area |
| 239 | */ |
| 240 | private void createDeviceAndLinkFromRouterLsa(OspfLsa ospfLsa, OspfArea ospfArea) { |
| 241 | RouterLsa routerLsa = (RouterLsa) ospfLsa; |
| 242 | List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink(); |
| 243 | Iterator iterator = ospfLsaLinkList.iterator(); |
| 244 | Ip4Address advertisingRouterId = routerLsa.advertisingRouter(); |
| 245 | adRouterId = advertisingRouterId; |
| 246 | while (iterator.hasNext()) { |
| 247 | OspfLsaLink ospfLsaLink = (OspfLsaLink) iterator.next(); |
| 248 | Ip4Address linkId = Ip4Address.valueOf(ospfLsaLink.linkId()); |
| 249 | Ip4Address linkData = Ip4Address.valueOf(ospfLsaLink.linkData()); |
| 250 | if (ospfLsaLink.linkType() == 1) { |
| 251 | if ((advertisingRouterId.equals(ospfArea.routerId())) || (linkId.equals(ospfArea.routerId()))) { |
| 252 | System.out.println("OspfInterface information will not display in web "); |
| 253 | } else { |
| 254 | removeDevice(advertisingRouterId); |
| 255 | removeLinks(advertisingRouterId); |
| 256 | DeviceInformation deviceInformationPointToPoint = |
| 257 | createDeviceInformation(false, linkId, linkId, advertisingRouterId, linkData, |
| 258 | ospfArea.areaId(), false); |
| 259 | String key = "device:" + advertisingRouterId; |
| 260 | setDeviceInformationMap(key, deviceInformationPointToPoint); |
| 261 | String linkIdKey = "linkId:" + advertisingRouterId + "-" + linkId; |
| 262 | addLocalLink(linkIdKey, linkData, advertisingRouterId, linkId, true, false); |
| 263 | } |
| 264 | } else if (ospfLsaLink.linkType() == 2) { |
| 265 | |
| 266 | if ((advertisingRouterId.equals(ospfArea.routerId())) || (linkId.equals(ospfArea.routerId()))) { |
| 267 | log.debug("OspfInterface information will not display in web "); |
| 268 | } else { |
| 269 | if (linkId.equals(linkData)) { |
| 270 | if (drRouter.equals(Ip4Address.valueOf("0.0.0.0"))) { |
| 271 | log.debug("drRouter not elected {} ", drRouter.toString()); |
| 272 | } else { |
| 273 | if (drRouterOld.equals(linkId)) { |
| 274 | log.debug("drRouterOld same as link id {} ", drRouterOld.toString()); |
| 275 | } else { |
| 276 | String key = "device:" + drRouterOld; |
| 277 | DeviceInformation deviceInformation1 = deviceInformation(key); |
| 278 | if (deviceInformation1 != null) { |
| 279 | deviceInformation1.setAlreadyCreated(true); |
| 280 | setDeviceInformationMapToDelete(key, deviceInformation1); |
| 281 | String linkIdKey = "linkId:" + linkId + "-" + deviceInformation1.neighborId(); |
| 282 | addLocalLink(linkIdKey, linkData, linkId, deviceInformation1.neighborId(), |
| 283 | true, false); |
| 284 | String linkIdKey1 = "linkId:" + linkId + "-" + advertisingRouterId; |
| 285 | addLocalLink(linkIdKey1, linkData, linkId, advertisingRouterId, true, false); |
| 286 | } else { |
| 287 | DeviceInformation deviceInformationToDelete = |
| 288 | createDeviceInformation(true, drRouterOld, drRouterOld, |
| 289 | drRouterOld, drRouterOld, |
| 290 | drRouterOld, true); |
| 291 | setDeviceInformationMapToDelete(key, deviceInformationToDelete); |
| 292 | String linkIdKey1 = "linkId:" + linkId + "-" + advertisingRouterId; |
| 293 | addLocalLink(linkIdKey1, linkData, linkId, advertisingRouterId, true, false); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | drRouter = linkId; |
| 298 | drRouterOld = linkId; |
| 299 | DeviceInformation deviceInformationForDr = |
| 300 | createDeviceInformation(false, linkId, advertisingRouterId, linkId, linkData, |
| 301 | ospfArea.areaId(), true); |
| 302 | String key = "device:" + linkId; |
| 303 | setDeviceInformationMap(key, deviceInformationForDr); |
| 304 | DeviceInformation deviceInformationForAdvertisingRouter = |
| 305 | createDeviceInformation(false, linkId, advertisingRouterId, advertisingRouterId, |
| 306 | linkData, ospfArea.areaId(), false); |
| 307 | String key1 = "device:" + advertisingRouterId; |
| 308 | setDeviceInformationMap(key1, deviceInformationForAdvertisingRouter); |
| 309 | if (drRouter.equals(Ip4Address.valueOf("0.0.0.0"))) { |
| 310 | System.out.println("Link will not get create since dr is not valid"); |
| 311 | //Need to analysis since this place will not get Dr information |
| 312 | String linkIdKey = "linkId:" + linkId + "-" + advertisingRouterId; |
| 313 | addLocalLink(linkIdKey, linkData, linkId, advertisingRouterId, true, false); |
| 314 | } else { |
| 315 | String linkIdKey = "linkId:" + drRouter + "-" + advertisingRouterId; |
| 316 | addLocalLink(linkIdKey, linkData, drRouter, advertisingRouterId, true, false); |
| 317 | } |
| 318 | } else { |
| 319 | DeviceInformation deviceInformationDrOther = |
| 320 | createDeviceInformation(false, linkId, linkId, advertisingRouterId, |
| 321 | linkData, ospfArea.areaId(), false); |
| 322 | String key = "device:" + advertisingRouterId; |
| 323 | setDeviceInformationMap(key, deviceInformationDrOther); |
| 324 | if (drRouter.equals(Ip4Address.valueOf("0.0.0.0"))) { |
| 325 | String linkIdKey = "linkId:" + linkId + "-" + advertisingRouterId; |
| 326 | addLocalLink(linkIdKey, linkData, linkId, advertisingRouterId, true, false); |
| 327 | } else { |
| 328 | String linkIdKey = "linkId:" + drRouter + "-" + advertisingRouterId; |
| 329 | addLocalLink(linkIdKey, linkData, drRouter, advertisingRouterId, true, false); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Creates Device and Link instance from the NetworkLsa parameters. |
| 339 | * |
| 340 | * @param ospfLsa OSPF LSA instance |
| 341 | * @param ospfArea OSPF area instance |
| 342 | */ |
| 343 | private void createDeviceAndLinkFromNetworkLsa(OspfLsa ospfLsa, OspfArea ospfArea) { |
| 344 | NetworkLsa networkLsa = (NetworkLsa) ospfLsa; |
| 345 | Ip4Address advertisingRouterId = networkLsa.networkMask(); |
| 346 | System.out.println("AdvertisingRouterId is : " + advertisingRouterId); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Creates Device and Link instance from the OpaqueLsa parameters. |
| 351 | * |
| 352 | * @param ospfLsa OSPF LSA instance |
| 353 | * @param ospfArea OSPF area instance |
| 354 | */ |
| 355 | private void createDeviceAndLinkFromOpaqueLsa(OspfLsa ospfLsa, OspfArea ospfArea) { |
| 356 | OspfLinkTed ospfLinkTed = new OspfLinkTedImpl(); |
| 357 | OpaqueLsa10 opaqueLsa10 = (OpaqueLsa10) ospfLsa; |
| 358 | List<TopLevelTlv> topLevelTlvList = opaqueLsa10.topLevelValues(); |
| 359 | for (TopLevelTlv topLevelTlv : topLevelTlvList) { |
| 360 | if (topLevelTlv instanceof LinkTlv) { |
| 361 | LinkTlv linkTlv = (LinkTlv) topLevelTlv; |
| 362 | List<LinkSubType> subTypes = linkTlv.subTlvList(); |
| 363 | for (LinkSubType type : subTypes) { |
| 364 | if (type instanceof UnreservedBandwidth) { |
| 365 | UnreservedBandwidth unreservedBandwidth = (UnreservedBandwidth) type; |
| 366 | List<Float> bandwidthFloatValues = unreservedBandwidth.getUnReservedBandwidthValue(); |
| 367 | List<Bandwidth> bandwidthList = new ArrayList<>(); |
| 368 | for (Float value : bandwidthFloatValues) { |
| 369 | Bandwidth bandwidth = Bandwidth.bps((double) value); |
| 370 | ospfLinkTed.setMaxUnResBandwidth(bandwidth); |
| 371 | bandwidthList.add(bandwidth); |
| 372 | } |
| 373 | } |
| 374 | if (type instanceof MaximumBandwidth) { |
| 375 | MaximumBandwidth maximumBandwidth = (MaximumBandwidth) type; |
| 376 | float maxBandValue = maximumBandwidth.getMaximumBandwidthValue(); |
| 377 | Bandwidth bandwidth = Bandwidth.bps((double) maxBandValue); |
| 378 | ospfLinkTed.setMaximumLink(bandwidth); |
| 379 | } |
| 380 | if (type instanceof MaximumReservableBandwidth) { |
| 381 | MaximumReservableBandwidth maximumReservableBandwidth = (MaximumReservableBandwidth) type; |
| 382 | float maxResBandValue = maximumReservableBandwidth.getMaximumBandwidthValue(); |
| 383 | Bandwidth bandwidth = Bandwidth.bps((double) maxResBandValue); |
| 384 | ospfLinkTed.setMaxReserved(bandwidth); |
| 385 | } |
| 386 | if (type instanceof TrafficEngineeringMetric) { |
| 387 | TrafficEngineeringMetric trafficEngineeringMetric = (TrafficEngineeringMetric) type; |
| 388 | long teMetric = trafficEngineeringMetric.getTrafficEngineeringMetricValue(); |
| 389 | ospfLinkTed.setTeMetric((Integer) (int) teMetric); |
| 390 | } |
| 391 | if (type instanceof LocalInterfaceIpAddress) { |
| 392 | LocalInterfaceIpAddress localInterfaceIpAddress = (LocalInterfaceIpAddress) type; |
| 393 | List<String> stringValue = localInterfaceIpAddress.getLocalInterfaceIPAddress(); |
| 394 | List<Ip4Address> localIp4Address = new ArrayList<>(); |
| 395 | for (String value : stringValue) { |
| 396 | Ip4Address ip4Address = Ip4Address.valueOf(value); |
| 397 | localIp4Address.add(ip4Address); |
| 398 | } |
| 399 | ospfLinkTed.setIpv4LocRouterId(localIp4Address); |
| 400 | } |
| 401 | if (type instanceof RemoteInterfaceIpAddress) { |
| 402 | RemoteInterfaceIpAddress remoteInterfaceIpAddress = (RemoteInterfaceIpAddress) type; |
| 403 | List<String> stringValue = remoteInterfaceIpAddress.getRemoteInterfaceAddress(); |
| 404 | List<Ip4Address> remoteIp4Address = new ArrayList<>(); |
| 405 | for (String value : stringValue) { |
| 406 | Ip4Address ip4Address = Ip4Address.valueOf(value); |
| 407 | remoteIp4Address.add(ip4Address); |
| 408 | } |
| 409 | ospfLinkTed.setIpv4RemRouterId(remoteIp4Address); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | ospfLinkTedHashMap.put(adRouterId.toString(), ospfLinkTed); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Adds link information to LinkInformationMap. |
| 421 | * |
| 422 | * @param advertisingRouter advertising router |
| 423 | * @param linkData link data address |
| 424 | * @param linkSrc link source address |
| 425 | * @param linkDest link destination address |
| 426 | * @param opaqueEnabled opaque enabled or not |
| 427 | * @param linkSrcIdNotRouterId link source id or not |
| 428 | */ |
| 429 | public void addLocalLink(String advertisingRouter, Ip4Address linkData, Ip4Address linkSrc, Ip4Address linkDest, |
| 430 | boolean opaqueEnabled, boolean linkSrcIdNotRouterId) { |
| 431 | String linkKey = "link:"; |
| 432 | LinkInformation linkInformation = new LinkInformationImpl(); |
| 433 | linkInformation.setLinkId(advertisingRouter); |
| 434 | linkInformation.setLinkSourceId(linkSrc); |
| 435 | linkInformation.setLinkDestinationId(linkDest); |
| 436 | linkInformation.setAlreadyCreated(false); |
| 437 | linkInformation.setLinkSrcIdNotRouterId(linkSrcIdNotRouterId); |
| 438 | linkInformation.setInterfaceIp(linkData); |
| 439 | if (linkDest != null) { |
| 440 | linkInformation.setLinkSrcIdNotRouterId(false); |
| 441 | } |
| 442 | linkKey = linkKey + "-" + linkSrc + "-" + linkDest; |
| 443 | setLinkInformationMap(linkKey, linkInformation); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Removes links from LinkInformationMap. |
| 448 | * |
| 449 | * @param routerId router id |
| 450 | */ |
| 451 | public void removeLinks(Ip4Address routerId) { |
| 452 | Map<String, LinkInformation> linkInformationMaplocal = linkInformationMap; |
| 453 | if (linkInformationMaplocal != null) { |
| 454 | for (Map.Entry<String, LinkInformation> entry : linkInformationMap.entrySet()) { |
| 455 | String key = entry.getKey(); |
| 456 | boolean check = key.contains(routerId.toString()); |
| 457 | LinkInformation linkInformation = linkInformationMap.get(key); |
| 458 | boolean check1 = (linkInformation.linkDestinationId() == routerId) ? true : false; |
| 459 | if (check || check1) { |
| 460 | toRemove.add(key); |
| 461 | } |
| 462 | } |
| 463 | removeLinkFromMap(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Removes Device from DeviceInformationMap. |
| 469 | * |
| 470 | * @param routerId router id |
| 471 | */ |
| 472 | public void removeDevice(Ip4Address routerId) { |
| 473 | String key = "device:" + routerId; |
| 474 | this.deviceInformationMap.remove(key); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Removes link information from Map. |
| 479 | */ |
| 480 | private void removeLinkFromMap() { |
| 481 | Iterator iterator = toRemove.iterator(); |
| 482 | while (iterator.hasNext()) { |
| 483 | String key = (String) iterator.next(); |
| 484 | removeLinkInformationMap(key); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Updates the deviceAndLinkInformation list for received OSPF LSA. |
| 490 | * |
| 491 | * @param ospfLsa OSPF LSA instance |
| 492 | * @param ospfArea OSPF area instance |
| 493 | */ |
| 494 | public void updateLinkInformation(OspfLsa ospfLsa, OspfArea ospfArea) { |
| 495 | if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) { |
| 496 | RouterLsa routerLsa = (RouterLsa) ospfLsa; |
| 497 | routerLsa.lsType(); |
| 498 | List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink(); |
| 499 | for (OspfLsaLink link : ospfLsaLinkList) { |
| 500 | if (link.linkType == 1 || link.linkType == 2) { |
| 501 | if ((routerLsa.advertisingRouter().equals(ospfArea.routerId())) || |
| 502 | (link.equals(ospfArea.routerId()))) { |
| 503 | log.debug("OspfInterface information will not display in web "); |
| 504 | } else { |
| 505 | String key = routerLsa.advertisingRouter() + "-" + link.linkData(); |
| 506 | Set<OspfLsaLink> linkInformations = new HashSet<>(); |
| 507 | if (deviceAndLinkInformation.containsKey(key)) { |
| 508 | linkInformations = deviceAndLinkInformation.get(key); |
| 509 | linkInformations.add(link); |
| 510 | deviceAndLinkInformation.put(key, linkInformations); |
| 511 | } else { |
| 512 | linkInformations.add(link); |
| 513 | deviceAndLinkInformation.put(key, linkInformations); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Gets all the router information which needs to delete from deviceList. |
| 523 | * |
| 524 | * @param ospfLsa OSPF LSA instance |
| 525 | * @param ospfArea OSPF area instance |
| 526 | * @return list of deleted router information |
| 527 | */ |
| 528 | public List<String> getDeleteRouterInformation(OspfLsa ospfLsa, OspfArea ospfArea) { |
| 529 | List<String> removedLinkList = new ArrayList<>(); |
| 530 | if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) { |
| 531 | |
| 532 | RouterLsa routerLsa = (RouterLsa) ospfLsa; |
| 533 | List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink(); |
| 534 | for (OspfLsaLink link : ospfLsaLinkList) { |
| 535 | if (link.linkType == 1 || link.linkType == 2) { |
| 536 | if ((routerLsa.advertisingRouter().equals(ospfArea.routerId())) || |
| 537 | (link.equals(ospfArea.routerId()))) { |
| 538 | log.debug("OspfInterface information will not display in web "); |
| 539 | } else { |
| 540 | String key = routerLsa.advertisingRouter() + "-" + link.linkData(); |
| 541 | Set<OspfLsaLink> linkInformations = deviceAndLinkInformation.get(key); |
| 542 | if (linkInformations.contains(link)) { |
| 543 | linkInformations.remove(link); |
| 544 | deviceAndLinkInformation.put(key, linkInformations); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | Set<String> keys = deviceAndLinkInformation.keySet(); |
| 549 | for (String key : keys) { |
| 550 | Set<OspfLsaLink> linkInformations = deviceAndLinkInformation.get(key); |
| 551 | for (OspfLsaLink link1 : linkInformations) { |
| 552 | String removedLink = link1.linkId(); |
| 553 | removedLinkList.add(removedLink); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | return removedLinkList; |
| 559 | } |
| 560 | } |