Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 19 | package org.onlab.onos.sdnip.cli; |
| 20 | |
| 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.onlab.onos.cli.AbstractShellCommand; |
| 23 | import org.onlab.onos.sdnip.SdnIpService; |
| 24 | import org.onlab.onos.sdnip.bgp.BgpConstants; |
| 25 | import 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") |
| 32 | public 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 | } |