Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | */ |
| 16 | package org.onosproject.cli.net; |
| 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; |
Jonathan Hart | f54c3e7 | 2016-04-21 18:08:44 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.incubator.net.routing.Route; |
| 26 | import org.onosproject.incubator.net.routing.RouteService; |
| 27 | import org.onosproject.incubator.net.routing.RouteTableId; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | /** |
| 33 | * Command to show the routes in the routing tables. |
| 34 | */ |
Jonathan Hart | 92ca5d3 | 2016-04-14 18:05:52 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "routes", |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 36 | description = "Lists all routes in the route store") |
| 37 | public class RoutesListCommand extends AbstractShellCommand { |
| 38 | |
Jonathan Hart | f54c3e7 | 2016-04-21 18:08:44 -0700 | [diff] [blame] | 39 | @Option(name = "-s", aliases = "--summary", |
| 40 | description = "Show summary of routes") |
| 41 | private boolean summary = false; |
| 42 | |
| 43 | private static final String FORMAT_SUMMARY = |
| 44 | "Number of routes in table %s: %s"; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 45 | private static final String FORMAT_HEADER = |
Charles Chan | 218b9f3 | 2016-12-06 20:38:27 -0800 | [diff] [blame] | 46 | " Network Next Hop Source"; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 47 | private static final String FORMAT_ROUTE = |
Charles Chan | 218b9f3 | 2016-12-06 20:38:27 -0800 | [diff] [blame] | 48 | " %-18s %-15s %-10s"; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 49 | |
| 50 | private static final String FORMAT_TABLE = "Table: %s"; |
| 51 | private static final String FORMAT_TOTAL = " Total: %d"; |
| 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
| 55 | RouteService service = AbstractShellCommand.get(RouteService.class); |
| 56 | |
| 57 | Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes(); |
| 58 | |
Jonathan Hart | f54c3e7 | 2016-04-21 18:08:44 -0700 | [diff] [blame] | 59 | if (summary) { |
| 60 | if (outputJson()) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ObjectNode result = mapper.createObjectNode(); |
| 63 | result.put("totalRoutes4", allRoutes.get(new RouteTableId("ipv4")).size()); |
| 64 | result.put("totalRoutes6", allRoutes.get(new RouteTableId("ipv6")).size()); |
| 65 | print("%s", result); |
| 66 | } else { |
| 67 | allRoutes.forEach((id, routes) -> print(FORMAT_SUMMARY, id, routes.size())); |
| 68 | } |
| 69 | |
| 70 | return; |
| 71 | } |
| 72 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 73 | if (outputJson()) { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ObjectNode result = mapper.createObjectNode(); |
| 76 | result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4")))); |
| 77 | result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6")))); |
| 78 | print("%s", result); |
| 79 | } else { |
| 80 | allRoutes.forEach((id, routes) -> { |
| 81 | print(FORMAT_TABLE, id); |
| 82 | print(FORMAT_HEADER); |
Charles Chan | 218b9f3 | 2016-12-06 20:38:27 -0800 | [diff] [blame] | 83 | routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop(), r.source())); |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 84 | print(FORMAT_TOTAL, routes.size()); |
| 85 | print(""); |
| 86 | }); |
| 87 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 88 | |
| 89 | } |
| 90 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Produces a JSON array of routes. |
| 93 | * |
| 94 | * @param routes the routes with the data |
| 95 | * @return JSON array with the routes |
| 96 | */ |
| 97 | private JsonNode json(Collection<Route> routes) { |
| 98 | ObjectMapper mapper = new ObjectMapper(); |
| 99 | ArrayNode result = mapper.createArrayNode(); |
| 100 | |
| 101 | for (Route route : routes) { |
| 102 | result.add(json(mapper, route)); |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Produces JSON object for a route. |
| 109 | * |
| 110 | * @param mapper the JSON object mapper to use |
| 111 | * @param route the route with the data |
| 112 | * @return JSON object for the route |
| 113 | */ |
| 114 | private ObjectNode json(ObjectMapper mapper, Route route) { |
| 115 | ObjectNode result = mapper.createObjectNode(); |
| 116 | |
| 117 | result.put("prefix", route.prefix().toString()); |
| 118 | result.put("nextHop", route.nextHop().toString()); |
| 119 | |
| 120 | return result; |
| 121 | } |
| 122 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 123 | } |