blob: 1cd78e2562ab66f931bc88394165ad674ff35638 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip.cli;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070017
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080018import java.util.Collection;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070024import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080025import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.sdnip.RouteEntry;
28import org.onosproject.sdnip.SdnIpService;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070029
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")
35public class RoutesListCommand extends AbstractShellCommand {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080036 @Option(name = "-s", aliases = "--summary",
37 description = "SDN-IP routes summary",
38 required = false, multiValued = false)
39 private boolean routesSummary = false;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070040
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080041 private static final String FORMAT_SUMMARY = "Total SDN-IP routes = %d";
42 private static final String FORMAT_ROUTE =
Jonathan Hart0b04bed2014-10-16 16:39:19 -070043 "prefix=%s, nexthop=%s";
44
45 @Override
46 protected void execute() {
47 SdnIpService service = get(SdnIpService.class);
48
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080049 // 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 Hart0b04bed2014-10-16 16:39:19 -070072 }
73 }
74
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080075 /**
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 Hart0b04bed2014-10-16 16:39:19 -070095 private void printRoute(RouteEntry route) {
96 if (route != null) {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080097 print(FORMAT_ROUTE, route.prefix(), route.nextHop());
Jonathan Hart0b04bed2014-10-16 16:39:19 -070098 }
99 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800100
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 Hart0b04bed2014-10-16 16:39:19 -0700132}