Allow adding/removing static routes through network config service
Change-Id: I800730d12af737ccecdb76f2000bca0984507dce
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
new file mode 100644
index 0000000..99dbc57
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteConfig.java
@@ -0,0 +1,53 @@
+/*
+ * 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.incubator.net.routing;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+
+import java.util.Set;
+
+/**
+ * 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";
+
+ /**
+ * Returns all routes in this configuration.
+ *
+ * @return A set of route.
+ */
+ public Set<Route> getRoutes() {
+ ImmutableSet.Builder<Route> routes = ImmutableSet.builder();
+ array.forEach(route -> {
+ try {
+ IpPrefix prefix = IpPrefix.valueOf(route.path(PREFIX).asText());
+ IpAddress nextHop = IpAddress.valueOf(route.path(NEXTHOP).asText());
+ routes.add(new Route(Route.Source.STATIC, prefix, nextHop));
+ } catch (IllegalArgumentException e) {
+ // Ignores routes that cannot be parsed correctly
+ }
+ });
+ return routes.build();
+ }
+}
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/routing/RouteConfigTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/routing/RouteConfigTest.java
new file mode 100644
index 0000000..b72f75c
--- /dev/null
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/routing/RouteConfigTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.incubator.net.routing;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+
+import java.io.InputStream;
+import java.util.Set;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for class {@link RouteConfigTest}.
+ */
+public class RouteConfigTest {
+ private static final String KEY = "org.onosproject.routing";
+
+ private static final IpPrefix PREFIX1 = IpPrefix.valueOf("10.0.0.1/24");
+ private static final IpPrefix PREFIX2 = IpPrefix.valueOf("20.0.0.1/24");
+ private static final IpPrefix PREFIX3 = IpPrefix.valueOf("30.0.0.1/24");
+ private static final IpAddress NEXTHOP1 = IpAddress.valueOf("192.168.1.1");
+ private static final IpAddress NEXTHOP2 = IpAddress.valueOf("192.168.2.1");
+ private static final Route ROUTE1 = new Route(Route.Source.STATIC, PREFIX1, NEXTHOP1);
+ private static final Route ROUTE2 = new Route(Route.Source.STATIC, PREFIX2, NEXTHOP1);
+ private static final Route ROUTE3 = new Route(Route.Source.STATIC, PREFIX3, NEXTHOP2);
+ private static final Set<Route> EXPECTED_ROUTES = ImmutableSet.of(ROUTE1, ROUTE2, ROUTE3);
+ private static final Set<Route> UNEXPECTED_ROUTES = ImmutableSet.of(ROUTE1, ROUTE2);
+
+ private RouteConfig config;
+
+ @Before
+ public void setUp() throws Exception {
+ InputStream jsonStream = RouteConfigTest.class
+ .getResourceAsStream("/route-config.json");
+ ApplicationId subject = new TestApplicationId(KEY);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonNode = mapper.readTree(jsonStream);
+ ConfigApplyDelegate delegate = new MockDelegate();
+
+ config = new RouteConfig();
+ config.init(subject, KEY, jsonNode, mapper, delegate);
+ }
+
+ @Test
+ public void getRoutes() throws Exception {
+ assertThat(config.getRoutes(), is(EXPECTED_ROUTES));
+ assertThat(config.getRoutes(), not(UNEXPECTED_ROUTES));
+ }
+
+ private class MockDelegate implements ConfigApplyDelegate {
+ @Override
+ public void onApply(Config config) {
+ }
+ }
+}
\ No newline at end of file
diff --git a/incubator/api/src/test/resources/route-config.json b/incubator/api/src/test/resources/route-config.json
new file mode 100644
index 0000000..9675f12
--- /dev/null
+++ b/incubator/api/src/test/resources/route-config.json
@@ -0,0 +1,14 @@
+[
+ {
+ "prefix": "10.0.0.1/24",
+ "nextHop": "192.168.1.1"
+ },
+ {
+ "prefix": "20.0.0.1/24",
+ "nextHop": "192.168.1.1"
+ },
+ {
+ "prefix": "30.0.0.1/24",
+ "nextHop": "192.168.2.1"
+ }
+]
\ No newline at end of file
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 c266de0..7902473 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,12 +23,14 @@
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.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;
@@ -36,6 +38,11 @@
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;
@@ -78,18 +85,34 @@
@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
@@ -98,6 +121,8 @@
routeStore.unsetDelegate(delegate);
hostService.removeListener(hostListener);
+ netcfgRegistry.removeListener(netcfgListener);
+ netcfgRegistry.unregisterConfigFactory(routeConfigFactory);
}
/**
@@ -328,4 +353,50 @@
}
}
+ 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 ba73cde..e740f3f 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,6 +39,7 @@
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;
@@ -93,6 +94,7 @@
routeManager = new TestRouteManager();
routeManager.hostService = hostService;
+ routeManager.netcfgRegistry = createMock(NetworkConfigRegistry.class);
LocalRouteStore routeStore = new LocalRouteStore();
routeStore.activate();