blob: 260cfe603060592be6c8a4119140050ec1de75c9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -070016package org.onlab.onos.sdnip.cli;
17
18import org.apache.karaf.shell.commands.Command;
19import org.onlab.onos.cli.AbstractShellCommand;
20import org.onlab.onos.sdnip.SdnIpService;
21import org.onlab.onos.sdnip.bgp.BgpConstants;
22import org.onlab.onos.sdnip.bgp.BgpRouteEntry;
23
24/**
25 * Command to show the routes learned through BGP.
26 */
27@Command(scope = "onos", name = "bgp-routes",
28 description = "Lists all routes received from BGP")
29public class BgpRoutesListCommand extends AbstractShellCommand {
30
31 private static final String FORMAT =
32 "prefix=%s, nexthop=%s, origin=%s, localpref=%s, med=%s, aspath=%s, bgpid=%s";
33
34 @Override
35 protected void execute() {
36 SdnIpService service = get(SdnIpService.class);
37
38 for (BgpRouteEntry route : service.getBgpRoutes()) {
39 printRoute(route);
40 }
41 }
42
43 private void printRoute(BgpRouteEntry route) {
44 if (route != null) {
45 print(FORMAT, route.prefix(), route.nextHop(),
46 originToString(route.getOrigin()), route.getLocalPref(),
47 route.getMultiExitDisc(), route.getAsPath(),
48 route.getBgpSession().getRemoteBgpId());
49 }
50 }
51
52 private static String originToString(int origin) {
53 String originString = "UNKNOWN";
54
55 switch (origin) {
56 case BgpConstants.Update.Origin.IGP:
57 originString = "IGP";
58 break;
59 case BgpConstants.Update.Origin.EGP:
60 originString = "EGP";
61 break;
62 case BgpConstants.Update.Origin.INCOMPLETE:
63 originString = "INCOMPLETE";
64 break;
65 default:
66 break;
67 }
68
69 return originString;
70 }
71
72}