Added REST API call to fetch routes count and routes count by type for Route Service
Change-Id: Ia9a5b269df17805d2f9aab2ca926bf7656168aee
diff --git a/apps/route-service/app/src/main/java/org/onosproject/routeservice/rest/RouteServiceWebResource.java b/apps/route-service/app/src/main/java/org/onosproject/routeservice/rest/RouteServiceWebResource.java
index a1c0296..0196af0 100644
--- a/apps/route-service/app/src/main/java/org/onosproject/routeservice/rest/RouteServiceWebResource.java
+++ b/apps/route-service/app/src/main/java/org/onosproject/routeservice/rest/RouteServiceWebResource.java
@@ -23,7 +23,7 @@
import org.onosproject.routeservice.Route;
import org.onosproject.routeservice.RouteAdminService;
import org.onosproject.routeservice.RouteService;
-
+import org.onosproject.routeservice.RouteInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
@@ -34,13 +34,14 @@
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static org.onlab.util.Tools.nullIsIllegal;
import static org.onlab.util.Tools.readTreeFromStream;
-
/**
* Manage the unicast routing information.
*/
@@ -72,6 +73,81 @@
return ok(root).build();
}
+ /**
+ * Get count of all unicast routes.
+ * Returns count of all known unicast routes.
+ *
+ * @return 200 OK with count of all known unicast routes
+ * @onos.rsModel RoutesGetCount
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/routes/count")
+ public Response getRoutesCount() {
+ RouteService service = get(RouteService.class);
+ ObjectNode root = mapper().createObjectNode();
+ service.getRouteTables().forEach(table -> {
+ Collection<RouteInfo> routes = service.getRoutes(table);
+ root.put(table.name() + "PrefixCount", routes.stream().count());
+ });
+ return ok(root).build();
+ }
+
+ /**
+ * Get count of all types routes .
+ * Returns count of all known route types.
+ *
+ * @return 200 OK with count of all route types
+ * @onos.rsModel RoutesGetTypeCount
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/routes/types/count")
+ public Response getRoutesCountByType() {
+ RouteService service = get(RouteService.class);
+ ObjectNode root = mapper().createObjectNode();
+ service.getRouteTables().forEach(table -> {
+ List<Route> staticRoutes = new ArrayList<>();
+ List<Route> fpmRoutes = new ArrayList<>();
+ List<Route> ripRoutes = new ArrayList<>();
+ List<Route> dhcpRoutes = new ArrayList<>();
+ List<Route> dhcpLQRoutes = new ArrayList<>();
+ List<Route> bgpRoutes = new ArrayList<>();
+ List<Route> routes = service.getRoutes(table).stream()
+ .flatMap(ri -> ri.allRoutes().stream())
+ .map(ResolvedRoute::route)
+ .collect(Collectors.toList());
+ routes.forEach(route -> {
+ if (route.source() == Route.Source.STATIC) {
+ staticRoutes.add(route);
+ }
+ if (route.source() == Route.Source.FPM) {
+ fpmRoutes.add(route);
+ }
+ if (route.source() == Route.Source.RIP) {
+ ripRoutes.add(route);
+ }
+ if (route.source() == Route.Source.DHCP) {
+ dhcpRoutes.add(route);
+ }
+ if (route.source() == Route.Source.DHCPLQ) {
+ dhcpLQRoutes.add(route);
+ }
+ if (route.source() == Route.Source.BGP) {
+ bgpRoutes.add(route);
+ }
+ });
+ root.put(table.name() + "StaticRouteCount", staticRoutes.size());
+ root.put(table.name() + "FpmRouteCount", fpmRoutes.size());
+ root.put(table.name() + "RipRouteCount", ripRoutes.size());
+ root.put(table.name() + "DhcpRouteCount", dhcpRoutes.size());
+ root.put(table.name() + "DhcpLQRouteCount", dhcpLQRoutes.size());
+ root.put(table.name() + "BgpRouteCount", bgpRoutes.size());
+ root.put(table.name() + "TotalRouteCount", routes.stream().count());
+ });
+ return ok(root).build();
+ }
+
/**
* Create new unicast route.
* Creates a new route in the unicast RIB. Source field is kept optional.
diff --git a/apps/route-service/app/src/main/resources/definitions/RoutesGetCount.json b/apps/route-service/app/src/main/resources/definitions/RoutesGetCount.json
new file mode 100644
index 0000000..e1f1eb9
--- /dev/null
+++ b/apps/route-service/app/src/main/resources/definitions/RoutesGetCount.json
@@ -0,0 +1,17 @@
+{
+ "type": "object",
+ "required": [
+ "ipv4PrefixCount",
+ "ipv6PrefixCount"
+ ],
+ "properties": {
+ "ipv4PrefixCount": {
+ "type": "long",
+ "example": "2"
+ },
+ "ipv6PrefixCount": {
+ "type": "long",
+ "example": "3"
+ }
+ }
+}
diff --git a/apps/route-service/app/src/main/resources/definitions/RoutesGetTypeCount.json b/apps/route-service/app/src/main/resources/definitions/RoutesGetTypeCount.json
new file mode 100644
index 0000000..497772d
--- /dev/null
+++ b/apps/route-service/app/src/main/resources/definitions/RoutesGetTypeCount.json
@@ -0,0 +1,77 @@
+{
+ "type": "object",
+ "required": [
+ "ipv4StaticRouteCount",
+ "ipv4FpmRouteCount",
+ "ipv4RipRouteCount",
+ "ipv4DhcpRouteCount",
+ "ipv4DhcpLQRouteCount",
+ "ipv4BgpRouteCount",
+ "ipv4TotalRouteCount",
+ "ipv6StaticRouteCount",
+ "ipv6FpmRouteCount",
+ "ipv6RipRouteCount",
+ "ipv6DhcpRouteCount",
+ "ipv6DhcpLQRouteCount",
+ "ipv6BgpRouteCount",
+ "ipv6TotalRouteCount"
+ ],
+ "properties": {
+ "ipv4StaticRouteCount": {
+ "type": "int",
+ "example": "1"
+ },
+ "ipv4FpmRouteCount": {
+ "type": "int",
+ "example": "3"
+ },
+ "ipv4RipRouteCount": {
+ "type": "int",
+ "example": "1"
+ },
+ "ipv4DhcpRouteCount": {
+ "type": "int",
+ "example": "2"
+ },
+ "ipv4DhcpLQRouteCount": {
+ "type": "int",
+ "example": "3"
+ },
+ "ipv4BgpRouteCount": {
+ "type": "int",
+ "example": "2"
+ },
+ "ipv4TotalRouteCount": {
+ "type": "long",
+ "example": "4"
+ },
+ "ipv6StaticRouteCount": {
+ "type": "int",
+ "example": "3"
+ },
+ "ipv6FpmRouteCount": {
+ "type": "int",
+ "example": "1"
+ },
+ "ipv6RipRouteCount": {
+ "type": "int",
+ "example": "2"
+ },
+ "ipv6DhcpRouteCount": {
+ "type": "int",
+ "example": "4"
+ },
+ "ipv6DhcpLQRouteCount": {
+ "type": "int",
+ "example": "2"
+ },
+ "ipv6BgpRouteCount": {
+ "type": "int",
+ "example": "3"
+ },
+ "ipv6TotalRouteCount": {
+ "type": "long",
+ "example": "2"
+ }
+ }
+}