blob: ab2b91ef05cfd0d75f4187a7fec69c43f01d898f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 Hart41349e92015-02-09 14:14:02 -080025import org.onosproject.routingapi.RouteEntry;
26import org.onosproject.routingapi.RoutingService;
27
28import java.util.Collection;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070029
30/**
31 * Command to show the list of routes in SDN-IP's routing table.
32 */
33@Command(scope = "onos", name = "routes",
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080034 description = "Lists all SDN-IP best routes")
Jonathan Hart0b04bed2014-10-16 16:39:19 -070035public class RoutesListCommand extends AbstractShellCommand {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080036 @Option(name = "-s", aliases = "--summary",
37 description = "SDN-IP routes summary",
38 required = false, multiValued = false)
39 private boolean routesSummary = false;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070040
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080041 private static final String FORMAT_SUMMARY_V4 =
42 "Total SDN-IP IPv4 routes = %d";
43 private static final String FORMAT_SUMMARY_V6 =
44 "Total SDN-IP IPv6 routes = %d";
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080045 private static final String FORMAT_HEADER =
46 " Network Next Hop";
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080047 private static final String FORMAT_ROUTE =
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080048 " %-18s %-15s";
Jonathan Hart0b04bed2014-10-16 16:39:19 -070049
50 @Override
51 protected void execute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080052 RoutingService service = AbstractShellCommand.get(RoutingService.class);
Jonathan Hart0b04bed2014-10-16 16:39:19 -070053
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080054 // Print summary of the routes
55 if (routesSummary) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080056 printSummary(service.getRoutes4(), service.getRoutes6());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080057 return;
58 }
59
60 // Print all routes
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080061 printRoutes(service.getRoutes4(), service.getRoutes6());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080062 }
63
64 /**
65 * Prints summary of the routes.
66 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080067 * @param routes4 the IPv4 routes
68 * @param routes6 the IPv6 routes
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080069 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080070 private void printSummary(Collection<RouteEntry> routes4,
71 Collection<RouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080072 if (outputJson()) {
73 ObjectMapper mapper = new ObjectMapper();
74 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080075 result.put("totalRoutes4", routes4.size());
76 result.put("totalRoutes6", routes6.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080077 print("%s", result);
78 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080079 print(FORMAT_SUMMARY_V4, routes4.size());
80 print(FORMAT_SUMMARY_V6, routes6.size());
Jonathan Hart0b04bed2014-10-16 16:39:19 -070081 }
82 }
83
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080084 /**
85 * Prints all routes.
86 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080087 * @param routes4 the IPv4 routes to print
88 * @param routes6 the IPv6 routes to print
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080089 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080090 private void printRoutes(Collection<RouteEntry> routes4,
91 Collection<RouteEntry> routes6) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080092 if (outputJson()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080093 ObjectMapper mapper = new ObjectMapper();
94 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -080095 result.set("routes4", json(routes4));
96 result.set("routes6", json(routes6));
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080097 print("%s", result);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080098 } else {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080099 // The IPv4 routes
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800100 print(FORMAT_HEADER);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800101 for (RouteEntry route : routes4) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800102 printRoute(route);
103 }
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800104 print(FORMAT_SUMMARY_V4, routes4.size());
105 print(""); // Empty separator line
106 // The IPv6 routes
107 print(FORMAT_HEADER);
108 for (RouteEntry route : routes6) {
109 printRoute(route);
110 }
111 print(FORMAT_SUMMARY_V6, routes6.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800112 }
113 }
114
115 /**
116 * Prints a route.
117 *
118 * @param route the route to print
119 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700120 private void printRoute(RouteEntry route) {
121 if (route != null) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800122 print(FORMAT_ROUTE, route.prefix(), route.nextHop());
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700123 }
124 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800125
126 /**
127 * Produces a JSON array of routes.
128 *
129 * @param routes the routes with the data
130 * @return JSON array with the routes
131 */
132 private JsonNode json(Collection<RouteEntry> routes) {
133 ObjectMapper mapper = new ObjectMapper();
134 ArrayNode result = mapper.createArrayNode();
135
136 for (RouteEntry route : routes) {
137 result.add(json(mapper, route));
138 }
139 return result;
140 }
141
142 /**
143 * Produces JSON object for a route.
144 *
145 * @param mapper the JSON object mapper to use
146 * @param route the route with the data
147 * @return JSON object for the route
148 */
149 private ObjectNode json(ObjectMapper mapper, RouteEntry route) {
150 ObjectNode result = mapper.createObjectNode();
151
152 result.put("prefix", route.prefix().toString());
153 result.put("nextHop", route.nextHop().toString());
154
155 return result;
156 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700157}