Lift route config access into its own component on top of the RouteService.

Also implement isValid for route config.

Change-Id: I5f4819ffa7c0e87e83407c3c30af7cf91d63c83b
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteConfig.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteConfig.java
index 99dbc57..a607ca7 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteConfig.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteConfig.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.incubator.net.routing;
 
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableSet;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
@@ -28,7 +29,6 @@
  * Route configuration object for Route Service.
  */
 public class RouteConfig extends Config<ApplicationId> {
-    private static final String ROUTES = "routes";
     private static final String PREFIX = "prefix";
     private static final String NEXTHOP = "nextHop";
 
@@ -50,4 +50,17 @@
         });
         return routes.build();
     }
+
+    @Override
+    public boolean isValid() {
+        array.forEach(routeNode -> {
+            if (!routeNode.isObject()) {
+                throw new IllegalArgumentException("Not object node");
+            }
+            ObjectNode route = (ObjectNode) routeNode;
+            isIpPrefix(route, PREFIX, FieldPresence.MANDATORY);
+            isIpAddress(route, NEXTHOP, FieldPresence.MANDATORY);
+        });
+        return true;
+    }
 }
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/ConfigurationRouteSource.java b/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/ConfigurationRouteSource.java
new file mode 100644
index 0000000..6537bae
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/ConfigurationRouteSource.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2017-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.incubator.net.routing.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.incubator.net.routing.Route;
+import org.onosproject.incubator.net.routing.RouteAdminService;
+import org.onosproject.incubator.net.routing.RouteConfig;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.SubjectFactories;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Route source that installs static routes configured in the network configuration.
+ */
+@Component(immediate = true)
+public class ConfigurationRouteSource {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry netcfgRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected RouteAdminService routeService;
+
+    private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory =
+            new ConfigFactory<ApplicationId, RouteConfig>(
+                    SubjectFactories.APP_SUBJECT_FACTORY,
+                    RouteConfig.class, "routes", true) {
+                @Override
+                public RouteConfig createConfig() {
+                    return new RouteConfig();
+                }
+            };
+    private final InternalNetworkConfigListener netcfgListener =
+            new InternalNetworkConfigListener();
+
+    @Activate
+    protected void activate() {
+        netcfgRegistry.addListener(netcfgListener);
+        netcfgRegistry.registerConfigFactory(routeConfigFactory);
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        netcfgRegistry.removeListener(netcfgListener);
+        netcfgRegistry.unregisterConfigFactory(routeConfigFactory);
+    }
+
+    private void processRouteConfigAdded(NetworkConfigEvent event) {
+        Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
+        routeService.update(routes);
+    }
+
+    private void processRouteConfigUpdated(NetworkConfigEvent event) {
+        Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
+        Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
+        Set<Route> pendingRemove = prevRoutes.stream()
+                .filter(prevRoute -> routes.stream()
+                        .noneMatch(route -> route.prefix().equals(prevRoute.prefix())))
+                .collect(Collectors.toSet());
+        Set<Route> pendingUpdate = routes.stream()
+                .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet());
+        routeService.update(pendingUpdate);
+        routeService.withdraw(pendingRemove);
+    }
+
+    private void processRouteConfigRemoved(NetworkConfigEvent event) {
+        Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
+        routeService.withdraw(prevRoutes);
+    }
+
+    private class InternalNetworkConfigListener implements
+            NetworkConfigListener {
+        @Override
+        public void event(NetworkConfigEvent event) {
+            if (event.configClass().equals(RouteConfig.class)) {
+                switch (event.type()) {
+                case CONFIG_ADDED:
+                    processRouteConfigAdded(event);
+                    break;
+                case CONFIG_UPDATED:
+                    processRouteConfigUpdated(event);
+                    break;
+                case CONFIG_REMOVED:
+                    processRouteConfigRemoved(event);
+                    break;
+                default:
+                    break;
+                }
+            }
+        }
+    }
+}
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/RouteManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/RouteManager.java
index 5f3640b..368d65e 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/RouteManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/RouteManager.java
@@ -23,14 +23,12 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.packet.IpAddress;
-import org.onosproject.core.ApplicationId;
 import org.onosproject.event.ListenerService;
-import org.onosproject.incubator.net.routing.NextHopData;
 import org.onosproject.incubator.net.routing.NextHop;
+import org.onosproject.incubator.net.routing.NextHopData;
 import org.onosproject.incubator.net.routing.ResolvedRoute;
 import org.onosproject.incubator.net.routing.Route;
 import org.onosproject.incubator.net.routing.RouteAdminService;
