blob: 31b755559c2b52c2265a34eb5711980cd5601672 [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Jonathan Hart96c146b2017-02-24 16:32:00 -08002 * Copyright 2017-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;
Jonathan Hart96c146b2017-02-24 16:32:00 -080024import org.onosproject.incubator.net.routing.ResolvedRoute;
Jonathan Hart96c146b2017-02-24 16:32:00 -080025import org.onosproject.incubator.net.routing.RouteInfo;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070026import org.onosproject.incubator.net.routing.RouteService;
27import org.onosproject.incubator.net.routing.RouteTableId;
28
29import java.util.Collection;
Jonathan Hart96c146b2017-02-24 16:32:00 -080030import java.util.Comparator;
31import java.util.Objects;
32import java.util.Optional;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070033
34/**
35 * Command to show the routes in the routing tables.
36 */
Jonathan Hart92ca5d32016-04-14 18:05:52 -070037@Command(scope = "onos", name = "routes",
Jonathan Hart96c146b2017-02-24 16:32:00 -080038 description = "Lists routes in the route store")
Jonathan Hartbfc5c482016-04-05 18:57:00 -070039public class RoutesListCommand extends AbstractShellCommand {
40
Charles Chan92ca94d2017-03-17 18:05:22 -070041 private static final String NETWORK = "Network";
42 private static final String NEXTHOP = "Next Hop";
43 private static final String SOURCE = "Source";
44
45 private static final String FORMAT_ROUTE = "%-1s %-18s %-15s %-10s";
46 private static final String FORMAT_ROUTE6 = "%-1s %-43s %-39s %-10s";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070047
48 private static final String FORMAT_TABLE = "Table: %s";
49 private static final String FORMAT_TOTAL = " Total: %d";
50
51 @Override
52 protected void execute() {
53 RouteService service = AbstractShellCommand.get(RouteService.class);
54
Jonathan Hartb2111342016-04-20 16:57:37 -070055 if (outputJson()) {
56 ObjectMapper mapper = new ObjectMapper();
57 ObjectNode result = mapper.createObjectNode();
Jonathan Hart96c146b2017-02-24 16:32:00 -080058 result.set("routes4", json(service.getRoutes(new RouteTableId("ipv4"))));
59 result.set("routes6", json(service.getRoutes(new RouteTableId("ipv6"))));
Jonathan Hartb2111342016-04-20 16:57:37 -070060 print("%s", result);
61 } else {
Jonathan Hart96c146b2017-02-24 16:32:00 -080062 service.getRouteTables().forEach(id -> {
Jonathan Hart96c146b2017-02-24 16:32:00 -080063 Collection<RouteInfo> tableRoutes = service.getRoutes(id);
64
Charles Chan92ca94d2017-03-17 18:05:22 -070065 String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ?
66 FORMAT_ROUTE6 : FORMAT_ROUTE;
67
68 // Print header
69 print(FORMAT_TABLE, id);
70 print(format, "", NETWORK, NEXTHOP, SOURCE);
71
72 // Print routing entries
Jonathan Hart96c146b2017-02-24 16:32:00 -080073 tableRoutes.stream()
74 .sorted(Comparator.comparing(r -> r.prefix().address()))
Charles Chan92ca94d2017-03-17 18:05:22 -070075 .forEach(route -> this.print(format, route));
Jonathan Hart96c146b2017-02-24 16:32:00 -080076
77 print(FORMAT_TOTAL, tableRoutes.size());
Jonathan Hartb2111342016-04-20 16:57:37 -070078 print("");
79 });
80 }
Jonathan Hart96c146b2017-02-24 16:32:00 -080081 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070082
Charles Chan92ca94d2017-03-17 18:05:22 -070083 private void print(String format, RouteInfo routeInfo) {
Jonathan Hart96c146b2017-02-24 16:32:00 -080084 routeInfo.allRoutes()
Charles Chan92ca94d2017-03-17 18:05:22 -070085 .forEach(r -> print(format, isBestRoute(routeInfo.bestRoute(), r) ? ">" : "",
Jonathan Hartf2e7a342017-03-20 15:43:21 -070086 r.prefix(), r.nextHop(), r.route().source()));
Jonathan Hart96c146b2017-02-24 16:32:00 -080087 }
88
89
90
91 private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) {
92 return Objects.equals(bestRoute.orElse(null), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -070093 }
94
Jonathan Hartb2111342016-04-20 16:57:37 -070095 /**
96 * Produces a JSON array of routes.
97 *
98 * @param routes the routes with the data
99 * @return JSON array with the routes
100 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800101 private JsonNode json(Collection<RouteInfo> routes) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700102 ObjectMapper mapper = new ObjectMapper();
103 ArrayNode result = mapper.createArrayNode();
104
Jonathan Hart96c146b2017-02-24 16:32:00 -0800105 routes.stream()
106 .flatMap(ri -> ri.allRoutes().stream())
107 .forEach(r -> result.add(json(mapper, r)));
108
Jonathan Hartb2111342016-04-20 16:57:37 -0700109 return result;
110 }
111
112 /**
113 * Produces JSON object for a route.
114 *
115 * @param mapper the JSON object mapper to use
116 * @param route the route with the data
117 * @return JSON object for the route
118 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800119 private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700120 ObjectNode result = mapper.createObjectNode();
121
122 result.put("prefix", route.prefix().toString());
123 result.put("nextHop", route.nextHop().toString());
124
125 return result;
126 }
127
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700128}