Make vRouter components into separate apps.
This allows us to leverage the ONOS app subsystem for selecting which
components to load.
CORD-710
Change-Id: Ibd7c4c1afd2caa137b44c085e7b6b5b4a1082521
diff --git a/apps/routing-api/BUCK b/apps/routing-api/BUCK
index d9db85a..4e4939b 100644
--- a/apps/routing-api/BUCK
+++ b/apps/routing-api/BUCK
@@ -1,5 +1,6 @@
COMPILE_DEPS = [
'//lib:CORE_DEPS',
+ '//incubator/api:onos-incubator-api',
]
TEST_DEPS = [
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/AsyncDeviceFetcher.java b/apps/routing-api/src/main/java/org/onosproject/routing/AsyncDeviceFetcher.java
new file mode 100644
index 0000000..fd5dfc3
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/AsyncDeviceFetcher.java
@@ -0,0 +1,104 @@
+/*
+ * 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.routing;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Provides a means of asynchronously waiting on devices.
+ */
+public final class AsyncDeviceFetcher {
+
+ private DeviceService deviceService;
+
+ private DeviceListener listener = new InternalDeviceListener();
+
+ private Map<DeviceId, CompletableFuture<DeviceId>> devices = new ConcurrentHashMap();
+
+ private AsyncDeviceFetcher(DeviceService deviceService) {
+ this.deviceService = deviceService;
+ deviceService.addListener(listener);
+ }
+
+ /**
+ * Shuts down.
+ */
+ public void shutdown() {
+ deviceService.removeListener(listener);
+ devices.clear();
+ }
+
+ /**
+ * Returns a completable future that completes when the device is available
+ * for the first time.
+ *
+ * @param deviceId ID of the device
+ * @return completable future
+ */
+ public CompletableFuture<DeviceId> getDevice(DeviceId deviceId) {
+ CompletableFuture<DeviceId> future = new CompletableFuture<>();
+ return devices.computeIfAbsent(deviceId, deviceId1 -> {
+ if (deviceService.isAvailable(deviceId)) {
+ future.complete(deviceId);
+ }
+ return future;
+ });
+ }
+
+ /**
+ * Creates a device fetcher based on the device service.
+ *
+ * @param deviceService device service
+ * @return device fetcher
+ */
+ public static AsyncDeviceFetcher create(DeviceService deviceService) {
+ return new AsyncDeviceFetcher(deviceService);
+ }
+
+ private class InternalDeviceListener implements DeviceListener {
+ @Override
+ public void event(DeviceEvent event) {
+ switch (event.type()) {
+ case DEVICE_ADDED:
+ case DEVICE_AVAILABILITY_CHANGED:
+ if (deviceService.isAvailable(event.subject().id())) {
+ DeviceId deviceId = event.subject().id();
+ CompletableFuture<DeviceId> future = devices.get(deviceId);
+ if (future != null) {
+ future.complete(deviceId);
+ }
+ }
+ break;
+ case DEVICE_UPDATED:
+ case DEVICE_REMOVED:
+ case DEVICE_SUSPENDED:
+ case PORT_ADDED:
+ case PORT_UPDATED:
+ case PORT_REMOVED:
+ default:
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/NextHop.java b/apps/routing-api/src/main/java/org/onosproject/routing/NextHop.java
new file mode 100644
index 0000000..b8b800c
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/NextHop.java
@@ -0,0 +1,101 @@
+/*
+ * 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.routing;
+
+import java.util.Objects;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Represents a next hop for routing, whose MAC address has already been resolved.
+ */
+public class NextHop {
+
+ private final IpAddress ip;
+ private final MacAddress mac;
+ private final NextHopGroupKey group;
+
+ /**
+ * Creates a new next hop.
+ *
+ * @param ip next hop's IP address
+ * @param mac next hop's MAC address
+ * @param group next hop's group
+ */
+ public NextHop(IpAddress ip, MacAddress mac, NextHopGroupKey group) {
+ this.ip = ip;
+ this.mac = mac;
+ this.group = group;
+ }
+
+ /**
+ * Returns the next hop's IP address.
+ *
+ * @return next hop's IP address
+ */
+ public IpAddress ip() {
+ return ip;
+ }
+
+ /**
+ * Returns the next hop's MAC address.
+ *
+ * @return next hop's MAC address
+ */
+ public MacAddress mac() {
+ return mac;
+ }
+
+ /**
+ * Returns the next hop group.
+ *
+ * @return group
+ */
+ public NextHopGroupKey group() {
+ return group;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof NextHop)) {
+ return false;
+ }
+
+ NextHop that = (NextHop) o;
+
+ return Objects.equals(this.ip, that.ip) &&
+ Objects.equals(this.mac, that.mac) &&
+ Objects.equals(this.group, that.group);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(ip, mac, group);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("ip", ip)
+ .add("mac", mac)
+ .add("group", group)
+ .toString();
+ }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/NextHopGroupKey.java b/apps/routing-api/src/main/java/org/onosproject/routing/NextHopGroupKey.java
new file mode 100644
index 0000000..12d993b
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/NextHopGroupKey.java
@@ -0,0 +1,73 @@
+/*
+ * 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.routing;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.IpAddress;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Identifier for a next hop group.
+ */
+public class NextHopGroupKey {
+
+ private final IpAddress address;
+
+ /**
+ * Creates a new next hop group key.
+ *
+ * @param address next hop's IP address
+ */
+ public NextHopGroupKey(IpAddress address) {
+ this.address = checkNotNull(address);
+ }
+
+ /**
+ * Returns the next hop's IP address.
+ *
+ * @return next hop's IP address
+ */
+ public IpAddress address() {
+ return address;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof NextHopGroupKey)) {
+ return false;
+ }
+
+ NextHopGroupKey that = (NextHopGroupKey) o;
+
+ return Objects.equals(this.address, that.address);
+ }
+
+ @Override
+ public int hashCode() {
+ return address.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("address", address)
+ .toString();
+ }
+}
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/RouterInterfaceManager.java b/apps/routing-api/src/main/java/org/onosproject/routing/RouterInterfaceManager.java
new file mode 100644
index 0000000..8b1deff
--- /dev/null
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/RouterInterfaceManager.java
@@ -0,0 +1,200 @@
+/*
+ * 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.routing;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceEvent;
+import org.onosproject.incubator.net.intf.InterfaceListener;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Manages which interfaces are part of the router when the configuration is
+ * updated, and handles the provisioning/unprovisioning of interfaces when they
+ * are added/removed.
+ */
+public class RouterInterfaceManager {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private final Consumer<Interface> provisioner;
+ private final Consumer<Interface> unprovisioner;
+
+ private Set<String> configuredInterfaces = Collections.emptySet();
+ private Set<Interface> provisioned = new HashSet<>();
+
+ private InterfaceService interfaceService;
+ private InterfaceListener listener = new InternalInterfaceListener();
+
+ private final DeviceId routerDeviceId;
+
+ /**
+ * Creates a new router interface manager.
+ *
+ * @param deviceId router device ID
+ * @param configuredInterfaces names of interfaces configured for this router
+ * @param interfaceService interface service
+ * @param provisioner consumer that will provision new interfaces
+ * @param unprovisioner consumer that will unprovision old interfaces
+ */
+ public RouterInterfaceManager(DeviceId deviceId,
+ Set<String> configuredInterfaces,
+ InterfaceService interfaceService,
+ Consumer<Interface> provisioner,
+ Consumer<Interface> unprovisioner) {
+ this.routerDeviceId = checkNotNull(deviceId);
+ this.provisioner = checkNotNull(provisioner);
+ this.unprovisioner = checkNotNull(unprovisioner);
+ this.interfaceService = checkNotNull(interfaceService);
+ this.configuredInterfaces = checkNotNull(configuredInterfaces);
+
+ provision();
+
+ interfaceService.addListener(listener);
+ }
+
+ /**
+ * Cleans up the router and unprovisions all interfaces.
+ */
+ public void cleanup() {
+ interfaceService.removeListener(listener);
+
+ unprovision();
+ }
+
+ /**
+ * Retrieves the set of configured interface names.
+ *
+ * @return interface names
+ */
+ public Set<String> configuredInterfaces() {
+ return configuredInterfaces;
+ }
+
+ /**
+ * Changes the set of interfaces configured on the router.
+ *
+ * @param newConfiguredInterfaces new set of router interfaces
+ */
+ public void changeConfiguredInterfaces(Set<String> newConfiguredInterfaces) {
+ Set<String> oldConfiguredInterfaces = configuredInterfaces;
+ configuredInterfaces = ImmutableSet.copyOf(newConfiguredInterfaces);
+
+ if (newConfiguredInterfaces.isEmpty() && !oldConfiguredInterfaces.isEmpty()) {
+ // Reverted to using all interfaces. Provision interfaces that
+ // weren't previously in the configured list
+ getInterfacesForDevice(routerDeviceId)
+ .filter(intf -> !oldConfiguredInterfaces.contains(intf.name()))
+ .forEach(this::provision);
+ } else if (!newConfiguredInterfaces.isEmpty() && oldConfiguredInterfaces.isEmpty()) {
+ // Began using an interface list. Unprovision interfaces that
+ // are not in the new interface list.
+ getInterfacesForDevice(routerDeviceId)
+ .filter(intf -> !newConfiguredInterfaces.contains(intf.name()))
+ .forEach(this::unprovision);
+ } else {
+ // The existing interface list was changed.
+ Set<String> toUnprovision = Sets.difference(oldConfiguredInterfaces, newConfiguredInterfaces);
+ Set<String> toProvision = Sets.difference(newConfiguredInterfaces, oldConfiguredInterfaces);
+
+ toUnprovision.forEach(name ->
+ getInterfacesForDevice(routerDeviceId)
+ .filter(intf -> intf.name().equals(name))
+ .findFirst()
+ .ifPresent(this::unprovision)
+ );
+
+ toProvision.forEach(name ->
+ getInterfacesForDevice(routerDeviceId)
+ .filter(intf -> intf.name().equals(name))
+ .findFirst()
+ .ifPresent(this::provision)
+ );
+ }
+
+ configuredInterfaces = newConfiguredInterfaces;
+ }
+
+ private void provision() {
+ getInterfacesForDevice(routerDeviceId)
+ .filter(this::shouldUse)
+ .forEach(this::provision);
+ }
+
+ private void unprovision() {
+ getInterfacesForDevice(routerDeviceId)
+ .filter(this::shouldUse)
+ .forEach(this::unprovision);
+ }
+
+ private void provision(Interface intf) {
+ if (!provisioned.contains(intf) && shouldUse(intf)) {
+ log.info("Provisioning interface {}", intf);
+ provisioner.accept(intf);
+ provisioned.add(intf);
+ }
+ }
+
+ private void unprovision(Interface intf) {
+ if (provisioned.contains(intf)) {
+ log.info("Unprovisioning interface {}", intf);
+ unprovisioner.accept(intf);
+ provisioned.remove(intf);
+ }
+ }
+
+ private boolean shouldUse(Interface intf) {
+ return configuredInterfaces.isEmpty() || configuredInterfaces.contains(intf.name());
+ }
+
+ private Stream<Interface> getInterfacesForDevice(DeviceId deviceId) {
+ return interfaceService.getInterfaces().stream()
+ .filter(intf -> intf.connectPoint().deviceId().equals(deviceId));
+ }
+
+ private class InternalInterfaceListener implements InterfaceListener {
+ @Override
+ public void event(InterfaceEvent event) {
+ Interface intf = event.subject();
+ switch (event.type()) {
+ case INTERFACE_ADDED:
+ provision(intf);
+ break;
+ case INTERFACE_UPDATED:
+ // TODO
+ break;
+ case INTERFACE_REMOVED:
+ unprovision(intf);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}