-import org.onosproject.incubator.net.routing.RouteConfig;
 import org.onosproject.incubator.net.routing.RouteEvent;
 import org.onosproject.incubator.net.routing.RouteListener;
 import org.onosproject.incubator.net.routing.RouteService;
@@ -38,11 +36,6 @@
 import org.onosproject.incubator.net.routing.RouteStoreDelegate;
 import org.onosproject.incubator.net.routing.RouteTableId;
 import org.onosproject.net.Host;
-import org.onosproject.net.config.ConfigFactory;
-import org.onosproject.net.config.NetworkConfigEvent;
-import org.onosproject.net.config.NetworkConfigListener;
-import org.onosproject.net.config.NetworkConfigRegistry;
-import org.onosproject.net.config.basics.SubjectFactories;
 import org.onosproject.net.host.HostEvent;
 import org.onosproject.net.host.HostListener;
 import org.onosproject.net.host.HostService;
@@ -85,34 +78,17 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected NetworkConfigRegistry netcfgRegistry;
-
     @GuardedBy(value = "this")
     private Map<RouteListener, ListenerQueue> listeners = new HashMap<>();
 
     private ThreadFactory threadFactory;
 
-    private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory =
-            new ConfigFactory<ApplicationId, RouteConfig>(
-                    SubjectFactories.APP_SUBJECT_FACTORY,
-                    RouteConfig.class, "routes", true) {
-                @Override
-                public RouteConfig createConfig() {
-                    return new RouteConfig();
-                }
-            };
-    private final InternalNetworkConfigListener netcfgListener =
-            new InternalNetworkConfigListener();
-
     @Activate
     protected void activate() {
         threadFactory = groupedThreads("onos/route", "listener-%d", log);
 
         routeStore.setDelegate(delegate);
         hostService.addListener(hostListener);
-        netcfgRegistry.addListener(netcfgListener);
-        netcfgRegistry.registerConfigFactory(routeConfigFactory);
     }
 
     @Deactivate
@@ -121,8 +97,6 @@
 
         routeStore.unsetDelegate(delegate);
         hostService.removeListener(hostListener);
-        netcfgRegistry.removeListener(netcfgListener);
-        netcfgRegistry.unregisterConfigFactory(routeConfigFactory);
     }
 
     /**
@@ -355,50 +329,4 @@
         }
     }
 
-    private class InternalNetworkConfigListener implements NetworkConfigListener {
-        @Override
-        public void event(NetworkConfigEvent event) {
-            if (event.configClass().equals(RouteConfig.class)) {
-                switch (event.type()) {
-                    case CONFIG_ADDED:
-                        processRouteConfigAdded(event);
-                        break;
-                    case CONFIG_UPDATED:
-                        processRouteConfigUpdated(event);
-                        break;
-                    case CONFIG_REMOVED:
-                        processRouteConfigRemoved(event);
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-
-        private void processRouteConfigAdded(NetworkConfigEvent event) {
-            log.info("processRouteConfigAdded {}", event);
-            Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
-            update(routes);
-        }
-
-        private void processRouteConfigUpdated(NetworkConfigEvent event) {
-            log.info("processRouteConfigUpdated {}", event);
-            Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
-            Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
-            Set<Route> pendingRemove = prevRoutes.stream()
-                    .filter(prevRoute -> routes.stream()
-                            .noneMatch(route -> route.prefix().equals(prevRoute.prefix())))
-                    .collect(Collectors.toSet());
-            Set<Route> pendingUpdate = routes.stream()
-                    .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet());
-            update(pendingUpdate);
-            withdraw(pendingRemove);
-        }
-
-        private void processRouteConfigRemoved(NetworkConfigEvent event) {
-            log.info("processRouteConfigRemoved {}", event);
-            Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
-            withdraw(prevRoutes);
-        }
-    }
 }
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/routing/impl/RouteManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/routing/impl/RouteManagerTest.java
index e740f3f..ba73cde 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/routing/impl/RouteManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/routing/impl/RouteManagerTest.java
@@ -39,7 +39,6 @@
 import org.onosproject.net.HostId;
 import org.onosproject.net.HostLocation;
 import org.onosproject.net.PortNumber;
-import org.onosproject.net.config.NetworkConfigRegistry;
 import org.onosproject.net.host.HostEvent;
 import org.onosproject.net.host.HostListener;
 import org.onosproject.net.host.HostService;
@@ -94,7 +93,6 @@
 
         routeManager = new TestRouteManager();
         routeManager.hostService = hostService;
-        routeManager.netcfgRegistry = createMock(NetworkConfigRegistry.class);
 
         LocalRouteStore routeStore = new LocalRouteStore();
         routeStore.activate();