Committed by
Gerrit Code Review
Add JSON output for routes CLI command
Change-Id: If4901d58da3130bf30439205e4e87cb1b1e157ad
Showing
1 changed file
with
44 additions
and
0 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,6 +49,13 @@ public class RoutesListCommand extends AbstractShellCommand { | ... | @@ -45,6 +49,13 @@ 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 | ||
| 52 | + if (outputJson()) { | ||
| 53 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 54 | + ObjectNode result = mapper.createObjectNode(); | ||
| 55 | + result.set("routes4", json(allRoutes.get(new RouteTableId("ipv4")))); | ||
| 56 | + result.set("routes6", json(allRoutes.get(new RouteTableId("ipv6")))); | ||
| 57 | + print("%s", result); | ||
| 58 | + } else { | ||
| 48 | allRoutes.forEach((id, routes) -> { | 59 | allRoutes.forEach((id, routes) -> { |
| 49 | print(FORMAT_TABLE, id); | 60 | print(FORMAT_TABLE, id); |
| 50 | print(FORMAT_HEADER); | 61 | print(FORMAT_HEADER); |
| ... | @@ -52,7 +63,40 @@ public class RoutesListCommand extends AbstractShellCommand { | ... | @@ -52,7 +63,40 @@ public class RoutesListCommand extends AbstractShellCommand { |
| 52 | print(FORMAT_TOTAL, routes.size()); | 63 | print(FORMAT_TOTAL, routes.size()); |
| 53 | print(""); | 64 | print(""); |
| 54 | }); | 65 | }); |
| 66 | + } | ||
| 67 | + | ||
| 68 | + } | ||
| 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()); | ||
| 55 | 98 | ||
| 99 | + return result; | ||
| 56 | } | 100 | } |
| 57 | 101 | ||
| 58 | } | 102 | } | ... | ... |
-
Please register or login to post a comment