blob: 3c523088304aa7cd4b37e435b297463c6d5d6272 [file] [log] [blame]
sunish vk7bdf4d42016-06-24 12:29:43 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
sunish vk7bdf4d42016-06-24 12:29:43 +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.isis.controller.impl.topology;
17
18import org.onlab.util.Bandwidth;
19import org.onosproject.isis.controller.topology.DeviceInformation;
20import org.onosproject.isis.controller.topology.IsisRouter;
21import org.onosproject.isis.controller.topology.LinkInformation;
22import org.onosproject.isis.controller.topology.TopologyForDeviceAndLink;
23import org.onosproject.isis.controller.topology.IsisLinkTed;
24import org.onosproject.isis.io.isispacket.pdu.LsPdu;
25import org.onosproject.isis.io.isispacket.tlv.IsExtendedReachability;
26import org.onosproject.isis.io.isispacket.tlv.IsisTlv;
27import org.onosproject.isis.io.isispacket.tlv.NeighborForExtendedIs;
28
29import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
30import org.onosproject.isis.io.isispacket.tlv.subtlv.InterfaceIpAddress;
31import org.onosproject.isis.io.isispacket.tlv.subtlv.NeighborIpAddress;
32import org.onosproject.isis.io.isispacket.tlv.subtlv.AdministrativeGroup;
33import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringMetric;
34import org.onosproject.isis.io.isispacket.tlv.subtlv.UnreservedBandwidth;
35import org.onosproject.isis.io.isispacket.tlv.subtlv.MaximumReservableBandwidth;
36import org.onosproject.isis.io.isispacket.tlv.subtlv.MaximumBandwidth;
37import org.onosproject.isis.io.util.IsisConstants;
38import org.onosproject.isis.io.util.IsisUtil;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import java.util.ArrayList;
43import java.util.LinkedHashMap;
44import java.util.List;
45import java.util.Map;
46
47/**
48 * Represents device and link topology information.
49 */
50public class TopologyForDeviceAndLinkImpl implements TopologyForDeviceAndLink {
51
52 private static final Logger log = LoggerFactory.getLogger(TopologyForDeviceAndLinkImpl.class);
53 private Map<String, DeviceInformation> deviceInformationMap = new LinkedHashMap<>();
54 private Map<String, IsisRouter> isisRouterDetails = new LinkedHashMap<>();
55 private Map<String, DeviceInformation> deviceInformationMapForPointToPoint = new LinkedHashMap<>();
56 private Map<String, DeviceInformation> deviceInformationMapToDelete = new LinkedHashMap<>();
57 private Map<String, LinkInformation> addedLinkInformationMap = new LinkedHashMap<>();
58
59 /**
60 * Gets device information.
61 *
62 * @return device information
63 */
64 public Map<String, DeviceInformation> deviceInformationMap() {
65 return deviceInformationMap;
66 }
67
68 /**
69 * Gets ISIS router list information.
70 *
71 * @return router information
72 */
73 public Map<String, IsisRouter> isisDeviceList() {
74 return isisRouterDetails;
75 }
76
77 /**
78 * Sets device information.
79 *
80 * @param key key used to add in map
81 * @param deviceInformationMap device information instance
82 */
83 public void setDeviceInformationMap(String key, DeviceInformation deviceInformationMap) {
84 if (deviceInformationMap != null) {
85 this.deviceInformationMap.put(key, deviceInformationMap);
86 }
87
88 }
89
90 /**
91 * Gets deviceInformation as map for Point-To-Point.
92 *
93 * @return deviceInformationMap
94 */
95 public Map<String, DeviceInformation> deviceInformationMapForPointToPoint() {
96 return deviceInformationMapForPointToPoint;
97 }
98
99 /**
100 * Sets deviceInformation as map for Point-To-Point..
101 *
102 * @param key key used to add in map
103 * @param deviceInformationMap device information instance
104 */
105 public void setDeviceInformationMapForPointToPoint(String key, DeviceInformation deviceInformationMap) {
106 if (deviceInformationMap != null) {
107 this.deviceInformationMapForPointToPoint.put(key, deviceInformationMap);
108 }
109
110 }
111
112 /**
113 * Gets deviceInformation as map.
114 *
115 * @return deviceInformationMap to delete from core
116 */
117 public Map<String, DeviceInformation> deviceInformationMapToDelete() {
118 return deviceInformationMapToDelete;
119 }
120
121 /**
122 * Sets device information for removal.
123 *
124 * @param key ket used to add in map
125 * @param deviceInformationMapToDelete map from device information to remove
126 */
127 public void setDeviceInformationMapToDelete(String key, DeviceInformation deviceInformationMapToDelete) {
128 if (deviceInformationMapToDelete != null) {
129 this.deviceInformationMapToDelete.put(key, deviceInformationMapToDelete);
130 }
131 }
132
133 /**
134 * Removes Device Information.
135 *
136 * @param key ket used to remove from map
137 */
138 public void removeDeviceInformationMapFromDeleteMap(String key) {
139 removeDeviceInformationMap(key);
140 if (this.deviceInformationMapToDelete.containsKey(key)) {
141 this.deviceInformationMapToDelete.remove(key);
142 }
143 }
144
145 /**
146 * Gets Device Information.
147 *
148 * @param key system id as key to store in map
149 * @return Device Information
150 */
151 public DeviceInformation deviceInformation(String key) {
152 DeviceInformation deviceInformation = this.deviceInformationMap.get(key);
153 return deviceInformation;
154 }
155
156 /**
157 * Removes Device Information from map.
158 *
159 * @param key key used to remove from map
160 */
161 public void removeDeviceInformationMap(String key) {
162 if (this.deviceInformationMap.containsKey(key)) {
163 this.deviceInformationMap.remove(key);
164 }
165 }
166
167 @Override
168 public void removeLinks(String linkId) {
169 this.addedLinkInformationMap.remove(linkId);
170 }
171
172 /**
173 * Gets link information as map.
174 *
175 * @return link information as map
176 */
177 public Map<String, LinkInformation> linkInformationMap() {
178 return addedLinkInformationMap;
179 }
180
181 private LinkInformation getLinkInformation(String key) {
182 LinkInformation linkInformation = this.addedLinkInformationMap.get(key);
183 return linkInformation;
184 }
185
186 /**
187 * Sets link information in map.
188 *
189 * @param key key used to add in map
190 * @param linkInformationMap link information instance
191 */
192 public void setLinkInformationMap(String key, LinkInformation linkInformationMap) {
193 if (!this.addedLinkInformationMap.containsKey(key)) {
194 this.addedLinkInformationMap.put(key, linkInformationMap);
195 }
196 }
197
198 /**
199 * Gets linkInformation as map for PointToPoint.
200 *
201 * @return linkInformationMap
202 */
203 public Map<String, LinkInformation> linkInformationMapForPointToPoint() {
204 return addedLinkInformationMap;
205 }
206
207 /**
208 * Sets linkInformation as map for PointToPoint.
209 *
210 * @param key key used to add in map
211 * @param linkInformationMap link information instance
212 */
213 public void setLinkInformationMapForPointToPoint(String key, LinkInformation linkInformationMap) {
214 if (!this.addedLinkInformationMap.containsKey(key)) {
215 this.addedLinkInformationMap.put(key, linkInformationMap);
216 }
217 }
218
219 /**
220 * Removes Link Information from linkInformationMap.
221 *
222 * @param key key used to remove in map
223 */
224 public void removeLinkInformationMap(String key) {
225 if (this.addedLinkInformationMap.containsKey(key)) {
226 this.addedLinkInformationMap.remove(key);
227 }
228 }
229
230 /**
231 * Returns the ISIS router instance.
232 *
233 * @param systemId system ID to get router details
234 * @return ISIS router instance
235 */
236 public IsisRouter isisRouter(String systemId) {
237 String routerId = IsisUtil.removeTailingZeros(systemId);
238 IsisRouter isisRouter = isisRouterDetails.get(routerId);
239 if (isisRouter != null) {
240 return isisRouter;
241 } else {
242 log.debug("IsisRouter is not available");
243 IsisRouter isisRouterCheck = new DefaultIsisRouter();
244 isisRouterCheck.setSystemId(routerId);
245 return isisRouterCheck;
246 }
247 }
248
249 /**
250 * Removes the ISIS router instance from map.
251 *
252 * @param systemId system ID to remove router details
253 */
254 public void removeRouter(String systemId) {
255 String routerId = IsisUtil.removeTailingZeros(systemId);
256 isisRouterDetails.remove(systemId);
257 }
258
259 /**
260 * Creates Device instance.
261 *
262 * @param lsPdu ISIS LSPDU instance
263 * @return isisRouter isisRouter instance
264 */
265 public IsisRouter createDeviceInfo(LsPdu lsPdu) {
266 IsisRouter isisRouter = createIsisRouter(lsPdu);
267
268 if (isisRouter.systemId() != null) {
269 if (isisRouter.interfaceId() == null && isisRouter.neighborRouterId() == null) {
270 isisRouter.setInterfaceId(IsisConstants.DEFAULTIP);
271 isisRouter.setNeighborRouterId(IsisConstants.DEFAULTIP);
272 isisRouterDetails.put(isisRouter.systemId(), isisRouter);
273 }
274 }
275 return isisRouter;
Ray Milkey88cc3432017-03-30 17:19:08 -0700276 }
sunish vk7bdf4d42016-06-24 12:29:43 +0530277
Ray Milkey88cc3432017-03-30 17:19:08 -0700278 /**
sunish vk7bdf4d42016-06-24 12:29:43 +0530279 * Removes Device and Link instance.
280 *
281 * @param lsPdu ISIS LSPDU instance
282 * @return isisRouter isisRouter instance
Ray Milkey88cc3432017-03-30 17:19:08 -0700283 */
284 /*
sunish vk7bdf4d42016-06-24 12:29:43 +0530285 public IsisRouter removeDeviceAndLinkInfo(LsPdu lsPdu) {
286 IsisRouter isisRouter = createIsisRouter(lsPdu);
287 return isisRouter;
288 }*/
289
290 /**
291 * Creates link information.
292 *
293 * @param lsPdu ls pdu instance
294 * @param ownSystemId system ID
295 * @return link information
296 */
297 public Map<String, LinkInformation> createLinkInfo(LsPdu lsPdu, String ownSystemId) {
298 for (IsisTlv isisTlv : lsPdu.tlvs()) {
299 if (isisTlv instanceof IsExtendedReachability) {
300 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
301 List<NeighborForExtendedIs> neighborForExtendedIsList = isExtendedReachability.neighbours();
302 for (NeighborForExtendedIs neighbor : neighborForExtendedIsList) {
303 String neighbourId = neighbor.neighborId();
304 String routerId = IsisUtil.removeTailingZeros(lsPdu.lspId());
305 if (!(neighbourId.equals(ownSystemId))) {
306 IsisRouter isisRouter = isisRouterDetails.get(neighbourId);
307 if (isisRouter != null) {
308 String linkId = "link:" + routerId + "-" + neighbourId;
309 addedLinkInformationMap.put(linkId, createLinkInformation(lsPdu, linkId,
310 routerId, neighbourId));
311 } else {
312 createIsisRouterDummy(neighbourId);
313 String linkId = "link:" + routerId + "-" + neighbourId;
314 LinkInformation linkInformation = createLinkInformation(lsPdu, linkId,
315 routerId, neighbourId);
316 linkInformation.setAlreadyCreated(true);
317 addedLinkInformationMap.put(linkId, linkInformation);
318 }
319 }
320
321 }
322 }
323 }
324 return addedLinkInformationMap;
325 }
326
327 /**
328 * Removes link information.
329 *
330 * @param systemId system ID to remove link information
331 * @return updated link information
332 */
333 public Map<String, LinkInformation> removeLinkInfo(String systemId) {
334 String routerId = IsisUtil.removeTailingZeros(systemId);
335 Map<String, LinkInformation> removeLinkInformationMap = new LinkedHashMap<>();
336 for (String key : addedLinkInformationMap.keySet()) {
337 if (key.contains(routerId)) {
338 removeLinkInformationMap.put(key, addedLinkInformationMap.get(key));
339 }
340 }
341 return removeLinkInformationMap;
342 }
343
344 /**
345 * Creates link information.
346 *
347 * @param lsPdu link state pdu
348 * @param linkId link id
349 * @param localRouter local router system id
350 * @param neighborId destination router system id
351 * @return linkInformation instance
352 */
353 private LinkInformation createLinkInformation(LsPdu lsPdu, String linkId, String localRouter, String neighborId) {
354 LinkInformation linkInformation = new DefaultIsisLinkInformation();
355 IsisRouter isisRouter = isisRouterDetails.get(neighborId);
356 for (IsisTlv isisTlv : lsPdu.tlvs()) {
357 if (isisTlv instanceof IsExtendedReachability) {
358 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
359 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
360 for (NeighborForExtendedIs teTlv : neighbours) {
361 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
362 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
363 if (teSubTlv instanceof InterfaceIpAddress) {
364 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
365 linkInformation.setInterfaceIp(localIpAddress.localInterfaceIPAddress());
366 } else if (teSubTlv instanceof NeighborIpAddress) {
367 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
368 linkInformation.setNeighborIp(neighborIpAddress.neighborIPAddress());
369 }
370
371 }
372 }
373 }
374 }
375 linkInformation.setLinkId(linkId);
376 linkInformation.setAlreadyCreated(false);
377 linkInformation.setLinkDestinationId(neighborId);
378 linkInformation.setLinkSourceId(localRouter);
379 return linkInformation;
380 }
381
382 /**
383 * Creates ISIS router instance.
384 *
385 * @param lsPdu lsp instance
386 * @return isisRouter instance
387 */
388 private IsisRouter createIsisRouter(LsPdu lsPdu) {
389 IsisRouter isisRouter = new DefaultIsisRouter();
390 if (IsisUtil.checkIsDis(lsPdu.lspId())) {
391 isisRouter.setDis(true);
392 } else {
393 isisRouter.setDis(false);
394 }
395 isisRouter.setSystemId(IsisUtil.removeTailingZeros(lsPdu.lspId()));
396 for (IsisTlv isisTlv : lsPdu.tlvs()) {
397 if (isisTlv instanceof IsExtendedReachability) {
398 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
399 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
400 for (NeighborForExtendedIs teTlv : neighbours) {
401 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
402 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
403 if (teSubTlv instanceof InterfaceIpAddress) {
404 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
405 isisRouter.setInterfaceId(localIpAddress.localInterfaceIPAddress());
406 } else if (teSubTlv instanceof NeighborIpAddress) {
407 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
408 isisRouter.setNeighborRouterId(neighborIpAddress.neighborIPAddress());
409 }
410
411 }
412 }
413 }
414 }
415 return isisRouter;
416 }
417
418 /**
419 * Creates ISIS router instance.
420 *
421 * @param systemId system ID
422 * @return isisRouter instance
423 */
424 private IsisRouter createIsisRouterDummy(String systemId) {
425 IsisRouter isisRouter = new DefaultIsisRouter();
426 isisRouter.setSystemId(systemId);
427 isisRouter.setDis(false);
428 isisRouter.setInterfaceId(IsisConstants.DEFAULTIP);
429 isisRouter.setNeighborRouterId(IsisConstants.DEFAULTIP);
430
431 return isisRouter;
432 }
433
434 /**
435 * Creates the ISIS link TED information.
436 *
437 * @param lsPdu link state PDU
438 * @return isisLinkTed
439 */
440 public IsisLinkTed createIsisLinkTedInfo(LsPdu lsPdu) {
441 IsisLinkTed isisLinkTed = new DefaultIsisLinkTed();
442 for (IsisTlv isisTlv : lsPdu.tlvs()) {
443 if (isisTlv instanceof IsExtendedReachability) {
444 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
445 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
446 for (NeighborForExtendedIs teTlv : neighbours) {
447 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
448 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
449 if (teSubTlv instanceof AdministrativeGroup) {
450 AdministrativeGroup ag = (AdministrativeGroup) teSubTlv;
451 isisLinkTed.setAdministrativeGroup(ag.administrativeGroup());
452 }
453 if (teSubTlv instanceof InterfaceIpAddress) {
454 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
455 isisLinkTed.setIpv4InterfaceAddress(localIpAddress.localInterfaceIPAddress());
456 }
457 if (teSubTlv instanceof NeighborIpAddress) {
458 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
459 isisLinkTed.setIpv4NeighborAddress(neighborIpAddress.neighborIPAddress());
460 }
461 if (teSubTlv instanceof TrafficEngineeringMetric) {
462 TrafficEngineeringMetric teM = (TrafficEngineeringMetric) teSubTlv;
463 isisLinkTed.setTeDefaultMetric(teM.getTrafficEngineeringMetricValue());
464 }
465 if (teSubTlv instanceof MaximumBandwidth) {
466 MaximumBandwidth maxLinkBandwidth = (MaximumBandwidth) teSubTlv;
467 isisLinkTed.setMaximumLinkBandwidth(
468 Bandwidth.bps(maxLinkBandwidth.getMaximumBandwidthValue()));
469 }
470 if (teSubTlv instanceof MaximumReservableBandwidth) {
471 MaximumReservableBandwidth maxReservableBw = (MaximumReservableBandwidth) teSubTlv;
472 isisLinkTed.setMaximumReservableLinkBandwidth(
473 Bandwidth.bps(maxReservableBw.getMaximumBandwidthValue()));
474 }
475 if (teSubTlv instanceof UnreservedBandwidth) {
476 UnreservedBandwidth unReservedBandwidth = (UnreservedBandwidth) teSubTlv;
477 List<Bandwidth> bandwidthList = new ArrayList<>();
478 List<Float> unReservedBandwidthList = unReservedBandwidth.unReservedBandwidthValue();
479 for (Float unReservedBandwidthFloatValue : unReservedBandwidthList) {
480 Bandwidth bandwidth = Bandwidth.bps(unReservedBandwidthFloatValue);
481 bandwidthList.add(bandwidth);
482 }
483 isisLinkTed.setUnreservedBandwidth(bandwidthList);
484 }
485 }
486 }
487 }
488 }
489 return isisLinkTed;
490 }
Ray Milkey88cc3432017-03-30 17:19:08 -0700491}