/*
 * Copyright 2016-present Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.cli.net;

import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.incubator.net.routing.Route;
import org.onosproject.incubator.net.routing.RouteService;
import org.onosproject.incubator.net.routing.RouteTableId;

import java.util.Collection;
import java.util.Map;

/**
 * Command to show the routes in the routing tables.
 */
@Command(scope = "onos", name = "routes",
        description = "Lists all routes in the route store")
public class RoutesListCommand extends AbstractShellCommand {

    private static final String FORMAT_HEADER =
        "   Network            Next Hop";
    private static final String FORMAT_ROUTE =
        "   %-18s %-15s";

    private static final String FORMAT_TABLE = "Table: %s";
    private static final String FORMAT_TOTAL = "   Total: %d";

    @Override
    protected void execute() {
        RouteService service = AbstractShellCommand.get(RouteService.class);

        Map<RouteTableId, Collection<Route>> allRoutes = service.getAllRoutes();

        allRoutes.forEach((id, routes) -> {
            print(FORMAT_TABLE, id);
            print(FORMAT_HEADER);
            routes.forEach(r -> print(FORMAT_ROUTE, r.prefix(), r.nextHop()));
            print(FORMAT_TOTAL, routes.size());
            print("");
        });

    }

}

@@ -645,16 +645,225 @@
 i.net;%0A%0A
+import com.fasterxml.jackson.databind.JsonNode;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.node.ArrayNode;%0Aimport com.fasterxml.jackson.databind.node.ObjectNode;%0A
 import o
@@ -894,32 +894,32 @@
 mmands.Command;%0A
-
 import org.onosp
@@ -1890,24 +1890,383 @@
 lRoutes();%0A%0A
+        if (outputJson()) %7B%0A            ObjectMapper mapper = new ObjectMapper();%0A            ObjectNode result = mapper.createObjectNode();%0A            result.set(%22routes4%22, json(allRoutes.get(new RouteTableId(%22ipv4%22))));%0A            result.set(%22routes6%22, json(allRoutes.get(new RouteTableId(%22ipv6%22))));%0A            print(%22%25s%22, result);%0A        %7D else %7B%0A    
         allR
@@ -2301,32 +2301,36 @@
 %3E %7B%0A            
+    
 print(FORMAT_TAB
@@ -2342,32 +2342,36 @@
 d);%0A            
+ 
+   
 print(FORMAT_HEA
@@ -2376,16 +2376,20 @@
 EADER);%0A
+    
         
@@ -2463,32 +2463,36 @@
 ));%0A            
+   
+ 
 print(FORMAT_TOT
@@ -2519,24 +2519,28 @@
             
+    
 print(%22%22);%0A 
@@ -2550,12 +2550,924 @@
     
-%7D);%0A
+    %7D);%0A        %7D%0A%0A    %7D%0A%0A    /**%0A     * Produces a JSON array of routes.%0A     *%0A     * @param routes the routes with the data%0A     * @return JSON array with the routes%0A     */%0A    private JsonNode json(Collection%3CRoute%3E routes) %7B%0A        ObjectMapper mapper = new ObjectMapper();%0A        ArrayNode result = mapper.createArrayNode();%0A%0A        for (Route route : routes) %7B%0A            result.add(json(mapper, route));%0A        %7D%0A        return result;%0A    %7D%0A%0A    /**%0A     * Produces JSON object for a route.%0A     *%0A     * @param mapper the JSON object mapper to use%0A     * @param route the route with the data%0A     * @return JSON object for the route%0A     */%0A    private ObjectNode json(ObjectMapper mapper, Route route) %7B%0A        ObjectNode result = mapper.createObjectNode();%0A%0A        result.put(%22prefix%22, route.prefix().toString());%0A        result.put(%22nextHop%22, route.nextHop().toString());%0A%0A        return result;
 %0A   

Add JSON output for routes CLI command