Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.cli; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 25 | import org.onosproject.routing.bgp.BgpConstants.Update; |
| 26 | import org.onosproject.routing.bgp.BgpInfoService; |
| 27 | import org.onosproject.routing.bgp.BgpRouteEntry; |
| 28 | import org.onosproject.routing.bgp.BgpSession; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.Collection; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Command to show the routes learned through BGP. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "bgp-routes", |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 37 | description = "Lists all BGP best routes") |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 38 | public class BgpRoutesListCommand extends AbstractShellCommand { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 39 | @Option(name = "-s", aliases = "--summary", |
| 40 | description = "BGP routes summary", |
| 41 | required = false, multiValued = false) |
| 42 | private boolean routesSummary = false; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 43 | |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 44 | @Option(name = "-n", aliases = "--neighbor", |
| 45 | description = "Routes from a BGP neighbor", |
| 46 | required = false, multiValued = false) |
| 47 | private String bgpNeighbor; |
| 48 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 49 | private static final String FORMAT_SUMMARY_V4 = |
| 50 | "Total BGP IPv4 routes = %d"; |
| 51 | private static final String FORMAT_SUMMARY_V6 = |
| 52 | "Total BGP IPv6 routes = %d"; |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 53 | private static final String FORMAT_HEADER = |
| 54 | " Network Next Hop Origin LocalPref MED BGP-ID"; |
| 55 | private static final String FORMAT_ROUTE_LINE1 = |
| 56 | " %-18s %-15s %6s %9s %9s %-15s"; |
| 57 | private static final String FORMAT_ROUTE_LINE2 = |
| 58 | " AsPath %s"; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | protected void execute() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 62 | BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 63 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 64 | // Print summary of the routes |
| 65 | if (routesSummary) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 66 | printSummary(service.getBgpRoutes4(), service.getBgpRoutes6()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 70 | BgpSession foundBgpSession = null; |
| 71 | if (bgpNeighbor != null) { |
| 72 | // Print the routes from a single neighbor (if found) |
| 73 | for (BgpSession bgpSession : service.getBgpSessions()) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 74 | if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) { |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 75 | foundBgpSession = bgpSession; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | if (foundBgpSession == null) { |
| 80 | print("BGP neighbor %s not found", bgpNeighbor); |
| 81 | return; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Print the routes |
| 86 | if (foundBgpSession != null) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 87 | printRoutes(foundBgpSession.getBgpRibIn4(), |
| 88 | foundBgpSession.getBgpRibIn6()); |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 89 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 90 | printRoutes(service.getBgpRoutes4(), service.getBgpRoutes6()); |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 91 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Prints summary of the routes. |
| 96 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 97 | * @param routes4 the IPv4 routes |
| 98 | * @param routes6 the IPv6 routes |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 99 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 100 | private void printSummary(Collection<BgpRouteEntry> routes4, |
| 101 | Collection<BgpRouteEntry> routes6) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 102 | if (outputJson()) { |
| 103 | ObjectMapper mapper = new ObjectMapper(); |
| 104 | ObjectNode result = mapper.createObjectNode(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 105 | result.put("totalRoutes4", routes4.size()); |
| 106 | result.put("totalRoutes6", routes6.size()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 107 | print("%s", result); |
| 108 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 109 | print(FORMAT_SUMMARY_V4, routes4.size()); |
| 110 | print(FORMAT_SUMMARY_V6, routes6.size()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Prints all routes. |
| 116 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 117 | * @param routes4 the IPv4 routes to print |
| 118 | * @param routes6 the IPv6 routes to print |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 119 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 120 | private void printRoutes(Collection<BgpRouteEntry> routes4, |
| 121 | Collection<BgpRouteEntry> routes6) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 122 | if (outputJson()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 123 | ObjectMapper mapper = new ObjectMapper(); |
| 124 | ObjectNode result = mapper.createObjectNode(); |
| 125 | result.put("routes4", json(routes4)); |
| 126 | result.put("routes6", json(routes6)); |
| 127 | print("%s", result); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 128 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 129 | // The IPv4 routes |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 130 | print(FORMAT_HEADER); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 131 | for (BgpRouteEntry route : routes4) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 132 | printRoute(route); |
| 133 | } |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 134 | print(FORMAT_SUMMARY_V4, routes4.size()); |
| 135 | print(""); // Empty separator line |
| 136 | // The IPv6 routes |
| 137 | print(FORMAT_HEADER); |
| 138 | for (BgpRouteEntry route : routes6) { |
| 139 | printRoute(route); |
| 140 | } |
| 141 | print(FORMAT_SUMMARY_V6, routes6.size()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Prints a BGP route. |
| 147 | * |
| 148 | * @param route the route to print |
| 149 | */ |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 150 | private void printRoute(BgpRouteEntry route) { |
| 151 | if (route != null) { |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 152 | print(FORMAT_ROUTE_LINE1, route.prefix(), route.nextHop(), |
| 153 | Update.Origin.typeToString(route.getOrigin()), |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 154 | route.getLocalPref(), route.getMultiExitDisc(), |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 155 | route.getBgpSession().remoteInfo().bgpId()); |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 156 | print(FORMAT_ROUTE_LINE2, asPath4Cli(route.getAsPath())); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 160 | /** |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 161 | * Formats the AS Path as a string that can be shown on the CLI. |
| 162 | * |
| 163 | * @param asPath the AS Path to format |
| 164 | * @return the AS Path as a string |
| 165 | */ |
| 166 | private String asPath4Cli(BgpRouteEntry.AsPath asPath) { |
| 167 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = |
| 168 | asPath.getPathSegments(); |
| 169 | |
| 170 | if (pathSegments.isEmpty()) { |
| 171 | return "[none]"; |
| 172 | } |
| 173 | |
| 174 | final StringBuilder builder = new StringBuilder(); |
| 175 | for (BgpRouteEntry.PathSegment pathSegment : pathSegments) { |
| 176 | String prefix = null; |
| 177 | String suffix = null; |
| 178 | switch (pathSegment.getType()) { |
| 179 | case Update.AsPath.AS_SET: |
| 180 | prefix = "[AS-Set"; |
| 181 | suffix = "]"; |
| 182 | break; |
| 183 | case Update.AsPath.AS_SEQUENCE: |
| 184 | break; |
| 185 | case Update.AsPath.AS_CONFED_SEQUENCE: |
| 186 | prefix = "[AS-Confed-Seq"; |
| 187 | suffix = "]"; |
| 188 | break; |
| 189 | case Update.AsPath.AS_CONFED_SET: |
| 190 | prefix = "[AS-Confed-Set"; |
| 191 | suffix = "]"; |
| 192 | break; |
| 193 | default: |
| 194 | builder.append(String.format("(type = %s)", |
| 195 | Update.AsPath.typeToString(pathSegment.getType()))); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | if (prefix != null) { |
| 200 | if (builder.length() > 0) { |
| 201 | builder.append(" "); // Separator |
| 202 | } |
| 203 | builder.append(prefix); |
| 204 | } |
| 205 | // Print the AS numbers |
| 206 | for (Long asn : pathSegment.getSegmentAsNumbers()) { |
| 207 | if (builder.length() > 0) { |
| 208 | builder.append(" "); // Separator |
| 209 | } |
| 210 | builder.append(String.format("%d", asn)); |
| 211 | } |
| 212 | if (suffix != null) { |
| 213 | // No need for separator |
| 214 | builder.append(prefix); |
| 215 | } |
| 216 | } |
| 217 | return builder.toString(); |
| 218 | } |
| 219 | |
| 220 | /** |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 221 | * Produces a JSON array of routes. |
| 222 | * |
| 223 | * @param routes the routes with the data |
| 224 | * @return JSON array with the routes |
| 225 | */ |
| 226 | private JsonNode json(Collection<BgpRouteEntry> routes) { |
| 227 | ObjectMapper mapper = new ObjectMapper(); |
| 228 | ArrayNode result = mapper.createArrayNode(); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 229 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 230 | for (BgpRouteEntry route : routes) { |
| 231 | result.add(json(mapper, route)); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 232 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 233 | return result; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 236 | /** |
| 237 | * Produces JSON object for a route. |
| 238 | * |
| 239 | * @param mapper the JSON object mapper to use |
| 240 | * @param route the route with the data |
| 241 | * @return JSON object for the route |
| 242 | */ |
| 243 | private ObjectNode json(ObjectMapper mapper, BgpRouteEntry route) { |
| 244 | ObjectNode result = mapper.createObjectNode(); |
| 245 | |
| 246 | result.put("prefix", route.prefix().toString()); |
| 247 | result.put("nextHop", route.nextHop().toString()); |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 248 | result.put("bgpId", |
| 249 | route.getBgpSession().remoteInfo().bgpId().toString()); |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 250 | result.put("origin", Update.Origin.typeToString(route.getOrigin())); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 251 | result.put("asPath", json(mapper, route.getAsPath())); |
| 252 | result.put("localPref", route.getLocalPref()); |
| 253 | result.put("multiExitDisc", route.getMultiExitDisc()); |
| 254 | |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Produces JSON object for an AS path. |
| 260 | * |
| 261 | * @param mapper the JSON object mapper to use |
| 262 | * @param asPath the AS path with the data |
| 263 | * @return JSON object for the AS path |
| 264 | */ |
| 265 | private ObjectNode json(ObjectMapper mapper, BgpRouteEntry.AsPath asPath) { |
| 266 | ObjectNode result = mapper.createObjectNode(); |
| 267 | ArrayNode pathSegmentsJson = mapper.createArrayNode(); |
| 268 | for (BgpRouteEntry.PathSegment pathSegment : asPath.getPathSegments()) { |
| 269 | ObjectNode pathSegmentJson = mapper.createObjectNode(); |
| 270 | pathSegmentJson.put("type", |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 271 | Update.AsPath.typeToString(pathSegment.getType())); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 272 | ArrayNode segmentAsNumbersJson = mapper.createArrayNode(); |
| 273 | for (Long asNumber : pathSegment.getSegmentAsNumbers()) { |
| 274 | segmentAsNumbersJson.add(asNumber); |
| 275 | } |
| 276 | pathSegmentJson.put("segmentAsNumbers", segmentAsNumbersJson); |
| 277 | pathSegmentsJson.add(pathSegmentJson); |
| 278 | } |
| 279 | result.put("pathSegments", pathSegmentsJson); |
| 280 | |
| 281 | return result; |
| 282 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 283 | } |