Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.cli; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 17 | |
| 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 | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 25 | import org.onosproject.routing.RouteEntry; |
| 26 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 27 | |
| 28 | import java.util.Collection; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 31 | * Command to show the routes in the routing table. |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 32 | */ |
| 33 | @Command(scope = "onos", name = "routes", |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 34 | description = "Lists all routes in the RIB") |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 35 | public class RoutesListCommand extends AbstractShellCommand { |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 36 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 37 | @Option(name = "-s", aliases = "--summary", |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 38 | description = "Show summary of routes", |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 39 | required = false, multiValued = false) |
| 40 | private boolean routesSummary = false; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 41 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 42 | private static final String FORMAT_SUMMARY_V4 = |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 43 | "Total IPv4 routes = %d"; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 44 | private static final String FORMAT_SUMMARY_V6 = |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 45 | "Total IPv6 routes = %d"; |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 46 | private static final String FORMAT_HEADER = |
| 47 | " Network Next Hop"; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 48 | private static final String FORMAT_ROUTE = |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 49 | " %-18s %-15s"; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 50 | |
| 51 | @Override |
| 52 | protected void execute() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 53 | RoutingService service = AbstractShellCommand.get(RoutingService.class); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 54 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 55 | // Print summary of the routes |
| 56 | if (routesSummary) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 57 | printSummary(service.getRoutes4(), service.getRoutes6()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
| 61 | // Print all routes |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 62 | printRoutes(service.getRoutes4(), service.getRoutes6()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Prints summary of the routes. |
| 67 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 68 | * @param routes4 the IPv4 routes |
| 69 | * @param routes6 the IPv6 routes |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 70 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 71 | private void printSummary(Collection<RouteEntry> routes4, |
| 72 | Collection<RouteEntry> routes6) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 73 | if (outputJson()) { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ObjectNode result = mapper.createObjectNode(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 76 | result.put("totalRoutes4", routes4.size()); |
| 77 | result.put("totalRoutes6", routes6.size()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 78 | print("%s", result); |
| 79 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 80 | print(FORMAT_SUMMARY_V4, routes4.size()); |
| 81 | print(FORMAT_SUMMARY_V6, routes6.size()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Prints all routes. |
| 87 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 88 | * @param routes4 the IPv4 routes to print |
| 89 | * @param routes6 the IPv6 routes to print |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 90 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 91 | private void printRoutes(Collection<RouteEntry> routes4, |
| 92 | Collection<RouteEntry> routes6) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 93 | if (outputJson()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 94 | ObjectMapper mapper = new ObjectMapper(); |
| 95 | ObjectNode result = mapper.createObjectNode(); |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 96 | result.set("routes4", json(routes4)); |
| 97 | result.set("routes6", json(routes6)); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 98 | print("%s", result); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 99 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 100 | // The IPv4 routes |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 101 | print(FORMAT_HEADER); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 102 | for (RouteEntry route : routes4) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 103 | printRoute(route); |
| 104 | } |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 105 | print(FORMAT_SUMMARY_V4, routes4.size()); |
| 106 | print(""); // Empty separator line |
| 107 | // The IPv6 routes |
| 108 | print(FORMAT_HEADER); |
| 109 | for (RouteEntry route : routes6) { |
| 110 | printRoute(route); |
| 111 | } |
| 112 | print(FORMAT_SUMMARY_V6, routes6.size()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Prints a route. |
| 118 | * |
| 119 | * @param route the route to print |
| 120 | */ |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 121 | private void printRoute(RouteEntry route) { |
| 122 | if (route != null) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 123 | print(FORMAT_ROUTE, route.prefix(), route.nextHop()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Produces a JSON array of routes. |
| 129 | * |
| 130 | * @param routes the routes with the data |
| 131 | * @return JSON array with the routes |
| 132 | */ |
| 133 | private JsonNode json(Collection<RouteEntry> routes) { |
| 134 | ObjectMapper mapper = new ObjectMapper(); |
| 135 | ArrayNode result = mapper.createArrayNode(); |
| 136 | |
| 137 | for (RouteEntry route : routes) { |
| 138 | result.add(json(mapper, route)); |
| 139 | } |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Produces JSON object for a route. |
| 145 | * |
| 146 | * @param mapper the JSON object mapper to use |
| 147 | * @param route the route with the data |
| 148 | * @return JSON object for the route |
| 149 | */ |
| 150 | private ObjectNode json(ObjectMapper mapper, RouteEntry route) { |
| 151 | ObjectNode result = mapper.createObjectNode(); |
| 152 | |
| 153 | result.put("prefix", route.prefix().toString()); |
| 154 | result.put("nextHop", route.nextHop().toString()); |
| 155 | |
| 156 | return result; |
| 157 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 158 | } |