Committed by
Gerrit Code Review
Add JSON output for routes CLI command
Change-Id: If4901d58da3130bf30439205e4e87cb1b1e157ad
Showing
1 changed file
with
51 additions
and
7 deletions
| ... | @@ -15,6 +15,10 @@ | ... | @@ -15,6 +15,10 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.cli.net; | 16 | package org.onosproject.cli.net; |
| 17 | 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; | ||
| 18 | import org.apache.karaf.shell.commands.Command; | 22 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.onosproject.cli.AbstractShellCommand; | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 20 | import org.onosproject.incubator.net.routing.Route; | 24 | import org.onosproject.incubator.net.routing.Route; |
| ... | @@ -45,14 +49,54 @@ public class RoutesListCommand extends AbstractShellCommand { | ... | @@ -45,14 +49,54 @@ public class RoutesListCommand extends AbstractShellCommand { |
| 45 | 49 | ||
| 46 | Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes(); | 50 | Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes(); |
| 47 | 51 | ||
| 48 | - allRoutes.forEach((id, routes) -> { | 52 | + if (outputJson()) { |
| 49 | - print(FORMAT_TABLE, id); | 53 | + ObjectMapper mapper = new ObjectMapper(); |
| 50 | - print(FORMAT_HEADER); | 54 | + ObjectNode result = mapper.createObjectNode(); |
| 51 | - routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop())); | 55 | + result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4")))); |
| 52 | - print(FORMAT_TOTAL, routes.size()); | 56 | + result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6")))); |
| 53 | - print(""); | 57 | + print("%s", result); |
| 54 | - }); | 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 | + } | ||
| 55 | 67 | ||
| 56 | } | 68 | } |
| 57 | 69 | ||
| 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 | + | ||
| 58 | } | 102 | } | ... | ... |
-
Please register or login to post a comment