Move routing from incubator to a separate app

Change-Id: I961d10af99c572b1f8d9b3d37c6f52dd04422007
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteAddCommand.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteAddCommand.java
new file mode 100644
index 0000000..1810a07
--- /dev/null
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteAddCommand.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.routeservice.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.routeservice.Route;
+import org.onosproject.routeservice.RouteAdminService;
+
+import java.util.Collections;
+
+/**
+ * Command to add a route to the routing table.
+ */
+@Command(scope = "onos", name = "route-add",
+        description = "Adds a route to the route table")
+public class RouteAddCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "prefix", description = "IP prefix of the route",
+            required = true)
+    String prefixString = null;
+
+    @Argument(index = 1, name = "nextHop", description = "IP address of the next hop",
+            required = true)
+    String nextHopString = null;
+
+    @Override
+    protected void execute() {
+        RouteAdminService service = AbstractShellCommand.get(RouteAdminService.class);
+
+        IpPrefix prefix = IpPrefix.valueOf(prefixString);
+        IpAddress nextHop = IpAddress.valueOf(nextHopString);
+
+        service.update(Collections.singleton(new Route(Route.Source.STATIC, prefix, nextHop)));
+    }
+
+}
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteRemoveCommand.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteRemoveCommand.java
new file mode 100644
index 0000000..9fef2ec
--- /dev/null
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteRemoveCommand.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.routeservice.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.routeservice.Route;
+import org.onosproject.routeservice.RouteAdminService;
+
+import java.util.Collections;
+
+/**
+ * Command to remove a route from the routing table.
+ */
+@Command(scope = "onos", name = "route-remove",
+        description = "Removes a route from the route table")
+public class RouteRemoveCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "prefix", description = "IP prefix of the route",
+            required = true)
+    String prefixString = null;
+
+    @Argument(index = 1, name = "nextHop", description = "IP address of the next hop",
+            required = true)
+    String nextHopString = null;
+
+    @Override
+    protected void execute() {
+        RouteAdminService service = AbstractShellCommand.get(RouteAdminService.class);
+
+        IpPrefix prefix = IpPrefix.valueOf(prefixString);
+        IpAddress nextHop = IpAddress.valueOf(nextHopString);
+
+        service.withdraw(Collections.singleton(new Route(Route.Source.STATIC, prefix, nextHop)));
+    }
+
+}
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteStoreCommand.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteStoreCommand.java
new file mode 100644
index 0000000..2aefbf7
--- /dev/null
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RouteStoreCommand.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.routeservice.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.routeservice.RouteStore;
+
+/**
+ * Command to show the current route store implementation.
+ */
+@Command(scope = "onos", name = "route-store",
+        description = "Show the current route store implementation.")
+public class RouteStoreCommand extends AbstractShellCommand {
+
+    @Override
+    protected void execute() {
+        RouteStore routeStore = AbstractShellCommand.get(RouteStore.class);
+        print(routeStore.name());
+    }
+}
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RoutesListCommand.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RoutesListCommand.java
new file mode 100644
index 0000000..9c7eae0
--- /dev/null
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/RoutesListCommand.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.routeservice.cli;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.routeservice.ResolvedRoute;
+import org.onosproject.routeservice.RouteInfo;
+import org.onosproject.routeservice.RouteService;
+import org.onosproject.routeservice.RouteTableId;
+
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Command to show the routes in the routing tables.
+ */
+@Command(scope = "onos", name = "routes",
+        description = "Lists routes in the route store")
+public class RoutesListCommand extends AbstractShellCommand {
+
+    private static final String NETWORK = "Network";
+    private static final String NEXTHOP = "Next Hop";
+    private static final String SOURCE = "Source";
+    private static final String NODE = "Node";
+
+    private static final String FORMAT_ROUTE = "%-1s   %-18s %-15s %s (%s)";
+    private static final String FORMAT_ROUTE6 = "%-1s   %-43s %-39s %s (%s)";
+
+    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);
+
+        if (outputJson()) {
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode result = mapper.createObjectNode();
+            result.set("routes4", json(service.getRoutes(new RouteTableId("ipv4"))));
+            result.set("routes6", json(service.getRoutes(new RouteTableId("ipv6"))));
+            print("%s", result);
+        } else {
+            service.getRouteTables().forEach(id -> {
+                Collection<RouteInfo> tableRoutes = service.getRoutes(id);
+
+                String format = tableRoutes.stream().anyMatch(route -> route.prefix().isIp6()) ?
+                        FORMAT_ROUTE6 : FORMAT_ROUTE;
+
+                // Print header
+                print(FORMAT_TABLE, id);
+                print(format, "", NETWORK, NEXTHOP, SOURCE, NODE);
+
+                // Print routing entries
+                tableRoutes.stream()
+                        .sorted(Comparator.comparing(r -> r.prefix().address()))
+                        .forEach(route -> this.print(format, route));
+
+                print(FORMAT_TOTAL, tableRoutes.size());
+                print("");
+            });
+        }
+    }
+
+    private void print(String format, RouteInfo routeInfo) {
+        routeInfo.allRoutes().stream()
+                .sorted(Comparator.comparing(r -> r.nextHop()))
+                .forEach(r -> print(format, isBestRoute(routeInfo.bestRoute(), r) ? ">" : "",
+                        r.prefix(), r.nextHop(), r.route().source(), r.route().sourceNode()));
+    }
+
+    private boolean isBestRoute(Optional<ResolvedRoute> bestRoute, ResolvedRoute route) {
+        return Objects.equals(bestRoute.orElse(null), route);
+    }
+
+    /**
+     * Produces a JSON array of routes.
+     *
+     * @param routes the routes with the data
+     * @return JSON array with the routes
+     */
+    private JsonNode json(Collection<RouteInfo> routes) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode result = mapper.createArrayNode();
+
+        routes.stream()
+                .flatMap(ri -> ri.allRoutes().stream())
+                .forEach(r -> result.add(json(mapper, r)));
+
+        return result;
+    }
+
+    /**
+     * Produces JSON object for a route.
+     *
+     * @param mapper the JSON object mapper to use
+     * @param route the route with the data
+     * @return JSON object for the route
+     */
+    private ObjectNode json(ObjectMapper mapper, ResolvedRoute route) {
+        ObjectNode result = mapper.createObjectNode();
+
+        result.put("prefix", route.prefix().toString());
+        result.put("nextHop", route.nextHop().toString());
+
+        return result;
+    }
+
+}
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/package-info.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/package-info.java
new file mode 100644
index 0000000..3880e5b
--- /dev/null
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Implementation of route service.
+ */
+package org.onosproject.routeservice.cli;