Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 3 | * |
| 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 Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 16 | package org.onosproject.routeservice.cli; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 24 | import org.onosproject.routeservice.ResolvedRoute; |
| 25 | import org.onosproject.routeservice.RouteInfo; |
| 26 | import org.onosproject.routeservice.RouteService; |
| 27 | import org.onosproject.routeservice.RouteTableId; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.Collection; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 30 | import java.util.Comparator; |
| 31 | import java.util.Objects; |
| 32 | import java.util.Optional; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Command to show the routes in the routing tables. |
| 36 | */ |
Jonathan Hart | 92ca5d3 | 2016-04-14 18:05:52 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "routes", |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 38 | description = "Lists routes in the route store") |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 39 | public class RoutesListCommand extends AbstractShellCommand { |
| 40 | |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 41 | private static final String NETWORK = "Network"; |
| 42 | private static final String NEXTHOP = "Next Hop"; |
| 43 | private static final String SOURCE = "Source"; |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 44 | private static final String NODE = "Node"; |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 45 | |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 46 | private static final String FORMAT_ROUTE = "%-1s %-18s %-15s %s (%s)"; |
| 47 | private static final String FORMAT_ROUTE6 = "%-1s %-43s %-39s %s (%s)"; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 48 | |
| 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 Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 56 | if (outputJson()) { |
| 57 | ObjectMapper mapper = new ObjectMapper(); |
| 58 | ObjectNode result = mapper.createObjectNode(); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 59 | result.set("routes4", json(service.getRoutes(new RouteTableId("ipv4")))); |
| 60 | result.set("routes6", json(service.getRoutes(new RouteTableId("ipv6")))); |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 61 | print("%s", result); |
| 62 | } else { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 63 | service.getRouteTables().forEach(id -> { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 64 | Collection<RouteInfo> tableRoutes = service.getRoutes(id); |
| 65 | |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 66 | String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ? |
| 67 | FORMAT_ROUTE6 : FORMAT_ROUTE; |
| 68 | |
| 69 | // Print header |
| 70 | print(FORMAT_TABLE, id); |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 71 | print(format, "", NETWORK, NEXTHOP, SOURCE, NODE); |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 72 | |
| 73 | // Print routing entries |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 74 | tableRoutes.stream() |
| 75 | .sorted(Comparator.comparing(r -> r.prefix().address())) |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 76 | .forEach(route -> this.print(format, route)); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 77 | |
| 78 | print(FORMAT_TOTAL, tableRoutes.size()); |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 79 | print(""); |
| 80 | }); |
| 81 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 82 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 83 | |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 84 | private void print(String format, RouteInfo routeInfo) { |
Jonathan Hart | 58ea8f2 | 2017-05-08 15:09:31 -0700 | [diff] [blame] | 85 | routeInfo.allRoutes().stream() |
| 86 | .sorted(Comparator.comparing(r -> r.nextHop())) |
Charles Chan | 92ca94d | 2017-03-17 18:05:22 -0700 | [diff] [blame] | 87 | .forEach(r -> print(format, isBestRoute(routeInfo.bestRoute(), r) ? ">" : "", |
Jonathan Hart | 10dbafd | 2017-05-18 15:53:03 -0700 | [diff] [blame] | 88 | r.prefix(), r.nextHop(), r.route().source(), r.route().sourceNode())); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 91 | private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) { |
| 92 | return Objects.equals(bestRoute.orElse(null), route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 95 | /** |
| 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 Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 101 | private JsonNode json(Collection<RouteInfo> routes) { |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 102 | ObjectMapper mapper = new ObjectMapper(); |
| 103 | ArrayNode result = mapper.createArrayNode(); |
| 104 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 105 | routes.stream() |
| 106 | .flatMap(ri -> ri.allRoutes().stream()) |
| 107 | .forEach(r -> result.add(json(mapper, r))); |
| 108 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 109 | 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 Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 119 | private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) { |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 120 | 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 Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 128 | } |