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