blob: 7b61f7c3b70205028839af9751157d04a6df94ae [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hartf4bd0482017-01-27 15:11:18 -080016
Jonathan Hart41349e92015-02-09 14:14:02 -080017package org.onosproject.routing.cli;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080018
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070023import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080024import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
Jonathan Hartf4bd0482017-01-27 15:11:18 -080026import org.onosproject.routing.bgp.BgpConstants;
Jonathan Hart41349e92015-02-09 14:14:02 -080027import org.onosproject.routing.bgp.BgpInfoService;
28import org.onosproject.routing.bgp.BgpRouteEntry;
29import org.onosproject.routing.bgp.BgpSession;
30
31import java.util.ArrayList;
32import java.util.Collection;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070033
34/**
35 * Command to show the routes learned through BGP.
36 */
37@Command(scope = "onos", name = "bgp-routes",
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080038 description = "Lists all BGP best routes")
Jonathan Hart0b04bed2014-10-16 16:39:19 -070039public class BgpRoutesListCommand extends AbstractShellCommand {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080040 @Option(name = "-s", aliases = "--summary",
41 description = "BGP routes summary",
42 required = false, multiValued = false)
43 private boolean routesSummary = false;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070044
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080045 @Option(name = "-n", aliases = "--neighbor",
46 description = "Routes from a BGP neighbor",
47 required = false, multiValued = false)
48 private String bgpNeighbor;
49
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080050 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 Radoslavovdfde7ab2014-12-04 14:06:39 -080054 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 Hart0b04bed2014-10-16 16:39:19 -070060
61 @Override
62 protected void execute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080063 BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class);
Jonathan Hart0b04bed2014-10-16 16:39:19 -070064
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080065 // Print summary of the routes
66 if (routesSummary) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080067 printSummary(service.getBgpRoutes4(), service.getBgpRoutes6());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080068 return;
69 }
70
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080071 BgpSession foundBgpSession = null;
72 if (bgpNeighbor != null) {
73 // Print the routes from a single neighbor (if found)
74 for (BgpSession bgpSession : service.getBgpSessions()) {
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080075 if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080076 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 Radoslavov3a0a52e2015-01-06 17:41:37 -080088 printRoutes(foundBgpSession.getBgpRibIn4(),
89 foundBgpSession.getBgpRibIn6());
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080090 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080091 printRoutes(service.getBgpRoutes4(), service.getBgpRoutes6());
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080092 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080093 }
94
95 /**
96 * Prints summary of the routes.
97 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080098 * @param routes4 the IPv4 routes
99 * @param routes6 the IPv6 routes
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800100 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800101 private void printSummary(Collection<BgpRouteEntry> routes4,
102 Collection<BgpRouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800103 if (outputJson()) {
104 ObjectMapper mapper = new ObjectMapper();
105 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800106 result.put("totalRoutes4", routes4.size());
107 result.put("totalRoutes6", routes6.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800108 print("%s", result);
109 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800110 print(FORMAT_SUMMARY_V4, routes4.size());
111 print(FORMAT_SUMMARY_V6, routes6.size());
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700112 }
113 }
114
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800115 /**
116 * Prints all routes.
117 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800118 * @param routes4 the IPv4 routes to print
119 * @param routes6 the IPv6 routes to print
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800120 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800121 private void printRoutes(Collection<BgpRouteEntry> routes4,
122 Collection<BgpRouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800123 if (outputJson()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800124 ObjectMapper mapper = new ObjectMapper();
125 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -0800126 result.set("routes4", json(routes4));
127 result.set("routes6", json(routes6));
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800128 print("%s", result);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800129 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800130 // The IPv4 routes
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800131 print(FORMAT_HEADER);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800132 for (BgpRouteEntry route : routes4) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800133 printRoute(route);
134 }
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800135 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 Radoslavov2ce1c522014-11-07 10:32:37 -0800143 }
144 }
145
146 /**
147 * Prints a BGP route.
148 *
149 * @param route the route to print
150 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700151 private void printRoute(BgpRouteEntry route) {
152 if (route != null) {
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800153 print(FORMAT_ROUTE_LINE1, route.prefix(), route.nextHop(),
Jonathan Hart2da1e602015-02-18 19:09:24 -0800154 BgpConstants.Update.Origin.typeToString(route.getOrigin()),
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800155 route.getLocalPref(), route.getMultiExitDisc(),
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800156 route.getBgpSession().remoteInfo().bgpId());
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800157 print(FORMAT_ROUTE_LINE2, asPath4Cli(route.getAsPath()));
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700158 }
159 }
160
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800161 /**
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800162 * 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 Hart2da1e602015-02-18 19:09:24 -0800180 case BgpConstants.Update.AsPath.AS_SET:
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800181 prefix = "[AS-Set";
182 suffix = "]";
183 break;
Jonathan Hart2da1e602015-02-18 19:09:24 -0800184 case BgpConstants.Update.AsPath.AS_SEQUENCE:
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800185 break;
Jonathan Hart2da1e602015-02-18 19:09:24 -0800186 case BgpConstants.Update.AsPath.AS_CONFED_SEQUENCE:
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800187 prefix = "[AS-Confed-Seq";
188 suffix = "]";
189 break;
Jonathan Hart2da1e602015-02-18 19:09:24 -0800190 case BgpConstants.Update.AsPath.AS_CONFED_SET:
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800191 prefix = "[AS-Confed-Set";
192 suffix = "]";
193 break;
194 default:
195 builder.append(String.format("(type = %s)",
Jonathan Hart2da1e602015-02-18 19:09:24 -0800196 BgpConstants.Update.AsPath.typeToString(pathSegment.getType())));
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800197 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 Radoslavov2ce1c522014-11-07 10:32:37 -0800222 * 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 Hart0b04bed2014-10-16 16:39:19 -0700230
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800231 for (BgpRouteEntry route : routes) {
232 result.add(json(mapper, route));
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700233 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800234 return result;
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700235 }
236
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800237 /**
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 Radoslavov8a36ce32015-01-28 12:26:57 -0800249 result.put("bgpId",
250 route.getBgpSession().remoteInfo().bgpId().toString());
Jonathan Hart2da1e602015-02-18 19:09:24 -0800251 result.put("origin", BgpConstants.Update.Origin.typeToString(route.getOrigin()));
Ray Milkey9d810f62015-02-13 11:20:58 -0800252 result.set("asPath", json(mapper, route.getAsPath()));
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800253 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 Hart2da1e602015-02-18 19:09:24 -0800272 BgpConstants.Update.AsPath.typeToString(pathSegment.getType()));
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800273 ArrayNode segmentAsNumbersJson = mapper.createArrayNode();
274 for (Long asNumber : pathSegment.getSegmentAsNumbers()) {
275 segmentAsNumbersJson.add(asNumber);
276 }
Ray Milkey9d810f62015-02-13 11:20:58 -0800277 pathSegmentJson.set("segmentAsNumbers", segmentAsNumbersJson);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800278 pathSegmentsJson.add(pathSegmentJson);
279 }
Ray Milkey9d810f62015-02-13 11:20:58 -0800280 result.set("pathSegments", pathSegmentsJson);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800281
282 return result;
283 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700284}