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; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.incubator.net.routing.Route; |
| 25 | import org.onosproject.incubator.net.routing.RouteService; |
| 26 | import org.onosproject.incubator.net.routing.RouteTableId; |
| 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | /** |
| 32 | * Command to show the routes in the routing tables. |
| 33 | */ |
Jonathan Hart | 92ca5d3 | 2016-04-14 18:05:52 -0700 | [diff] [blame] | 34 | @Command(scope = "onos", name = "routes", |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 35 | description = "Lists all routes in the route store") |
| 36 | public class RoutesListCommand extends AbstractShellCommand { |
| 37 | |
| 38 | private static final String FORMAT_HEADER = |
| 39 | " Network Next Hop"; |
| 40 | private static final String FORMAT_ROUTE = |
| 41 | " %-18s %-15s"; |
| 42 | |
| 43 | private static final String FORMAT_TABLE = "Table: %s"; |
| 44 | private static final String FORMAT_TOTAL = " Total: %d"; |
| 45 | |
| 46 | @Override |
| 47 | protected void execute() { |
| 48 | RouteService service = AbstractShellCommand.get(RouteService.class); |
| 49 | |
| 50 | Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes(); |
| 51 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 52 | if (outputJson()) { |
| 53 | ObjectMapper mapper = new ObjectMapper(); |
| 54 | ObjectNode result = mapper.createObjectNode(); |
| 55 | result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4")))); |
| 56 | result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6")))); |
| 57 | print("%s", result); |
| 58 | } else { |
| 59 | allRoutes.forEach((id, routes) -> { |
| 60 | print(FORMAT_TABLE, id); |
| 61 | print(FORMAT_HEADER); |
| 62 | routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop())); |
| 63 | print(FORMAT_TOTAL, routes.size()); |
| 64 | print(""); |
| 65 | }); |
| 66 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 67 | |
| 68 | } |
| 69 | |
Jonathan Hart | b211134 | 2016-04-20 16:57:37 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Produces a JSON array of routes. |
| 72 | * |
| 73 | * @param routes the routes with the data |
| 74 | * @return JSON array with the routes |
| 75 | */ |
| 76 | private JsonNode json(Collection<Route> routes) { |
| 77 | ObjectMapper mapper = new ObjectMapper(); |
| 78 | ArrayNode result = mapper.createArrayNode(); |
| 79 | |
| 80 | for (Route route : routes) { |
| 81 | result.add(json(mapper, route)); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Produces JSON object for a route. |
| 88 | * |
| 89 | * @param mapper the JSON object mapper to use |
| 90 | * @param route the route with the data |
| 91 | * @return JSON object for the route |
| 92 | */ |
| 93 | private ObjectNode json(ObjectMapper mapper, Route route) { |
| 94 | ObjectNode result = mapper.createObjectNode(); |
| 95 | |
| 96 | result.put("prefix", route.prefix().toString()); |
| 97 | result.put("nextHop", route.nextHop().toString()); |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 102 | } |