blob: c4915bb88c7c6711839ee45473b29976c9fc9c99 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -070019package org.onlab.onos.sdnip.cli;
20
21import org.apache.karaf.shell.commands.Command;
22import org.onlab.onos.cli.AbstractShellCommand;
23import org.onlab.onos.sdnip.SdnIpService;
24import org.onlab.onos.sdnip.bgp.BgpConstants;
25import org.onlab.onos.sdnip.bgp.BgpRouteEntry;
26
27/**
28 * Command to show the routes learned through BGP.
29 */
30@Command(scope = "onos", name = "bgp-routes",
31 description = "Lists all routes received from BGP")
32public class BgpRoutesListCommand extends AbstractShellCommand {
33
34 private static final String FORMAT =
35 "prefix=%s, nexthop=%s, origin=%s, localpref=%s, med=%s, aspath=%s, bgpid=%s";
36
37 @Override
38 protected void execute() {
39 SdnIpService service = get(SdnIpService.class);
40
41 for (BgpRouteEntry route : service.getBgpRoutes()) {
42 printRoute(route);
43 }
44 }
45
46 private void printRoute(BgpRouteEntry route) {
47 if (route != null) {
48 print(FORMAT, route.prefix(), route.nextHop(),
49 originToString(route.getOrigin()), route.getLocalPref(),
50 route.getMultiExitDisc(), route.getAsPath(),
51 route.getBgpSession().getRemoteBgpId());
52 }
53 }
54
55 private static String originToString(int origin) {
56 String originString = "UNKNOWN";
57
58 switch (origin) {
59 case BgpConstants.Update.Origin.IGP:
60 originString = "IGP";
61 break;
62 case BgpConstants.Update.Origin.EGP:
63 originString = "EGP";
64 break;
65 case BgpConstants.Update.Origin.INCOMPLETE:
66 originString = "INCOMPLETE";
67 break;
68 default:
69 break;
70 }
71
72 return originString;
73 }
74
75}