blob: 1b4658ea86b13d67aecd1ae851215a569b0b8e54 [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
18import org.apache.karaf.shell.commands.Command;
19import org.onosproject.cli.AbstractShellCommand;
20import org.onosproject.incubator.net.routing.Route;
21import org.onosproject.incubator.net.routing.RouteService;
22import org.onosproject.incubator.net.routing.RouteTableId;
23
24import java.util.Collection;
25import java.util.Map;
26
27/**
28 * Command to show the routes in the routing tables.
29 */
Jonathan Hart92ca5d32016-04-14 18:05:52 -070030@Command(scope = "onos", name = "routes",
Jonathan Hartbfc5c482016-04-05 18:57:00 -070031 description = "Lists all routes in the route store")
32public class RoutesListCommand extends AbstractShellCommand {
33
34 private static final String FORMAT_HEADER =
35 " Network Next Hop";
36 private static final String FORMAT_ROUTE =
37 " %-18s %-15s";
38
39 private static final String FORMAT_TABLE = "Table: %s";
40 private static final String FORMAT_TOTAL = " Total: %d";
41
42 @Override
43 protected void execute() {
44 RouteService service = AbstractShellCommand.get(RouteService.class);
45
46 Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes();
47
48 allRoutes.forEach((id, routes) -> {
49 print(FORMAT_TABLE, id);
50 print(FORMAT_HEADER);
51 routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop()));
52 print(FORMAT_TOTAL, routes.size());
53 print("");
54 });
55
56 }
57
58}