blob: e2cb7147be741c7279e38c264f1ad5592b164a58 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070024import org.onosproject.cli.AbstractShellCommand;
Ray Milkey69ec8712017-08-08 13:00:43 -070025import org.onosproject.routeservice.ResolvedRoute;
26import org.onosproject.routeservice.RouteInfo;
27import org.onosproject.routeservice.RouteService;
28import org.onosproject.routeservice.RouteTableId;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070029
30import java.util.Collection;
Jonathan Hart96c146b2017-02-24 16:32:00 -080031import java.util.Comparator;
32import java.util.Objects;
33import java.util.Optional;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070034
35/**
36 * Command to show the routes in the routing tables.
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Jonathan Hart92ca5d32016-04-14 18:05:52 -070039@Command(scope = "onos", name = "routes",
Jonathan Hart96c146b2017-02-24 16:32:00 -080040 description = "Lists routes in the route store")
Jonathan Hartbfc5c482016-04-05 18:57:00 -070041public class RoutesListCommand extends AbstractShellCommand {
42
Charles Chan92ca94d2017-03-17 18:05:22 -070043 private static final String NETWORK = "Network";
44 private static final String NEXTHOP = "Next Hop";
45 private static final String SOURCE = "Source";
Jonathan Hart10dbafd2017-05-18 15:53:03 -070046 private static final String NODE = "Node";
Charles Chan92ca94d2017-03-17 18:05:22 -070047
Charles Chan32a31aa2018-04-11 11:10:42 -040048 private static final String FORMAT_ROUTE = "%-1s %-1s %-18s %-15s %s (%s)";
49 private static final String FORMAT_ROUTE6 = "%-1s %-1s %-43s %-39s %s (%s)";
Jonathan Hartbfc5c482016-04-05 18:57:00 -070050
51 private static final String FORMAT_TABLE = "Table: %s";
52 private static final String FORMAT_TOTAL = " Total: %d";
53
54 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070056 RouteService service = AbstractShellCommand.get(RouteService.class);
57
Jonathan Hartb2111342016-04-20 16:57:37 -070058 if (outputJson()) {
59 ObjectMapper mapper = new ObjectMapper();
60 ObjectNode result = mapper.createObjectNode();
Jonathan Hart96c146b2017-02-24 16:32:00 -080061 result.set("routes4", json(service.getRoutes(new RouteTableId("ipv4"))));
62 result.set("routes6", json(service.getRoutes(new RouteTableId("ipv6"))));
Jonathan Hartb2111342016-04-20 16:57:37 -070063 print("%s", result);
64 } else {
Charles Chan32a31aa2018-04-11 11:10:42 -040065 print("B: Best route, R: Resolved route\n");
Jonathan Hart96c146b2017-02-24 16:32:00 -080066 service.getRouteTables().forEach(id -> {
Jonathan Hart96c146b2017-02-24 16:32:00 -080067 Collection<RouteInfo> tableRoutes = service.getRoutes(id);
68
Charles Chan92ca94d2017-03-17 18:05:22 -070069 String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ?
70 FORMAT_ROUTE6 : FORMAT_ROUTE;
71
72 // Print header
73 print(FORMAT_TABLE, id);
Charles Chan32a31aa2018-04-11 11:10:42 -040074 print(format, "B", "R", NETWORK, NEXTHOP, SOURCE, NODE);
Charles Chan92ca94d2017-03-17 18:05:22 -070075
76 // Print routing entries
Jonathan Hart96c146b2017-02-24 16:32:00 -080077 tableRoutes.stream()
78 .sorted(Comparator.comparing(r -> r.prefix().address()))
Charles Chan92ca94d2017-03-17 18:05:22 -070079 .forEach(route -> this.print(format, route));
Jonathan Hart96c146b2017-02-24 16:32:00 -080080
81 print(FORMAT_TOTAL, tableRoutes.size());
Jonathan Hartb2111342016-04-20 16:57:37 -070082 print("");
83 });
84 }
Jonathan Hart96c146b2017-02-24 16:32:00 -080085 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070086
Charles Chan92ca94d2017-03-17 18:05:22 -070087 private void print(String format, RouteInfo routeInfo) {
Jonathan Hart58ea8f22017-05-08 15:09:31 -070088 routeInfo.allRoutes().stream()
Charles Chan32a31aa2018-04-11 11:10:42 -040089 .sorted(Comparator.comparing(ResolvedRoute::nextHop))
90 .forEach(r -> print(format,
91 isBestRoute(routeInfo.bestRoute(), r) ? ">" : "",
92 isResolvedRoute(r) ? "*" : "",
Jonathan Hart10dbafd2017-05-18 15:53:03 -070093 r.prefix(), r.nextHop(), r.route().source(), r.route().sourceNode()));
Jonathan Hart96c146b2017-02-24 16:32:00 -080094 }
95
Jonathan Hart96c146b2017-02-24 16:32:00 -080096 private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) {
97 return Objects.equals(bestRoute.orElse(null), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -070098 }
99
Charles Chan32a31aa2018-04-11 11:10:42 -0400100 private boolean isResolvedRoute(ResolvedRoute route) {
101 return route.nextHopMac() != null && route.nextHopVlan() != null;
102 }
103
Jonathan Hartb2111342016-04-20 16:57:37 -0700104 /**
105 * Produces a JSON array of routes.
106 *
107 * @param routes the routes with the data
108 * @return JSON array with the routes
109 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800110 private JsonNode json(Collection<RouteInfo> routes) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700111 ObjectMapper mapper = new ObjectMapper();
112 ArrayNode result = mapper.createArrayNode();
113
Jonathan Hart96c146b2017-02-24 16:32:00 -0800114 routes.stream()
115 .flatMap(ri -> ri.allRoutes().stream())
116 .forEach(r -> result.add(json(mapper, r)));
117
Jonathan Hartb2111342016-04-20 16:57:37 -0700118 return result;
119 }
120
121 /**
122 * Produces JSON object for a route.
123 *
124 * @param mapper the JSON object mapper to use
125 * @param route the route with the data
126 * @return JSON object for the route
127 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800128 private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700129 ObjectNode result = mapper.createObjectNode();
130
131 result.put("prefix", route.prefix().toString());
132 result.put("nextHop", route.nextHop().toString());
133
134 return result;
135 }
136
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700137}