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 | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip.cli; |
| 17 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 26 | import org.onlab.onos.cli.AbstractShellCommand; |
| 27 | import org.onlab.onos.sdnip.RouteEntry; |
| 28 | import org.onlab.onos.sdnip.SdnIpService; |
| 29 | |
| 30 | /** |
| 31 | * Command to show the list of routes in SDN-IP's routing table. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "routes", |
| 34 | description = "Lists all routes known to SDN-IP") |
| 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 | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 41 | private static final String FORMAT_SUMMARY = "Total SDN-IP routes = %d"; |
| 42 | private static final String FORMAT_ROUTE = |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 43 | "prefix=%s, nexthop=%s"; |
| 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | SdnIpService service = get(SdnIpService.class); |
| 48 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 49 | // Print summary of the routes |
| 50 | if (routesSummary) { |
| 51 | printSummary(service.getRoutes()); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Print all routes |
| 56 | printRoutes(service.getRoutes()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Prints summary of the routes. |
| 61 | * |
| 62 | * @param routes the routes |
| 63 | */ |
| 64 | private void printSummary(Collection<RouteEntry> routes) { |
| 65 | if (outputJson()) { |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ObjectNode result = mapper.createObjectNode(); |
| 68 | result.put("totalRoutes", routes.size()); |
| 69 | print("%s", result); |
| 70 | } else { |
| 71 | print(FORMAT_SUMMARY, routes.size()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Prints all routes. |
| 77 | * |
| 78 | * @param routes the routes to print |
| 79 | */ |
| 80 | private void printRoutes(Collection<RouteEntry> routes) { |
| 81 | if (outputJson()) { |
| 82 | print("%s", json(routes)); |
| 83 | } else { |
| 84 | for (RouteEntry route : routes) { |
| 85 | printRoute(route); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Prints a route. |
| 92 | * |
| 93 | * @param route the route to print |
| 94 | */ |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 95 | private void printRoute(RouteEntry route) { |
| 96 | if (route != null) { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 97 | print(FORMAT_ROUTE, route.prefix(), route.nextHop()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Produces a JSON array of routes. |
| 103 | * |
| 104 | * @param routes the routes with the data |
| 105 | * @return JSON array with the routes |
| 106 | */ |
| 107 | private JsonNode json(Collection<RouteEntry> routes) { |
| 108 | ObjectMapper mapper = new ObjectMapper(); |
| 109 | ArrayNode result = mapper.createArrayNode(); |
| 110 | |
| 111 | for (RouteEntry route : routes) { |
| 112 | result.add(json(mapper, route)); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Produces JSON object for a route. |
| 119 | * |
| 120 | * @param mapper the JSON object mapper to use |
| 121 | * @param route the route with the data |
| 122 | * @return JSON object for the route |
| 123 | */ |
| 124 | private ObjectNode json(ObjectMapper mapper, RouteEntry route) { |
| 125 | ObjectNode result = mapper.createObjectNode(); |
| 126 | |
| 127 | result.put("prefix", route.prefix().toString()); |
| 128 | result.put("nextHop", route.nextHop().toString()); |
| 129 | |
| 130 | return result; |
| 131 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 132 | } |