blob: b4bbf946a9b58c78e6ad62f3058fd39ed08aa5bb [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;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.incubator.net.routing.Route;
25import org.onosproject.incubator.net.routing.RouteService;
26import org.onosproject.incubator.net.routing.RouteTableId;
27
28import java.util.Collection;
29import java.util.Map;
30
31/**
32 * Command to show the routes in the routing tables.
33 */
Jonathan Hart92ca5d32016-04-14 18:05:52 -070034@Command(scope = "onos", name = "routes",
Jonathan Hartbfc5c482016-04-05 18:57:00 -070035 description = "Lists all routes in the route store")
36public class RoutesListCommand extends AbstractShellCommand {
37
38 private static final String FORMAT_HEADER =
39 " Network Next Hop";
40 private static final String FORMAT_ROUTE =
41 " %-18s %-15s";
42
43 private static final String FORMAT_TABLE = "Table: %s";
44 private static final String FORMAT_TOTAL = " Total: %d";
45
46 @Override
47 protected void execute() {
48 RouteService service = AbstractShellCommand.get(RouteService.class);
49
50 Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes();
51
Jonathan Hartb2111342016-04-20 16:57:37 -070052 if (outputJson()) {
53 ObjectMapper mapper = new ObjectMapper();
54 ObjectNode result = mapper.createObjectNode();
55 result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4"))));
56 result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6"))));
57 print("%s", result);
58 } else {
59 allRoutes.forEach((id, routes) -> {
60 print(FORMAT_TABLE, id);
61 print(FORMAT_HEADER);
62 routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop()));
63 print(FORMAT_TOTAL, routes.size());
64 print("");
65 });
66 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070067
68 }
69
Jonathan Hartb2111342016-04-20 16:57:37 -070070 /**
71 * Produces a JSON array of routes.
72 *
73 * @param routes the routes with the data
74 * @return JSON array with the routes
75 */
76 private JsonNode json(Collection<Route> routes) {
77 ObjectMapper mapper = new ObjectMapper();
78 ArrayNode result = mapper.createArrayNode();
79
80 for (Route route : routes) {
81 result.add(json(mapper, route));
82 }
83 return result;
84 }
85
86 /**
87 * Produces JSON object for a route.
88 *
89 * @param mapper the JSON object mapper to use
90 * @param route the route with the data
91 * @return JSON object for the route
92 */
93 private ObjectNode json(ObjectMapper mapper, Route route) {
94 ObjectNode result = mapper.createObjectNode();
95
96 result.put("prefix", route.prefix().toString());
97 result.put("nextHop", route.nextHop().toString());
98
99 return result;
100 }
101
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700102}