blob: aabbabd4e154dc17a59d065066c29c6f480593a9 [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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 */
Ray Milkey69ec8712017-08-08 13:00:43 -070016package org.onosproject.routeservice.cli;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070017
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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Command;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070023import org.onosproject.cli.AbstractShellCommand;
Ray Milkey69ec8712017-08-08 13:00:43 -070024import org.onosproject.routeservice.ResolvedRoute;
25import org.onosproject.routeservice.RouteInfo;
26import org.onosproject.routeservice.RouteService;
27import org.onosproject.routeservice.RouteTableId;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070028
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";
Jonathan Hart10dbafd2017-05-18 15:53:03 -070044 private static final String NODE = "Node";
Charles Chan92ca94d2017-03-17 18:05:22 -070045
Charles Chan32a31aa2018-04-11 11:10:42 -040046 private static final String FORMAT_ROUTE = "%-1s %-1s %-18s %-15s %s (%s)";
47 private static final String FORMAT_ROUTE6 = "%-1s %-1s %-43s %-39s %s (%s)";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070048
49 private static final String FORMAT_TABLE = "Table: %s";
50 private static final String FORMAT_TOTAL = " Total: %d";
51
52 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070053 protected void doExecute() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070054 RouteService service = AbstractShellCommand.get(RouteService.class);
55
Jonathan Hartb2111342016-04-20 16:57:37 -070056 if (outputJson()) {
57 ObjectMapper mapper = new ObjectMapper();
58 ObjectNode result = mapper.createObjectNode();
Jonathan Hart96c146b2017-02-24 16:32:00 -080059 result.set("routes4", json(service.getRoutes(new RouteTableId("ipv4"))));
60 result.set("routes6", json(service.getRoutes(new RouteTableId("ipv6"))));
Jonathan Hartb2111342016-04-20 16:57:37 -070061 print("%s", result);
62 } else {
Charles Chan32a31aa2018-04-11 11:10:42 -040063 print("B: Best route, R: Resolved route\n");
Jonathan Hart96c146b2017-02-24 16:32:00 -080064 service.getRouteTables().forEach(id -> {
Jonathan Hart96c146b2017-02-24 16:32:00 -080065 Collection<RouteInfo> tableRoutes = service.getRoutes(id);
66
Charles Chan92ca94d2017-03-17 18:05:22 -070067 String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ?
68 FORMAT_ROUTE6 : FORMAT_ROUTE;
69
70 // Print header
71 print(FORMAT_TABLE, id);
Charles Chan32a31aa2018-04-11 11:10:42 -040072 print(format, "B", "R", NETWORK, NEXTHOP, SOURCE, NODE);
Charles Chan92ca94d2017-03-17 18:05:22 -070073
74 // Print routing entries
Jonathan Hart96c146b2017-02-24 16:32:00 -080075 tableRoutes.stream()
76 .sorted(Comparator.comparing(r -> r.prefix().address()))
Charles Chan92ca94d2017-03-17 18:05:22 -070077 .forEach(route -> this.print(format, route));
Jonathan Hart96c146b2017-02-24 16:32:00 -080078
79 print(FORMAT_TOTAL, tableRoutes.size());
Jonathan Hartb2111342016-04-20 16:57:37 -070080 print("");
81 });
82 }
Jonathan Hart96c146b2017-02-24 16:32:00 -080083 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070084
Charles Chan92ca94d2017-03-17 18:05:22 -070085 private void print(String format, RouteInfo routeInfo) {
Jonathan Hart58ea8f22017-05-08 15:09:31 -070086 routeInfo.allRoutes().stream()
Charles Chan32a31aa2018-04-11 11:10:42 -040087 .sorted(Comparator.comparing(ResolvedRoute::nextHop))
88 .forEach(r -> print(format,
89 isBestRoute(routeInfo.bestRoute(), r) ? ">" : "",
90 isResolvedRoute(r) ? "*" : "",
Jonathan Hart10dbafd2017-05-18 15:53:03 -070091 r.prefix(), r.nextHop(), r.route().source(), r.route().sourceNode()));
Jonathan Hart96c146b2017-02-24 16:32:00 -080092 }
93
Jonathan Hart96c146b2017-02-24 16:32:00 -080094 private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) {
95 return Objects.equals(bestRoute.orElse(null), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -070096 }
97
Charles Chan32a31aa2018-04-11 11:10:42 -040098 private boolean isResolvedRoute(ResolvedRoute route) {
99 return route.nextHopMac() != null && route.nextHopVlan() != null;
100 }
101
Jonathan Hartb2111342016-04-20 16:57:37 -0700102 /**
103 * Produces a JSON array of routes.
104 *
105 * @param routes the routes with the data
106 * @return JSON array with the routes
107 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800108 private JsonNode json(Collection<RouteInfo> routes) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700109 ObjectMapper mapper = new ObjectMapper();
110 ArrayNode result = mapper.createArrayNode();
111
Jonathan Hart96c146b2017-02-24 16:32:00 -0800112 routes.stream()
113 .flatMap(ri -> ri.allRoutes().stream())
114 .forEach(r -> result.add(json(mapper, r)));
115
Jonathan Hartb2111342016-04-20 16:57:37 -0700116 return result;
117 }
118
119 /**
120 * Produces JSON object for a route.
121 *
122 * @param mapper the JSON object mapper to use
123 * @param route the route with the data
124 * @return JSON object for the route
125 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800126 private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700127 ObjectNode result = mapper.createObjectNode();
128
129 result.put("prefix", route.prefix().toString());
130 result.put("nextHop", route.nextHop().toString());
131
132 return result;
133 }
134
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700135}