blob: 3e5fb6879b7021460bd8fb076e757da77cfb6c97 [file] [log] [blame]
mohamedrahil00f6f262016-11-24 20:20:41 +05301/*
2 * Copyright 2016-present 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.bgp.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.bgp.controller.BgpController;
21import org.onosproject.bgp.controller.BgpLocalRib;
22import org.onosproject.bgpio.protocol.BgpLSNlri;
23
24import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
25import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
26import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
27import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
28import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
29import org.onosproject.bgpio.protocol.linkstate.BgpPrefixLSIdentifier;
30import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
31import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
32import org.onosproject.bgpio.types.IPv4AddressTlv;
33import org.onosproject.bgpio.types.RouteDistinguisher;
34import org.onosproject.bgpio.types.BgpValueType;
35import org.onosproject.bgpio.types.LinkStateAttributes;
36import org.onosproject.bgpio.types.MpReachNlri;
37import org.onosproject.bgpio.types.AutonomousSystemTlv;
38import org.onosproject.bgpio.types.IsIsNonPseudonode;
39import org.onosproject.bgpio.types.LocalPref;
40import org.onosproject.bgpio.types.Origin;
41import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
42import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
43import org.onosproject.bgpio.types.attr.BgpLinkAttrUnRsrvdLinkBandwidth;
44import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
45import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
46import org.onosproject.cli.AbstractShellCommand;
47import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.Map;
51import java.util.Set;
52import java.util.List;
53import java.util.Iterator;
54import java.util.Arrays;
55
56
57
58@Command(scope = "onos", name = "bgp-rib", description = "lists RIB configuration")
59public class BgpLocalRibDisplay extends AbstractShellCommand {
60 private static final Logger log = LoggerFactory.getLogger(BgpLocalRibDisplay.class);
61 private static final String NODETREE = "nodes";
62 private static final String LINKTREE = "links";
63 private static final String PREFIXTREE = "prefix";
64 private static final String VPNNODETREE = "vpnnodes";
65 private static final String VPNLINKTREE = "vpnlinkS";
66 private static final String VPNPREFIXTREE = "vpnprefix";
67 protected Origin origin;
68 protected LocalPref localPref;
69 protected BgpAttrRouterIdV4 bgpAttrRouterIdV4;
70 protected IsIsNonPseudonode isIsNonPseudonode;
71 protected AutonomousSystemTlv autonomousSystemTlv;
72 protected IPv4AddressTlv iPv4AddressTlv;
73 protected BgpLinkAttrMaxLinkBandwidth bgpLinkAttrMaxLinkBandwidth;
74 protected BgpLinkAttrUnRsrvdLinkBandwidth bgpLinkAttrUnRsrvdLinkBandwidth;
75 protected BgpLinkAttrTeDefaultMetric bgpLinkAttrTeDefaultMetric;
76 protected BgpLinkAttrIgpMetric bgpLinkAttrIgpMetric;
77 protected PathAttrNlriDetailsLocalRib pathAttrNlriDetailsLocalRib;
78 protected MpReachNlri mpReachNlri;
79 protected PathAttrNlriDetails pathAttrNlriDetails;
80 protected BgpNodeLSNlriVer4.ProtocolType protocolType;
81 protected BgpController bgpController = get(BgpController.class);
82 protected BgpLocalRib bgpLocalRib = bgpController.bgpLocalRib();
83 Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib> nodeTreeMap = bgpLocalRib.nodeTree();
84 Set<BgpNodeLSIdentifier> nodekeySet = nodeTreeMap.keySet();
85 Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib> linkTreeMap = bgpLocalRib.linkTree();
86 Set<BgpLinkLSIdentifier> linkkeySet = linkTreeMap.keySet();
87 @Argument(index = 0, name = "name",
88 description = "nodetree" + "\n" + "linktree" + "\n" + "prefixtree" + "\n" + "vpnnodetree" + "\n" +
89 "vpnlinktree" + "\n" + "vpnprefixtree", required = true, multiValued = false)
90 String name = null;
91 @Argument(index = 1, name = "numberofentries",
92 description = "numberofentries", required = false, multiValued = false)
93 int numberofentries;
94 @Argument(index = 2, name = "vpnId",
95 description = "vpnId", required = false, multiValued = false)
96 String vpnId = null;
97 private int count = 0;
98
99 @Override
100 protected void execute() {
101 switch (name) {
102 case NODETREE:
103 displayNodes();
104 break;
105 case LINKTREE:
106 displayLinks();
107 break;
108 case PREFIXTREE:
109 displayPrefix();
110 break;
111 case VPNNODETREE:
112 displayVpnNodes();
113 break;
114 case VPNLINKTREE:
115 displayVpnLinks();
116 break;
117 case VPNPREFIXTREE:
118 displayVpnPrefix();
119 break;
120 default:
121 System.out.print("Unknown Command");
122 break;
123 }
124
125 }
126
127 private void displayNodes() {
128 try {
129 int counter = 0;
130 print("Total number of entries = %s", nodeTreeMap.size());
131 for (BgpNodeLSIdentifier nodes : nodekeySet) {
132 if (numberofentries > nodeTreeMap.size() || numberofentries < 0) {
133 System.out.print("Wrong Argument");
134 break;
135 } else if (counter < numberofentries) {
136 pathAttrNlriDetailsLocalRib = nodeTreeMap.get(nodes);
137 displayNode();
138 counter++;
139 } else if (counter == 0) {
140 pathAttrNlriDetailsLocalRib = nodeTreeMap.get(nodes);
141 displayNode();
142
143
144 }
145
146 }
147 } catch (Exception e) {
148 log.debug("Error occurred while displaying BGP nodes: {}", e.getMessage());
149 }
150
151 }
152
153
154 private void displayLinks() {
155 try {
156 int counter = 0;
157 print("Total Number of entries = %d", linkTreeMap.size());
158 for (BgpLinkLSIdentifier links : linkkeySet) {
159 if (numberofentries > linkTreeMap.size() || numberofentries < 0) {
160 System.out.print("Wrong Argument");
161 break;
162 } else if (counter < numberofentries) {
163 pathAttrNlriDetailsLocalRib = linkTreeMap.get(links);
164 print("Total number of entries = %d", linkTreeMap.size());
165 displayLink();
166 counter++;
167 } else if (counter == 0) {
168 pathAttrNlriDetailsLocalRib = linkTreeMap.get(links);
169 displayLink();
170 }
171
172 }
173 } catch (Exception e) {
174 log.debug("Error occurred while displaying BGP links: {}", e.getMessage());
175 }
176 }
177
178 private void displayPrefix() {
179 try {
180 this.bgpController = get(BgpController.class);
181 bgpLocalRib = bgpController.bgpLocalRib();
182 Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib> prefixmap = bgpLocalRib.prefixTree();
183 Set<BgpPrefixLSIdentifier> prefixkeySet = prefixmap.keySet();
184 for (BgpPrefixLSIdentifier prefix : prefixkeySet) {
185 pathAttrNlriDetailsLocalRib = prefixmap.get(prefix);
186 pathAttrNlriDetails = pathAttrNlriDetailsLocalRib.localRibNlridetails();
187 print("No of entries = %d", prefixmap.size());
188 System.out.print(pathAttrNlriDetailsLocalRib.toString());
189
190 }
191
192
193 } catch (Exception e) {
194 log.debug("Error occurred while displaying BGP prefixes: {}", e.getMessage());
195
196 }
197 }
198
199 private void displayVpnNodes() {
200 try {
201 this.bgpController = get(BgpController.class);
202 bgpLocalRib = bgpController.bgpLocalRib();
203 Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnNode =
204 bgpLocalRib.vpnNodeTree();
205 Set<RouteDistinguisher> vpnNodekeySet = vpnNode.keySet();
206 for (RouteDistinguisher vpnNodes : vpnNodekeySet) {
207 boolean invalidProcess = true;
208 if (vpnId != null && vpnId.trim().equals(vpnNodes.hashCode())) {
209 invalidProcess = false;
210 displayNodes();
211 }
212 if (invalidProcess) {
213 print("%s\n", "Id " + vpnId + "does not exist...!!!");
214 }
215 }
216 } catch (Exception e) {
217 log.debug("Error occurred while displaying BGP nodes based on VPN : {}", e.getMessage());
218 }
219
220 }
221
222 private void displayVpnLinks() {
223 try {
224 this.bgpController = get(BgpController.class);
225 bgpLocalRib = bgpController.bgpLocalRib();
226 Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnLink =
227 bgpLocalRib.vpnLinkTree();
228 Set<RouteDistinguisher> vpnLinkkeySet = vpnLink.keySet();
229 for (RouteDistinguisher vpnLinks : vpnLinkkeySet) {
230 boolean invalidProcess = true;
231 if (vpnId != null && vpnId.trim().equals(vpnLinks.hashCode())) {
232 invalidProcess = false;
233 displayLinks();
234 }
235 if (invalidProcess) {
236 print("%s\n", "Id " + vpnId + "does not exist...!!!");
237 }
238 }
239 } catch (Exception e) {
240 log.debug("Error occurred while displaying BGP links based on VPN : {}", e.getMessage());
241 }
242 }
243
244 private void displayVpnPrefix() {
245 try {
246 this.bgpController = get(BgpController.class);
247 bgpLocalRib = bgpController.bgpLocalRib();
248 Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnPrefix =
249 bgpLocalRib.vpnPrefixTree();
250 Set<RouteDistinguisher> vpnPrefixkeySet = vpnPrefix.keySet();
251 for (RouteDistinguisher vpnprefixId : vpnPrefixkeySet) {
252 boolean invalidProcess = true;
253 if (vpnId != null && vpnId.trim().equals(vpnprefixId.hashCode())) {
254 invalidProcess = false;
255 displayPrefix();
256 }
257 if (invalidProcess) {
258 print("%s\n", "Id " + vpnId + "does not exist...!!!");
259 }
260 }
261 } catch (Exception e) {
262 log.debug("Error occurred while displaying BGP prefixes based on VPN : {}", e.getMessage());
263 }
264 }
265
266 private void displayNode() {
267
268
269 pathAttrNlriDetails = pathAttrNlriDetailsLocalRib.localRibNlridetails();
270 List<BgpValueType> bgpValueTypeList = pathAttrNlriDetails.pathAttributes();
271 protocolType = pathAttrNlriDetails.protocolID();
272 Iterator<BgpValueType> itrBgpValueType = bgpValueTypeList.iterator();
273 while (itrBgpValueType.hasNext()) {
274 BgpValueType bgpValueType = itrBgpValueType.next();
275 if (bgpValueType instanceof Origin) {
276 origin = (Origin) bgpValueType;
277 } else if (bgpValueType instanceof LocalPref) {
278 localPref = (LocalPref) bgpValueType;
279 } else if (bgpValueType instanceof LinkStateAttributes) {
280 LinkStateAttributes linkStateAttributes = (LinkStateAttributes) bgpValueType;
281 List linkStateAttribiuteList = linkStateAttributes.linkStateAttributes();
282 Iterator<BgpValueType> linkStateAttribiteIterator = linkStateAttribiuteList.iterator();
283 while (linkStateAttribiteIterator.hasNext()) {
284 BgpValueType bgpValueType1 = linkStateAttribiteIterator.next();
285 if (bgpValueType1 instanceof BgpAttrRouterIdV4) {
286 bgpAttrRouterIdV4 = (BgpAttrRouterIdV4) bgpValueType1;
287 }
288 }
289 } else if (bgpValueType instanceof MpReachNlri) {
290 mpReachNlri = (MpReachNlri) bgpValueType;
291 List<BgpLSNlri> bgpLSNlris = mpReachNlri.mpReachNlri();
292 Iterator<BgpLSNlri> bgpLsnlrisIterator = bgpLSNlris.iterator();
293 while (bgpLsnlrisIterator.hasNext()) {
294 BgpLSNlri bgpLSNlri = bgpLsnlrisIterator.next();
295 if (bgpLSNlri instanceof BgpNodeLSNlriVer4) {
296 BgpNodeLSNlriVer4 bgpNodeLSNlriVer4 = (BgpNodeLSNlriVer4) bgpLSNlri;
297 BgpNodeLSIdentifier bgpNodeLSIdentifier = bgpNodeLSNlriVer4.getLocalNodeDescriptors();
298 NodeDescriptors nodeDescriptors = bgpNodeLSIdentifier.getNodedescriptors();
299 List<BgpValueType> bgpvalueTypesList = nodeDescriptors.getSubTlvs();
300 Iterator<BgpValueType> bgpValueTypeIterator = bgpvalueTypesList.iterator();
301 while (bgpValueTypeIterator.hasNext()) {
302 BgpValueType valueType = bgpValueTypeIterator.next();
303 if (valueType instanceof IsIsNonPseudonode) {
304 isIsNonPseudonode = (IsIsNonPseudonode) valueType;
305
306 }
307 }
308 }
309 }
310 }
311 }
312 print("RibAsNumber = %s,PeerIdentifier = %s,RibIpAddress = %s,ProtocolType = %s,Origin = %s,LocalPref = %s," +
313 "RouterID = %s,IsoNodeID = %s,NextHop = %s", pathAttrNlriDetailsLocalRib.localRibAsNum(),
314 pathAttrNlriDetailsLocalRib.localRibIdentifier(), pathAttrNlriDetailsLocalRib.localRibIpAddress(),
315 protocolType.toString(), origin.origin(), localPref.localPref(), bgpAttrRouterIdV4.attrRouterId(),
316 Arrays.toString(isIsNonPseudonode.getIsoNodeId()), mpReachNlri.nexthop4());
317 }
318
319 private void displayLink() {
320
321 pathAttrNlriDetails = pathAttrNlriDetailsLocalRib.localRibNlridetails();
322 List<BgpValueType> valueTypes = pathAttrNlriDetails.pathAttributes();
323 Iterator<BgpValueType> itrBgpValueType = valueTypes.iterator();
324 while (itrBgpValueType.hasNext()) {
325 BgpValueType bgpValueType = itrBgpValueType.next();
326 if (bgpValueType instanceof Origin) {
327 origin = (Origin) bgpValueType;
328 } else if (bgpValueType instanceof LocalPref) {
329 localPref = (LocalPref) bgpValueType;
330 } else if (bgpValueType instanceof LinkStateAttributes) {
331 LinkStateAttributes linkStateAttributes = (LinkStateAttributes) bgpValueType;
332 List linkStateAttributelist = linkStateAttributes.linkStateAttributes();
333 Iterator<BgpValueType> linkStateAttributeIterator = linkStateAttributelist.iterator();
334 while (linkStateAttributeIterator.hasNext()) {
335 BgpValueType bgpValueType1 = linkStateAttributeIterator.next();
336 if (bgpValueType1 instanceof BgpAttrRouterIdV4) {
337 bgpAttrRouterIdV4 = (BgpAttrRouterIdV4) bgpValueType1;
338 } else if (bgpValueType1 instanceof BgpLinkAttrMaxLinkBandwidth) {
339 bgpLinkAttrMaxLinkBandwidth = (BgpLinkAttrMaxLinkBandwidth) bgpValueType1;
340 } else if (bgpValueType1 instanceof BgpLinkAttrUnRsrvdLinkBandwidth) {
341 bgpLinkAttrUnRsrvdLinkBandwidth = (BgpLinkAttrUnRsrvdLinkBandwidth) bgpValueType1;
342 } else if (bgpValueType1 instanceof BgpLinkAttrTeDefaultMetric) {
343 bgpLinkAttrTeDefaultMetric = (BgpLinkAttrTeDefaultMetric) bgpValueType1;
344 } else if (bgpValueType1 instanceof BgpLinkAttrIgpMetric) {
345 bgpLinkAttrIgpMetric = (BgpLinkAttrIgpMetric) bgpValueType1;
346 }
347
348 }
349 } else if (bgpValueType instanceof MpReachNlri) {
350 mpReachNlri = (MpReachNlri) bgpValueType;
351 List<BgpLSNlri> bgpLSNlris = mpReachNlri.mpReachNlri();
352 Iterator<BgpLSNlri> bgpLsnlrisIterator = bgpLSNlris.iterator();
353 while (bgpLsnlrisIterator.hasNext()) {
354 BgpLSNlri bgpLSNlri = bgpLsnlrisIterator.next();
355 if (bgpLSNlri instanceof BgpLinkLsNlriVer4) {
356 BgpLinkLsNlriVer4 bgpLinkLsNlriVer4 = (BgpLinkLsNlriVer4) bgpLSNlri;
357 BgpLinkLSIdentifier bgpLinkLSIdentifier = bgpLinkLsNlriVer4.getLinkIdentifier();
358 NodeDescriptors localnodeDescriptors = bgpLinkLSIdentifier.localNodeDescriptors();
359 NodeDescriptors remotenodeDescriptors = bgpLinkLSIdentifier.remoteNodeDescriptors();
360 List<BgpValueType> linkDescriptors = bgpLinkLSIdentifier.linkDescriptors();
361 List<BgpValueType> subTlvList = localnodeDescriptors.getSubTlvs();
362 Iterator<BgpValueType> subTlvIterator = subTlvList.iterator();
363 while (subTlvIterator.hasNext()) {
364 BgpValueType valueType = subTlvIterator.next();
365 if (valueType instanceof IsIsNonPseudonode) {
366 isIsNonPseudonode = (IsIsNonPseudonode) valueType;
367 } else if (valueType instanceof AutonomousSystemTlv) {
368 autonomousSystemTlv = (AutonomousSystemTlv) valueType;
369 }
370 }
371 List<BgpValueType> remotevalueTypes = remotenodeDescriptors.getSubTlvs();
372 Iterator<BgpValueType> remoteValueTypesIterator = remotevalueTypes.iterator();
373 while (remoteValueTypesIterator.hasNext()) {
374 BgpValueType valueType = remoteValueTypesIterator.next();
375 if (valueType instanceof IsIsNonPseudonode) {
376 isIsNonPseudonode = (IsIsNonPseudonode) valueType;
377 } else if (valueType instanceof AutonomousSystemTlv) {
378 autonomousSystemTlv = (AutonomousSystemTlv) valueType;
379 }
380 }
381 Iterator<BgpValueType> listIterator = linkDescriptors.iterator();
382 while (listIterator.hasNext()) {
383 BgpValueType valueType = listIterator.next();
384 if (valueType instanceof IPv4AddressTlv) {
385 iPv4AddressTlv = (IPv4AddressTlv) valueType;
386 }
387
388 }
389 }
390 }
391 }
392 }
393 print("PeerIdentifier = %s,Origin = %s,LocalPref = %s,RouterID = %s,MaxBandwidth = %s," +
394 "UnreservedBandwidth = %s,DefaultMetric = %s,IGPMetric = %s,IsoNodeID = %s,ASNum = %s," +
395 "IPAddress = %s,NextHop = %s", pathAttrNlriDetailsLocalRib.localRibIdentifier(),
396 origin.origin(), localPref.localPref(), bgpAttrRouterIdV4.attrRouterId(),
397 bgpLinkAttrMaxLinkBandwidth.linkAttrMaxLinkBandwidth(),
398 bgpLinkAttrUnRsrvdLinkBandwidth.getLinkAttrUnRsrvdLinkBandwidth().toString(),
399 bgpLinkAttrTeDefaultMetric.attrLinkDefTeMetric(), bgpLinkAttrIgpMetric.attrLinkIgpMetric(),
400 Arrays.toString(isIsNonPseudonode.getIsoNodeId()), autonomousSystemTlv.getAsNum(),
401 iPv4AddressTlv.address(), mpReachNlri.nexthop4().toString(),
402 pathAttrNlriDetailsLocalRib.localRibIpAddress(), origin.origin(), localPref.localPref(),
403 bgpAttrRouterIdV4.attrRouterId());
404
405 }
406}