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