Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 1 | package org.onlab.onos.sdnip.cli; |
| 2 | |
| 3 | import org.apache.karaf.shell.commands.Command; |
| 4 | import org.onlab.onos.cli.AbstractShellCommand; |
| 5 | import org.onlab.onos.sdnip.SdnIpService; |
| 6 | import org.onlab.onos.sdnip.bgp.BgpConstants; |
| 7 | import org.onlab.onos.sdnip.bgp.BgpRouteEntry; |
| 8 | |
| 9 | /** |
| 10 | * Command to show the routes learned through BGP. |
| 11 | */ |
| 12 | @Command(scope = "onos", name = "bgp-routes", |
| 13 | description = "Lists all routes received from BGP") |
| 14 | public class BgpRoutesListCommand extends AbstractShellCommand { |
| 15 | |
| 16 | private static final String FORMAT = |
| 17 | "prefix=%s, nexthop=%s, origin=%s, localpref=%s, med=%s, aspath=%s, bgpid=%s"; |
| 18 | |
| 19 | @Override |
| 20 | protected void execute() { |
| 21 | SdnIpService service = get(SdnIpService.class); |
| 22 | |
| 23 | for (BgpRouteEntry route : service.getBgpRoutes()) { |
| 24 | printRoute(route); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | private void printRoute(BgpRouteEntry route) { |
| 29 | if (route != null) { |
| 30 | print(FORMAT, route.prefix(), route.nextHop(), |
| 31 | originToString(route.getOrigin()), route.getLocalPref(), |
| 32 | route.getMultiExitDisc(), route.getAsPath(), |
| 33 | route.getBgpSession().getRemoteBgpId()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private static String originToString(int origin) { |
| 38 | String originString = "UNKNOWN"; |
| 39 | |
| 40 | switch (origin) { |
| 41 | case BgpConstants.Update.Origin.IGP: |
| 42 | originString = "IGP"; |
| 43 | break; |
| 44 | case BgpConstants.Update.Origin.EGP: |
| 45 | originString = "EGP"; |
| 46 | break; |
| 47 | case BgpConstants.Update.Origin.INCOMPLETE: |
| 48 | originString = "INCOMPLETE"; |
| 49 | break; |
| 50 | default: |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | return originString; |
| 55 | } |
| 56 | |
| 57 | } |