blob: f32ecd5c6caed4592bff3f046e845824337a1923 [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 Hartbfc5c482016-04-05 18:57:00 -070025import org.onosproject.incubator.net.routing.Route;
Jonathan Hart96c146b2017-02-24 16:32:00 -080026import org.onosproject.incubator.net.routing.RouteInfo;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070027import org.onosproject.incubator.net.routing.RouteService;
28import org.onosproject.incubator.net.routing.RouteTableId;
29
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 */
Jonathan Hart92ca5d32016-04-14 18:05:52 -070038@Command(scope = "onos", name = "routes",
Jonathan Hart96c146b2017-02-24 16:32:00 -080039 description = "Lists routes in the route store")
Jonathan Hartbfc5c482016-04-05 18:57:00 -070040public class RoutesListCommand extends AbstractShellCommand {
41
Charles Chan92ca94d2017-03-17 18:05:22 -070042 private static final String NETWORK = "Network";
43 private static final String NEXTHOP = "Next Hop";
44 private static final String SOURCE = "Source";
45
46 private static final String FORMAT_ROUTE = "%-1s %-18s %-15s %-10s";
47 private static final String FORMAT_ROUTE6 = "%-1s %-43s %-39s %-10s";
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
53 protected void execute() {
54 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 {
Jonathan Hart96c146b2017-02-24 16:32:00 -080063 service.getRouteTables().forEach(id -> {
Jonathan Hart96c146b2017-02-24 16:32:00 -080064 Collection<RouteInfo> tableRoutes = service.getRoutes(id);
65
Charles Chan92ca94d2017-03-17 18:05:22 -070066 String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ?
67 FORMAT_ROUTE6 : FORMAT_ROUTE;
68
69 // Print header
70 print(FORMAT_TABLE, id);
71 print(format, "", NETWORK, NEXTHOP, SOURCE);
72
73 // Print routing entries
Jonathan Hart96c146b2017-02-24 16:32:00 -080074 tableRoutes.stream()
75 .sorted(Comparator.comparing(r -> r.prefix().address()))
Charles Chan92ca94d2017-03-17 18:05:22 -070076 .forEach(route -> this.print(format, route));
Jonathan Hart96c146b2017-02-24 16:32:00 -080077
78 print(FORMAT_TOTAL, tableRoutes.size());
Jonathan Hartb2111342016-04-20 16:57:37 -070079 print("");
80 });
81 }
Jonathan Hart96c146b2017-02-24 16:32:00 -080082 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -070083
Charles Chan92ca94d2017-03-17 18:05:22 -070084 private void print(String format, RouteInfo routeInfo) {
Jonathan Hart96c146b2017-02-24 16:32:00 -080085 routeInfo.allRoutes()
Charles Chan92ca94d2017-03-17 18:05:22 -070086 .forEach(r -> print(format, isBestRoute(routeInfo.bestRoute(), r) ? ">" : "",
Jonathan Hart96c146b2017-02-24 16:32:00 -080087 r.prefix(), r.nextHop(), Route.Source.UNDEFINED));
88 }
89
90
91
92 private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) {
93 return Objects.equals(bestRoute.orElse(null), route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -070094 }
95
Jonathan Hartb2111342016-04-20 16:57:37 -070096 /**
97 * Produces a JSON array of routes.
98 *
99 * @param routes the routes with the data
100 * @return JSON array with the routes
101 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800102 private JsonNode json(Collection<RouteInfo> routes) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700103 ObjectMapper mapper = new ObjectMapper();
104 ArrayNode result = mapper.createArrayNode();
105
Jonathan Hart96c146b2017-02-24 16:32:00 -0800106 routes.stream()
107 .flatMap(ri -> ri.allRoutes().stream())
108 .forEach(r -> result.add(json(mapper, r)));
109
Jonathan Hartb2111342016-04-20 16:57:37 -0700110 return result;
111 }
112
113 /**
114 * Produces JSON object for a route.
115 *
116 * @param mapper the JSON object mapper to use
117 * @param route the route with the data
118 * @return JSON object for the route
119 */
Jonathan Hart96c146b2017-02-24 16:32:00 -0800120 private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) {
Jonathan Hartb2111342016-04-20 16:57:37 -0700121 ObjectNode result = mapper.createObjectNode();
122
123 result.put("prefix", route.prefix().toString());
124 result.put("nextHop", route.nextHop().toString());
125
126 return result;
127 }
128
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700129}