blob: 3283efc80edf3db5a235c492fb6ac7f7f5f389db [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 Hart41349e92015-02-09 14:14:02 -080016package org.onosproject.routing.cli;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070022import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080023import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart2da1e602015-02-18 19:09:24 -080025import org.onosproject.routing.RouteEntry;
26import org.onosproject.routing.RoutingService;
Jonathan Hart41349e92015-02-09 14:14:02 -080027
28import java.util.Collection;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070029
30/**
Jonathan Hartde15e1c2016-02-03 10:51:50 -080031 * Command to show the routes in the routing table.
Jonathan Hart0b04bed2014-10-16 16:39:19 -070032 */
33@Command(scope = "onos", name = "routes",
Jonathan Hartde15e1c2016-02-03 10:51:50 -080034 description = "Lists all routes in the RIB")
Jonathan Hart0b04bed2014-10-16 16:39:19 -070035public class RoutesListCommand extends AbstractShellCommand {
Jonathan Hartde15e1c2016-02-03 10:51:50 -080036
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080037 @Option(name = "-s", aliases = "--summary",
Jonathan Hartde15e1c2016-02-03 10:51:50 -080038 description = "Show summary of routes",
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080039 required = false, multiValued = false)
40 private boolean routesSummary = false;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070041
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080042 private static final String FORMAT_SUMMARY_V4 =
Jonathan Hartde15e1c2016-02-03 10:51:50 -080043 "Total IPv4 routes = %d";
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080044 private static final String FORMAT_SUMMARY_V6 =
Jonathan Hartde15e1c2016-02-03 10:51:50 -080045 "Total IPv6 routes = %d";
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080046 private static final String FORMAT_HEADER =
47 " Network Next Hop";
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080048 private static final String FORMAT_ROUTE =
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080049 " %-18s %-15s";
Jonathan Hart0b04bed2014-10-16 16:39:19 -070050
51 @Override
52 protected void execute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080053 RoutingService service = AbstractShellCommand.get(RoutingService.class);
Jonathan Hart0b04bed2014-10-16 16:39:19 -070054
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080055 // Print summary of the routes
56 if (routesSummary) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080057 printSummary(service.getRoutes4(), service.getRoutes6());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080058 return;
59 }
60
61 // Print all routes
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080062 printRoutes(service.getRoutes4(), service.getRoutes6());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080063 }
64
65 /**
66 * Prints summary of the routes.
67 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080068 * @param routes4 the IPv4 routes
69 * @param routes6 the IPv6 routes
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080070 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080071 private void printSummary(Collection<RouteEntry> routes4,
72 Collection<RouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080073 if (outputJson()) {
74 ObjectMapper mapper = new ObjectMapper();
75 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080076 result.put("totalRoutes4", routes4.size());
77 result.put("totalRoutes6", routes6.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080078 print("%s", result);
79 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080080 print(FORMAT_SUMMARY_V4, routes4.size());
81 print(FORMAT_SUMMARY_V6, routes6.size());
Jonathan Hart0b04bed2014-10-16 16:39:19 -070082 }
83 }
84
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080085 /**
86 * Prints all routes.
87 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080088 * @param routes4 the IPv4 routes to print
89 * @param routes6 the IPv6 routes to print
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080090 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080091 private void printRoutes(Collection<RouteEntry> routes4,
92 Collection<RouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080093 if (outputJson()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080094 ObjectMapper mapper = new ObjectMapper();
95 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -080096 result.set("routes4", json(routes4));
97 result.set("routes6", json(routes6));
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080098 print("%s", result);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080099 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800100 // The IPv4 routes
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800101 print(FORMAT_HEADER);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800102 for (RouteEntry route : routes4) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800103 printRoute(route);
104 }
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800105 print(FORMAT_SUMMARY_V4, routes4.size());
106 print(""); // Empty separator line
107 // The IPv6 routes
108 print(FORMAT_HEADER);
109 for (RouteEntry route : routes6) {
110 printRoute(route);
111 }
112 print(FORMAT_SUMMARY_V6, routes6.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800113 }
114 }
115
116 /**
117 * Prints a route.
118 *
119 * @param route the route to print
120 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700121 private void printRoute(RouteEntry route) {
122 if (route != null) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800123 print(FORMAT_ROUTE, route.prefix(), route.nextHop());
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700124 }
125 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800126
127 /**
128 * Produces a JSON array of routes.
129 *
130 * @param routes the routes with the data
131 * @return JSON array with the routes
132 */
133 private JsonNode json(Collection<RouteEntry> routes) {
134 ObjectMapper mapper = new ObjectMapper();
135 ArrayNode result = mapper.createArrayNode();
136
137 for (RouteEntry route : routes) {
138 result.add(json(mapper, route));
139 }
140 return result;
141 }
142
143 /**
144 * Produces JSON object for a route.
145 *
146 * @param mapper the JSON object mapper to use
147 * @param route the route with the data
148 * @return JSON object for the route
149 */
150 private ObjectNode json(ObjectMapper mapper, RouteEntry route) {
151 ObjectNode result = mapper.createObjectNode();
152
153 result.put("prefix", route.prefix().toString());
154 result.put("nextHop", route.nextHop().toString());
155
156 return result;
157 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700158}