blob: 8682a4accc3c71a61bf191c4367da345693ded8c [file] [log] [blame]
sunish vk7bdf4d42016-06-24 12:29:43 +05301/*
2* Copyright 2016 Open Networking Laboratory
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*/
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;
276 }/*
277
278 *//**
279 * Removes Device and Link instance.
280 *
281 * @param lsPdu ISIS LSPDU instance
282 * @return isisRouter isisRouter instance
283 *//*
284 public IsisRouter removeDeviceAndLinkInfo(LsPdu lsPdu) {
285 IsisRouter isisRouter = createIsisRouter(lsPdu);
286 return isisRouter;
287 }*/
288
289 /**
290 * Creates link information.
291 *
292 * @param lsPdu ls pdu instance
293 * @param ownSystemId system ID
294 * @return link information
295 */
296 public Map<String, LinkInformation> createLinkInfo(LsPdu lsPdu, String ownSystemId) {
297 for (IsisTlv isisTlv : lsPdu.tlvs()) {
298 if (isisTlv instanceof IsExtendedReachability) {
299 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
300 List<NeighborForExtendedIs> neighborForExtendedIsList = isExtendedReachability.neighbours();
301 for (NeighborForExtendedIs neighbor : neighborForExtendedIsList) {
302 String neighbourId = neighbor.neighborId();
303 String routerId = IsisUtil.removeTailingZeros(lsPdu.lspId());
304 if (!(neighbourId.equals(ownSystemId))) {
305 IsisRouter isisRouter = isisRouterDetails.get(neighbourId);
306 if (isisRouter != null) {
307 String linkId = "link:" + routerId + "-" + neighbourId;
308 addedLinkInformationMap.put(linkId, createLinkInformation(lsPdu, linkId,
309 routerId, neighbourId));
310 } else {
311 createIsisRouterDummy(neighbourId);
312 String linkId = "link:" + routerId + "-" + neighbourId;
313 LinkInformation linkInformation = createLinkInformation(lsPdu, linkId,
314 routerId, neighbourId);
315 linkInformation.setAlreadyCreated(true);
316 addedLinkInformationMap.put(linkId, linkInformation);
317 }
318 }
319
320 }
321 }
322 }
323 return addedLinkInformationMap;
324 }
325
326 /**
327 * Removes link information.
328 *
329 * @param systemId system ID to remove link information
330 * @return updated link information
331 */
332 public Map<String, LinkInformation> removeLinkInfo(String systemId) {
333 String routerId = IsisUtil.removeTailingZeros(systemId);
334 Map<String, LinkInformation> removeLinkInformationMap = new LinkedHashMap<>();
335 for (String key : addedLinkInformationMap.keySet()) {
336 if (key.contains(routerId)) {
337 removeLinkInformationMap.put(key, addedLinkInformationMap.get(key));
338 }
339 }
340 return removeLinkInformationMap;
341 }
342
343 /**
344 * Creates link information.
345 *
346 * @param lsPdu link state pdu
347 * @param linkId link id
348 * @param localRouter local router system id
349 * @param neighborId destination router system id
350 * @return linkInformation instance
351 */
352 private LinkInformation createLinkInformation(LsPdu lsPdu, String linkId, String localRouter, String neighborId) {
353 LinkInformation linkInformation = new DefaultIsisLinkInformation();
354 IsisRouter isisRouter = isisRouterDetails.get(neighborId);
355 for (IsisTlv isisTlv : lsPdu.tlvs()) {
356 if (isisTlv instanceof IsExtendedReachability) {
357 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
358 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
359 for (NeighborForExtendedIs teTlv : neighbours) {
360 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
361 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
362 if (teSubTlv instanceof InterfaceIpAddress) {
363 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
364 linkInformation.setInterfaceIp(localIpAddress.localInterfaceIPAddress());
365 } else if (teSubTlv instanceof NeighborIpAddress) {
366 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
367 linkInformation.setNeighborIp(neighborIpAddress.neighborIPAddress());
368 }
369
370 }
371 }
372 }
373 }
374 linkInformation.setLinkId(linkId);
375 linkInformation.setAlreadyCreated(false);
376 linkInformation.setLinkDestinationId(neighborId);
377 linkInformation.setLinkSourceId(localRouter);
378 return linkInformation;
379 }
380
381 /**
382 * Creates ISIS router instance.
383 *
384 * @param lsPdu lsp instance
385 * @return isisRouter instance
386 */
387 private IsisRouter createIsisRouter(LsPdu lsPdu) {
388 IsisRouter isisRouter = new DefaultIsisRouter();
389 if (IsisUtil.checkIsDis(lsPdu.lspId())) {
390 isisRouter.setDis(true);
391 } else {
392 isisRouter.setDis(false);
393 }
394 isisRouter.setSystemId(IsisUtil.removeTailingZeros(lsPdu.lspId()));
395 for (IsisTlv isisTlv : lsPdu.tlvs()) {
396 if (isisTlv instanceof IsExtendedReachability) {
397 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
398 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
399 for (NeighborForExtendedIs teTlv : neighbours) {
400 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
401 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
402 if (teSubTlv instanceof InterfaceIpAddress) {
403 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
404 isisRouter.setInterfaceId(localIpAddress.localInterfaceIPAddress());
405 } else if (teSubTlv instanceof NeighborIpAddress) {
406 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
407 isisRouter.setNeighborRouterId(neighborIpAddress.neighborIPAddress());
408 }
409
410 }
411 }
412 }
413 }
414 return isisRouter;
415 }
416
417 /**
418 * Creates ISIS router instance.
419 *
420 * @param systemId system ID
421 * @return isisRouter instance
422 */
423 private IsisRouter createIsisRouterDummy(String systemId) {
424 IsisRouter isisRouter = new DefaultIsisRouter();
425 isisRouter.setSystemId(systemId);
426 isisRouter.setDis(false);
427 isisRouter.setInterfaceId(IsisConstants.DEFAULTIP);
428 isisRouter.setNeighborRouterId(IsisConstants.DEFAULTIP);
429
430 return isisRouter;
431 }
432
433 /**
434 * Creates the ISIS link TED information.
435 *
436 * @param lsPdu link state PDU
437 * @return isisLinkTed
438 */
439 public IsisLinkTed createIsisLinkTedInfo(LsPdu lsPdu) {
440 IsisLinkTed isisLinkTed = new DefaultIsisLinkTed();
441 for (IsisTlv isisTlv : lsPdu.tlvs()) {
442 if (isisTlv instanceof IsExtendedReachability) {
443 IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv;
444 List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours();
445 for (NeighborForExtendedIs teTlv : neighbours) {
446 List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv();
447 for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) {
448 if (teSubTlv instanceof AdministrativeGroup) {
449 AdministrativeGroup ag = (AdministrativeGroup) teSubTlv;
450 isisLinkTed.setAdministrativeGroup(ag.administrativeGroup());
451 }
452 if (teSubTlv instanceof InterfaceIpAddress) {
453 InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv;
454 isisLinkTed.setIpv4InterfaceAddress(localIpAddress.localInterfaceIPAddress());
455 }
456 if (teSubTlv instanceof NeighborIpAddress) {
457 NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv;
458 isisLinkTed.setIpv4NeighborAddress(neighborIpAddress.neighborIPAddress());
459 }
460 if (teSubTlv instanceof TrafficEngineeringMetric) {
461 TrafficEngineeringMetric teM = (TrafficEngineeringMetric) teSubTlv;
462 isisLinkTed.setTeDefaultMetric(teM.getTrafficEngineeringMetricValue());
463 }
464 if (teSubTlv instanceof MaximumBandwidth) {
465 MaximumBandwidth maxLinkBandwidth = (MaximumBandwidth) teSubTlv;
466 isisLinkTed.setMaximumLinkBandwidth(
467 Bandwidth.bps(maxLinkBandwidth.getMaximumBandwidthValue()));
468 }
469 if (teSubTlv instanceof MaximumReservableBandwidth) {
470 MaximumReservableBandwidth maxReservableBw = (MaximumReservableBandwidth) teSubTlv;
471 isisLinkTed.setMaximumReservableLinkBandwidth(
472 Bandwidth.bps(maxReservableBw.getMaximumBandwidthValue()));
473 }
474 if (teSubTlv instanceof UnreservedBandwidth) {
475 UnreservedBandwidth unReservedBandwidth = (UnreservedBandwidth) teSubTlv;
476 List<Bandwidth> bandwidthList = new ArrayList<>();
477 List<Float> unReservedBandwidthList = unReservedBandwidth.unReservedBandwidthValue();
478 for (Float unReservedBandwidthFloatValue : unReservedBandwidthList) {
479 Bandwidth bandwidth = Bandwidth.bps(unReservedBandwidthFloatValue);
480 bandwidthList.add(bandwidth);
481 }
482 isisLinkTed.setUnreservedBandwidth(bandwidthList);
483 }
484 }
485 }
486 }
487 }
488 return isisLinkTed;
489 }
490}