blob: 780a9ab1a31b71d7cbf67de449c035e53c9fa9dd [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jonathan Hartbfc5c482016-04-05 18:57:00 -07003 *
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
7 *
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.
15 */
16package org.onosproject.cli.net;
17
Jonathan Hartb2111342016-04-20 16:57:37 -070018import 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 Hartbfc5c482016-04-05 18:57:00 -070022import org.apache.karaf.shell.commands.Command;
Jonathan Hartf54c3e72016-04-21 18:08:44 -070023import org.apache.karaf.shell.commands.Option;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.incubator.net.routing.Route;
26import org.onosproject.incubator.net.routing.RouteService;
27import org.onosproject.incubator.net.routing.RouteTableId;
28
29import java.util.Collection;
30import java.util.Map;
31
32/**
33 * Command to show the routes in the routing tables.
34 */
Jonathan Hart92ca5d32016-04-14 18:05:52 -070035@Command(scope = "onos", name = "routes",
Jonathan Hartbfc5c482016-04-05 18:57:00 -070036 description = "Lists all routes in the route store")
37public class RoutesListCommand extends AbstractShellCommand {
38
Jonathan Hartf54c3e72016-04-21 18:08:44 -070039 @Option(name = "-s", aliases = "--summary",
40 description = "Show summary of routes")
41 private boolean summary = false;
42
43 private static final String FORMAT_SUMMARY =
44 "Number of routes in table %s: %s";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070045 private static final String FORMAT_HEADER =
Charles Chan218b9f32016-12-06 20:38:27 -080046 " Network Next Hop Source";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070047 private static final String FORMAT_ROUTE =
Charles Chan218b9f32016-12-06 20:38:27 -080048 " %-18s %-15s %-10s";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070049
50 private static final String FORMAT_TABLE = "Table: %s";
51 private static final String FORMAT_TOTAL = " Total: %d";
52
53 @Override
54 protected void execute() {
55 RouteService service = AbstractShellCommand.get(RouteService.class);
56
57 Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes();
58
Jonathan Hartf54c3e72016-04-21 18:08:44 -070059 if (summary) {
60 if (outputJson()) {
61 ObjectMapper mapper = new ObjectMapper();
62 ObjectNode result = mapper.createObjectNode();
63 result.put("totalRoutes4", allRoutes.get(new RouteTableId("ipv4")).size());
64 result.put("totalRoutes6", allRoutes.get(new RouteTableId("ipv6")).size());
65 print("%s", result);
66 } else {
67 allRoutes.forEach((id, routes) -> print(FORMAT_SUMMARY, id, routes.size()));
68 }
69
70 return;
71 }
72
Jonathan Hartb2111342016-04-20 16:57:37 -070073 if (outputJson()) {
74 ObjectMapper mapper = new ObjectMapper();
75 ObjectNode result = mapper.createObjectNode();
76 result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4"))));
77 result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6"))));
78 print("%s", result);
79 } else {
80 allRoutes.forEach((id, routes) -> {
81 print(FORMAT_TABLE, id);
82 print(FORMAT_HEADER);
Charles Chan218b9f32016-12-06 20:38:27 -080083 routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop(), r.source()));
Jonathan Hartb2111342016-04-20 16:57:37 -070084 print(FORMAT_TOTAL, routes.size());
85 print("");
86 });
87 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070088
89 }
90
Jonathan Hartb2111342016-04-20 16:57:37 -070091 /**
92 * Produces a JSON array of routes.
93 *
94 * @param routes the routes with the data
95 * @return JSON array with the routes
96 */
97 private JsonNode json(Collection<Route> routes) {
98 ObjectMapper mapper = new ObjectMapper();
99 ArrayNode result = mapper.createArrayNode();
100
101 for (Route route : routes) {
102 result.add(json(mapper, route));
103 }
104 return result;
105 }
106
107 /**
108 * Produces JSON object for a route.
109 *
110 * @param mapper the JSON object mapper to use
111 * @param route the route with the data
112 * @return JSON object for the route
113 */
114 private ObjectNode json(ObjectMapper mapper, Route route) {
115 ObjectNode result = mapper.createObjectNode();
116
117 result.put("prefix", route.prefix().toString());
118 result.put("nextHop", route.nextHop().toString());
119
120 return result;
121 }
122
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700123}