blob: 1aae861346d3aa99a1d64aaa64ccae92faa32250 [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05303*
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*/
16package org.onosproject.ospf.controller.impl;
17
18import org.onlab.packet.Ip4Address;
19import org.onlab.util.Bandwidth;
20import org.onosproject.ospf.controller.DeviceInformation;
21import org.onosproject.ospf.controller.LinkInformation;
22import org.onosproject.ospf.controller.OspfArea;
23import org.onosproject.ospf.controller.OspfInterface;
24import org.onosproject.ospf.controller.OspfLinkTed;
25import org.onosproject.ospf.controller.OspfLsa;
26import org.onosproject.ospf.controller.OspfLsaType;
27import org.onosproject.ospf.controller.TopologyForDeviceAndLink;
28import org.onosproject.ospf.protocol.lsa.linksubtype.LinkSubType;
29import org.onosproject.ospf.protocol.lsa.linksubtype.LocalInterfaceIpAddress;
30import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumBandwidth;
31import org.onosproject.ospf.protocol.lsa.linksubtype.MaximumReservableBandwidth;
32import org.onosproject.ospf.protocol.lsa.linksubtype.RemoteInterfaceIpAddress;
33import org.onosproject.ospf.protocol.lsa.linksubtype.TrafficEngineeringMetric;
34import org.onosproject.ospf.protocol.lsa.linksubtype.UnreservedBandwidth;
35import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
36import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
37import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
38import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
39import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
40import org.onosproject.ospf.protocol.lsa.types.TopLevelTlv;
sunishvkf7c56552016-07-18 16:02:39 +053041import org.onosproject.ospf.protocol.util.OspfUtil;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45import java.util.ArrayList;
46import java.util.HashMap;
47import java.util.HashSet;
48import java.util.Iterator;
49import java.util.LinkedHashMap;
50import java.util.List;
51import java.util.Map;
52import java.util.Set;
sunishvkf7c56552016-07-18 16:02:39 +053053import java.util.TreeSet;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053054
55/**
56 * Represents device and link topology information.
57 */
58public class TopologyForDeviceAndLinkImpl implements TopologyForDeviceAndLink {
59
60 private static final Logger log = LoggerFactory.getLogger(TopologyForDeviceAndLinkImpl.class);
sunishvkf7c56552016-07-18 16:02:39 +053061 Set<Ip4Address> routerSet = new TreeSet<Ip4Address>();
62 Ip4Address firstValue = Ip4Address.valueOf("0.0.0.0");
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053063 private Map<String, DeviceInformation> deviceInformationMap = new LinkedHashMap();
sunishvkf7c56552016-07-18 16:02:39 +053064 private Map<Ip4Address, List<Ip4Address>> networkLsaMap = new LinkedHashMap();
65 private Map<String, DeviceInformation> deviceInformationMapForPointToPoint = new LinkedHashMap();
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053066 private Map<String, DeviceInformation> deviceInformationMapToDelete = new LinkedHashMap();
67 private HashMap<String, Set<OspfLsaLink>> deviceAndLinkInformation = new HashMap();
68 private HashMap<String, OspfLinkTed> ospfLinkTedHashMap = new LinkedHashMap();
69 private Ip4Address drRouter = Ip4Address.valueOf("0.0.0.0");
70 private Ip4Address drRouterOld = Ip4Address.valueOf("0.0.0.0");
71 private Ip4Address adRouterId = Ip4Address.valueOf("0.0.0.0");
72 private Map<String, LinkInformation> linkInformationMap = new LinkedHashMap();
sunishvkf7c56552016-07-18 16:02:39 +053073 private Map<String, LinkInformation> linkInformationMapForPointToPoint = new LinkedHashMap();
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053074 private List<String> toRemove = new ArrayList<>();
75
76 /**
77 * Gets device information.
78 *
79 * @return device information
80 */
81 public Map<String, DeviceInformation> deviceInformationMap() {
82 return deviceInformationMap;
83 }
84
85 /**
86 * Sets device information.
87 *
88 * @param key key used to add in map
89 * @param deviceInformationMap device information instance
90 */
91 public void setDeviceInformationMap(String key, DeviceInformation deviceInformationMap) {
92 if (deviceInformationMap != null) {
93 this.deviceInformationMap.put(key, deviceInformationMap);
94 }
95
96 }
97
98 /**
sunishvkf7c56552016-07-18 16:02:39 +053099 * Gets deviceInformation as map for Point-To-Point.
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530100 *
sunishvkf7c56552016-07-18 16:02:39 +0530101 * @return deviceInformationMap
102 */
103 public Map<String, DeviceInformation> deviceInformationMapForPointToPoint() {
104 return deviceInformationMapForPointToPoint;
105 }
106
107 /**
108 * Sets deviceInformation as map for Point-To-Point..
109 *
110 * @param key key to store device information
111 * @param deviceInformationMap device information instance
112 */
113 public void setDeviceInformationMapForPointToPoint(String key, DeviceInformation deviceInformationMap) {
114 if (deviceInformationMap != null) {
115 this.deviceInformationMapForPointToPoint.put(key, deviceInformationMap);
116 }
117
118 }
119
120 /**
121 * Gets deviceInformation as map.
122 *
123 * @return deviceInformationMap to delete from core
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530124 */
125 public Map<String, DeviceInformation> deviceInformationMapToDelete() {
126 return deviceInformationMapToDelete;
127 }
128
129 /**
130 * Sets device information for removal.
131 *
132 * @param key ket used to add in map
133 * @param deviceInformationMapToDelete map from device information to remove
134 */
sunishvkf7c56552016-07-18 16:02:39 +0530135 public void setDeviceInformationMapToDelete(String key, DeviceInformation deviceInformationMapToDelete) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530136 if (deviceInformationMapToDelete != null) {
137 this.deviceInformationMapToDelete.put(key, deviceInformationMapToDelete);
138 }
139 }
140
141 /**
142 * Removes Device Information.
143 *
144 * @param key ket used to remove from map
145 */
146 public void removeDeviceInformationMapFromDeleteMap(String key) {
147 removeDeviceInformationMap(key);
148 if (this.deviceInformationMapToDelete.containsKey(key)) {
149 this.deviceInformationMapToDelete.remove(key);
150 }
151 }
152
153 /**
154 * Gets Device Information.
155 *
156 * @param key key to store in map
157 * @return Device Information
158 */
159 public DeviceInformation deviceInformation(String key) {
160 DeviceInformation deviceInformation = this.deviceInformationMap.get(key);
161 return deviceInformation;
162 }
163
164 /**
165 * Removes Device Information from map.
166 *
167 * @param key key used to remove from map
168 */
169 public void removeDeviceInformationMap(String key) {
170 if (this.deviceInformationMap.containsKey(key)) {
171 this.deviceInformationMap.remove(key);
172 }
173 }
174
175 /**
176 * Gets link information as map.
177 *
178 * @return link information as map
179 */
180 public Map<String, LinkInformation> linkInformationMap() {
181 return linkInformationMap;
182 }
183
sunishvkf7c56552016-07-18 16:02:39 +0530184 private LinkInformation getLinkInformation(String key) {
185 LinkInformation linkInformation = this.linkInformationMap.get(key);
186 return linkInformation;
187 }
188
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530189 /**
190 * Sets link information in map.
191 *
192 * @param key key used to add in map
193 * @param linkInformationMap link information instance
194 */
195 public void setLinkInformationMap(String key, LinkInformation linkInformationMap) {
196 if (!this.linkInformationMap.containsKey(key)) {
197 this.linkInformationMap.put(key, linkInformationMap);
198 }
199 }
200
201 /**
sunishvkf7c56552016-07-18 16:02:39 +0530202 * Gets linkInformation as map for PointToPoint.
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530203 *
sunishvkf7c56552016-07-18 16:02:39 +0530204 * @return linkInformationMap
205 */
206 public Map<String, LinkInformation> linkInformationMapForPointToPoint() {
207 return linkInformationMap;
208 }
209
210 /**
211 * Sets linkInformation as map for PointToPoint.
212 *
213 * @param key key to store link information
214 * @param linkInformationMap link information instance
215 */
216 public void setLinkInformationMapForPointToPoint(String key, LinkInformation linkInformationMap) {
217 if (!this.linkInformationMap.containsKey(key)) {
218 this.linkInformationMap.put(key, linkInformationMap);
219 }
220 }
221
222 /**
223 * Removes Link Information from linkInformationMap.
224 *
225 * @param key key to remove link information
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530226 */
227 public void removeLinkInformationMap(String key) {
228 if (this.linkInformationMap.containsKey(key)) {
229 this.linkInformationMap.remove(key);
230 }
231 }
232
233
234 /**
235 * Gets OSPF Link TED details from the map.
236 *
Frank Wangd8ab0962017-08-11 11:09:30 +0800237 * @param key key used to retrieve from map
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530238 * @return OSPF link ted instance
239 */
240 public OspfLinkTed getOspfLinkTedHashMap(String key) {
241 OspfLinkTed ospfLinkTed = ospfLinkTedHashMap.get(key);
242 return ospfLinkTed;
243 }
244
245 /**
246 * Adds device information to map.
247 *
248 * @param ospfLsa OSPF LSA instance
249 * @param ospfInterface OSPF interface instance
250 * @param ospfArea OSPF area instance
251 */
252 public void addLocalDevice(OspfLsa ospfLsa, OspfInterface ospfInterface, OspfArea ospfArea) {
253 if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) {
254 createDeviceAndLinkFromRouterLsa(ospfLsa, ospfArea);
255 } else if (ospfLsa.getOspfLsaType().equals(OspfLsaType.NETWORK)) {
256 createDeviceAndLinkFromNetworkLsa(ospfLsa, ospfArea);
257 } else if (ospfLsa.getOspfLsaType().equals(OspfLsaType.AREA_LOCAL_OPAQUE_LSA)) {
258 createDeviceAndLinkFromOpaqueLsa(ospfLsa, ospfArea);
259 }
260 }
261
262 /**
263 * Creates device object from parameters.
264 *
265 * @param alreadyCreated device already created or not
266 * @param deviceId device id
267 * @param neighborId neighbor's id
268 * @param routerId router's id
269 * @param interfaceId interface id
270 * @param areaId area id
271 * @param isDr true if router is DR else false
272 */
273 private DeviceInformation createDeviceInformation(boolean alreadyCreated, Ip4Address deviceId,
274 Ip4Address neighborId, Ip4Address routerId,
275 Ip4Address interfaceId, Ip4Address areaId,
276 boolean isDr) {
277 DeviceInformation deviceInformation = new DeviceInformationImpl();
278 deviceInformation.setAlreadyCreated(alreadyCreated);
279 deviceInformation.setDeviceId(deviceId);
280 deviceInformation.setNeighborId(neighborId);
281 deviceInformation.setRouterId(routerId);
282 deviceInformation.addInterfaceId(interfaceId);
283 deviceInformation.setAreaId(areaId);
284 deviceInformation.setDr(isDr);
285 return deviceInformation;
286 }
287
288 /**
289 * Creates Device and Link instance from the RouterLsa parameters.
290 *
291 * @param ospfLsa OSPF LSA instance
292 * @param ospfArea OSPF area
293 */
294 private void createDeviceAndLinkFromRouterLsa(OspfLsa ospfLsa, OspfArea ospfArea) {
295 RouterLsa routerLsa = (RouterLsa) ospfLsa;
296 List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink();
297 Iterator iterator = ospfLsaLinkList.iterator();
298 Ip4Address advertisingRouterId = routerLsa.advertisingRouter();
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530299 while (iterator.hasNext()) {
300 OspfLsaLink ospfLsaLink = (OspfLsaLink) iterator.next();
301 Ip4Address linkId = Ip4Address.valueOf(ospfLsaLink.linkId());
302 Ip4Address linkData = Ip4Address.valueOf(ospfLsaLink.linkData());
303 if (ospfLsaLink.linkType() == 1) {
304 if ((advertisingRouterId.equals(ospfArea.routerId())) || (linkId.equals(ospfArea.routerId()))) {
sunishvkf7c56552016-07-18 16:02:39 +0530305 if (!advertisingRouterId.equals(ospfArea.routerId())) {
306 DeviceInformation deviceInformationPointToPoint =
307 createDeviceInformation(false, linkId, linkId, advertisingRouterId, linkData,
308 ospfArea.areaId(), false);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530309 String key = "device:" + advertisingRouterId;
sunishvkf7c56552016-07-18 16:02:39 +0530310 setDeviceInformationMapForPointToPoint(key, deviceInformationPointToPoint);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530311 }
sunishvkf7c56552016-07-18 16:02:39 +0530312 } else {
313 DeviceInformation deviceInformationPointToPoint =
314 createDeviceInformation(false, linkId, linkId, advertisingRouterId,
315 linkData, ospfArea.areaId(), false);
316 String key = "device:" + advertisingRouterId;
317 setDeviceInformationMapForPointToPoint(key, deviceInformationPointToPoint);
318 String linkIdKey = "linkId:" + advertisingRouterId + "-" + linkId;
319 addLocalLinkForPointToPoint(linkIdKey, linkData, advertisingRouterId, linkId, true, false);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530320 }
321 }
322 }
323 }
324
325 /**
sunishvkf7c56552016-07-18 16:02:39 +0530326 * Creates device and link instance from the network LSA parameters.
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530327 *
328 * @param ospfLsa OSPF LSA instance
329 * @param ospfArea OSPF area instance
330 */
331 private void createDeviceAndLinkFromNetworkLsa(OspfLsa ospfLsa, OspfArea ospfArea) {
332 NetworkLsa networkLsa = (NetworkLsa) ospfLsa;
sunishvkf7c56552016-07-18 16:02:39 +0530333 Ip4Address linkStateId = Ip4Address.valueOf(networkLsa.linkStateId());
334 Set<Ip4Address> drList = networkLsaMap.keySet();
335 try {
336 Ip4Address drToReplace = null;
337 for (Ip4Address drIp : drList) {
338 if (!drIp.equals(linkStateId)) {
339 if (OspfUtil.sameNetwork(drIp, linkStateId, networkLsa.networkMask())) {
340 drToReplace = drIp;
341 String key = "device:" + drToReplace;
342 DeviceInformation deleteDr = deviceInformation(key);
343 if (deleteDr != null) {
344 deleteDr.setAlreadyCreated(true);
345 setDeviceInformationMapToDelete(key, deleteDr);
346 }
347
348 networkLsaMap.remove(drToReplace);
349 break;
350 }
351 }
352 }
353 networkLsaMap.put(linkStateId, networkLsa.attachedRouters());
354
355 } catch (Exception e) {
356 log.debug("Error::TopologyForDeviceAndLinkImpl:: {}", e.getMessage());
357 }
358 constructDeviceForBroadCastTopology(ospfArea);
359 disp();
360
361 }
362
363 private void constructDeviceForBroadCastTopology(OspfArea ospfArea) {
364
365 for (Map.Entry<Ip4Address, List<Ip4Address>> entry : networkLsaMap.entrySet()) {
366 Ip4Address key = entry.getKey();
367 DeviceInformation deviceInformationForDr = createDeviceInformation(false, key, key, key,
368 key, ospfArea.areaId(), true);
369 String dr = "device:" + key;
370 setDeviceInformationMap(dr, deviceInformationForDr);
371 List<Ip4Address> value = entry.getValue();
372 for (Ip4Address connectedRouter : value) {
373 if (!connectedRouter.equals(ospfArea.routerId())) {
374 DeviceInformation deviceInformationAttachedRouters =
375 createDeviceInformation(false, connectedRouter, key, connectedRouter,
376 key, ospfArea.areaId(), false);
377 String attachedRouters = "device:" + connectedRouter;
378 setDeviceInformationMap(attachedRouters, deviceInformationAttachedRouters);
379 String linkIdKey = "linkId:" + key + "-" + connectedRouter;
380 addLocalLink(linkIdKey, key, key, connectedRouter, true, false);
381 }
382 }
383 }
384
385 }
386
387 private void disp() {
388 for (String key : deviceInformationMap.keySet()) {
389 DeviceInformation deviceInformation = deviceInformationMap.get(key);
390 log.debug("************************************************************************");
391 log.debug("DeviceInfoList RouterId is : {} and neighbour is {} and linkdata {}",
392 deviceInformation.routerId(), deviceInformation.neighborId(), deviceInformation.interfaceId());
393 }
394
395 for (Map.Entry<String, LinkInformation> entry : linkInformationMap.entrySet()) {
396 String linkDetail = entry.getKey();
397 log.debug("Link From and to is " + linkDetail);
398 }
399 log.debug("Devices Needs to delete from Core are : " + deviceInformationMapToDelete.size());
400 for (String key : deviceInformationMapToDelete.keySet()) {
401 DeviceInformation value = deviceInformationMapToDelete.get(key);
402 if (value.isAlreadyCreated()) {
403 log.debug("Device is deleted from list " + value.routerId());
404 }
405 }
406 }
407
408 private void getLinksToDelete(Set<Ip4Address> list, Ip4Address value, OspfArea ospfArea) {
409
410 Iterator iterator = list.iterator();
411
412 while (iterator.hasNext()) {
413 Ip4Address secondValue = (Ip4Address) iterator.next();
Saritha69359512017-05-09 15:01:48 +0530414 if (!value.toString().equals("0.0.0.0")) {
sunishvkf7c56552016-07-18 16:02:39 +0530415 if ((!value.equals(secondValue))) {
416 if ((!secondValue.equals(ospfArea.routerId()))) {
417 String key = "link:" + value.toString() + "-" + secondValue.toString();
418 String key1 = "link:" + secondValue.toString() + "-" + value.toString();
419 LinkInformation linkDetails = getLinkInformation(key);
420 LinkInformation linkDetailsOther = getLinkInformation(key1);
421 linkInformationMapForPointToPoint.put(key, linkDetails);
422 linkInformationMapForPointToPoint.put(key1, linkDetailsOther);
423 }
424 }
425 }
426
427 }
428
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530429 }
430
431 /**
432 * Creates Device and Link instance from the OpaqueLsa parameters.
433 *
434 * @param ospfLsa OSPF LSA instance
435 * @param ospfArea OSPF area instance
436 */
437 private void createDeviceAndLinkFromOpaqueLsa(OspfLsa ospfLsa, OspfArea ospfArea) {
438 OspfLinkTed ospfLinkTed = new OspfLinkTedImpl();
439 OpaqueLsa10 opaqueLsa10 = (OpaqueLsa10) ospfLsa;
440 List<TopLevelTlv> topLevelTlvList = opaqueLsa10.topLevelValues();
441 for (TopLevelTlv topLevelTlv : topLevelTlvList) {
442 if (topLevelTlv instanceof LinkTlv) {
443 LinkTlv linkTlv = (LinkTlv) topLevelTlv;
444 List<LinkSubType> subTypes = linkTlv.subTlvList();
445 for (LinkSubType type : subTypes) {
446 if (type instanceof UnreservedBandwidth) {
447 UnreservedBandwidth unreservedBandwidth = (UnreservedBandwidth) type;
448 List<Float> bandwidthFloatValues = unreservedBandwidth.getUnReservedBandwidthValue();
449 List<Bandwidth> bandwidthList = new ArrayList<>();
450 for (Float value : bandwidthFloatValues) {
451 Bandwidth bandwidth = Bandwidth.bps((double) value);
452 ospfLinkTed.setMaxUnResBandwidth(bandwidth);
453 bandwidthList.add(bandwidth);
454 }
455 }
456 if (type instanceof MaximumBandwidth) {
457 MaximumBandwidth maximumBandwidth = (MaximumBandwidth) type;
458 float maxBandValue = maximumBandwidth.getMaximumBandwidthValue();
459 Bandwidth bandwidth = Bandwidth.bps((double) maxBandValue);
460 ospfLinkTed.setMaximumLink(bandwidth);
461 }
462 if (type instanceof MaximumReservableBandwidth) {
463 MaximumReservableBandwidth maximumReservableBandwidth = (MaximumReservableBandwidth) type;
464 float maxResBandValue = maximumReservableBandwidth.getMaximumBandwidthValue();
465 Bandwidth bandwidth = Bandwidth.bps((double) maxResBandValue);
466 ospfLinkTed.setMaxReserved(bandwidth);
467 }
468 if (type instanceof TrafficEngineeringMetric) {
469 TrafficEngineeringMetric trafficEngineeringMetric = (TrafficEngineeringMetric) type;
470 long teMetric = trafficEngineeringMetric.getTrafficEngineeringMetricValue();
471 ospfLinkTed.setTeMetric((Integer) (int) teMetric);
472 }
473 if (type instanceof LocalInterfaceIpAddress) {
474 LocalInterfaceIpAddress localInterfaceIpAddress = (LocalInterfaceIpAddress) type;
475 List<String> stringValue = localInterfaceIpAddress.getLocalInterfaceIPAddress();
476 List<Ip4Address> localIp4Address = new ArrayList<>();
477 for (String value : stringValue) {
478 Ip4Address ip4Address = Ip4Address.valueOf(value);
479 localIp4Address.add(ip4Address);
480 }
481 ospfLinkTed.setIpv4LocRouterId(localIp4Address);
482 }
483 if (type instanceof RemoteInterfaceIpAddress) {
484 RemoteInterfaceIpAddress remoteInterfaceIpAddress = (RemoteInterfaceIpAddress) type;
485 List<String> stringValue = remoteInterfaceIpAddress.getRemoteInterfaceAddress();
486 List<Ip4Address> remoteIp4Address = new ArrayList<>();
487 for (String value : stringValue) {
488 Ip4Address ip4Address = Ip4Address.valueOf(value);
489 remoteIp4Address.add(ip4Address);
490 }
491 ospfLinkTed.setIpv4RemRouterId(remoteIp4Address);
492 }
493 }
494 }
495
496 }
497 ospfLinkTedHashMap.put(adRouterId.toString(), ospfLinkTed);
498 }
499
500
501 /**
502 * Adds link information to LinkInformationMap.
503 *
504 * @param advertisingRouter advertising router
505 * @param linkData link data address
506 * @param linkSrc link source address
507 * @param linkDest link destination address
508 * @param opaqueEnabled opaque enabled or not
509 * @param linkSrcIdNotRouterId link source id or not
510 */
511 public void addLocalLink(String advertisingRouter, Ip4Address linkData, Ip4Address linkSrc, Ip4Address linkDest,
512 boolean opaqueEnabled, boolean linkSrcIdNotRouterId) {
513 String linkKey = "link:";
514 LinkInformation linkInformation = new LinkInformationImpl();
515 linkInformation.setLinkId(advertisingRouter);
516 linkInformation.setLinkSourceId(linkSrc);
517 linkInformation.setLinkDestinationId(linkDest);
518 linkInformation.setAlreadyCreated(false);
519 linkInformation.setLinkSrcIdNotRouterId(linkSrcIdNotRouterId);
520 linkInformation.setInterfaceIp(linkData);
521 if (linkDest != null) {
522 linkInformation.setLinkSrcIdNotRouterId(false);
523 }
524 linkKey = linkKey + "-" + linkSrc + "-" + linkDest;
525 setLinkInformationMap(linkKey, linkInformation);
526 }
527
528 /**
sunishvkf7c56552016-07-18 16:02:39 +0530529 * Adds link information to LinkInformationMap for PointToPoint.
530 *
531 * @param advertisingRouter advertising router
532 * @param linkData link data
533 * @param linkSrc link source
534 * @param linkDest link destination
535 * @param opaqueEnabled whether opaque is enabled or not
536 * @param linkSrcIdNotRouterId whether link is source id or router id
537 */
538 public void addLocalLinkForPointToPoint(String advertisingRouter, Ip4Address linkData, Ip4Address linkSrc,
539 Ip4Address linkDest, boolean opaqueEnabled, boolean linkSrcIdNotRouterId) {
540 String linkKey = "link:";
541 LinkInformation linkInformation = new LinkInformationImpl();
542 linkInformation.setLinkId(advertisingRouter);
543 linkInformation.setLinkSourceId(linkSrc);
544 linkInformation.setLinkDestinationId(linkDest);
545 linkInformation.setAlreadyCreated(false);
546 linkInformation.setLinkSrcIdNotRouterId(linkSrcIdNotRouterId);
547 linkInformation.setInterfaceIp(linkData);
548 if (linkDest != null) {
549 linkInformation.setLinkSrcIdNotRouterId(false);
550 }
551 linkKey = linkKey + "-" + linkSrc + "-" + linkDest;
552 setLinkInformationMapForPointToPoint(linkKey, linkInformation);
553 }
554
555 /**
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530556 * Removes links from LinkInformationMap.
557 *
558 * @param routerId router id
559 */
560 public void removeLinks(Ip4Address routerId) {
sunishvkf7c56552016-07-18 16:02:39 +0530561 Map<String, LinkInformation> linkInformationMapLocal = linkInformationMap;
562 if (linkInformationMapLocal != null) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530563 for (Map.Entry<String, LinkInformation> entry : linkInformationMap.entrySet()) {
564 String key = entry.getKey();
565 boolean check = key.contains(routerId.toString());
566 LinkInformation linkInformation = linkInformationMap.get(key);
567 boolean check1 = (linkInformation.linkDestinationId() == routerId) ? true : false;
568 if (check || check1) {
569 toRemove.add(key);
570 }
571 }
572 removeLinkFromMap();
573 }
574 }
575
576 /**
577 * Removes Device from DeviceInformationMap.
578 *
579 * @param routerId router id
580 */
581 public void removeDevice(Ip4Address routerId) {
582 String key = "device:" + routerId;
583 this.deviceInformationMap.remove(key);
584 }
585
586 /**
587 * Removes link information from Map.
588 */
589 private void removeLinkFromMap() {
590 Iterator iterator = toRemove.iterator();
591 while (iterator.hasNext()) {
592 String key = (String) iterator.next();
593 removeLinkInformationMap(key);
594 }
595 }
596
597 /**
598 * Updates the deviceAndLinkInformation list for received OSPF LSA.
599 *
600 * @param ospfLsa OSPF LSA instance
601 * @param ospfArea OSPF area instance
602 */
603 public void updateLinkInformation(OspfLsa ospfLsa, OspfArea ospfArea) {
604 if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) {
605 RouterLsa routerLsa = (RouterLsa) ospfLsa;
606 routerLsa.lsType();
607 List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink();
608 for (OspfLsaLink link : ospfLsaLinkList) {
Ray Milkeye4afdb52017-04-05 09:42:04 -0700609 if (link.linkType() == 1 || link.linkType() == 2) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530610 if ((routerLsa.advertisingRouter().equals(ospfArea.routerId())) ||
611 (link.equals(ospfArea.routerId()))) {
612 log.debug("OspfInterface information will not display in web ");
613 } else {
614 String key = routerLsa.advertisingRouter() + "-" + link.linkData();
615 Set<OspfLsaLink> linkInformations = new HashSet<>();
616 if (deviceAndLinkInformation.containsKey(key)) {
617 linkInformations = deviceAndLinkInformation.get(key);
618 linkInformations.add(link);
619 deviceAndLinkInformation.put(key, linkInformations);
620 } else {
621 linkInformations.add(link);
622 deviceAndLinkInformation.put(key, linkInformations);
623 }
624 }
625 }
626 }
627 }
628 }
629
630 /**
631 * Gets all the router information which needs to delete from deviceList.
632 *
633 * @param ospfLsa OSPF LSA instance
634 * @param ospfArea OSPF area instance
635 * @return list of deleted router information
636 */
637 public List<String> getDeleteRouterInformation(OspfLsa ospfLsa, OspfArea ospfArea) {
638 List<String> removedLinkList = new ArrayList<>();
639 if (ospfLsa.getOspfLsaType().equals(OspfLsaType.ROUTER)) {
640
641 RouterLsa routerLsa = (RouterLsa) ospfLsa;
642 List<OspfLsaLink> ospfLsaLinkList = routerLsa.routerLink();
643 for (OspfLsaLink link : ospfLsaLinkList) {
Ray Milkeye4afdb52017-04-05 09:42:04 -0700644 if (link.linkType() == 1 || link.linkType() == 2) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530645 if ((routerLsa.advertisingRouter().equals(ospfArea.routerId())) ||
646 (link.equals(ospfArea.routerId()))) {
647 log.debug("OspfInterface information will not display in web ");
648 } else {
649 String key = routerLsa.advertisingRouter() + "-" + link.linkData();
650 Set<OspfLsaLink> linkInformations = deviceAndLinkInformation.get(key);
651 if (linkInformations.contains(link)) {
652 linkInformations.remove(link);
653 deviceAndLinkInformation.put(key, linkInformations);
654 }
655 }
656 }
657 Set<String> keys = deviceAndLinkInformation.keySet();
658 for (String key : keys) {
659 Set<OspfLsaLink> linkInformations = deviceAndLinkInformation.get(key);
660 for (OspfLsaLink link1 : linkInformations) {
661 String removedLink = link1.linkId();
662 removedLinkList.add(removedLink);
663 }
664 }
665 }
666 }
667 return removedLinkList;
668 }
Ray Milkeye4afdb52017-04-05 09:42:04 -0700669}