Move reactive-routing-specific config and classes out of common routing bundle
Change-Id: I463e5225337bfaa0ec85285658dbbabc8059f209
diff --git a/apps/reactive-routing/BUCK b/apps/reactive-routing/BUCK
index 6e5a46e..9728498 100644
--- a/apps/reactive-routing/BUCK
+++ b/apps/reactive-routing/BUCK
@@ -1,5 +1,6 @@
COMPILE_DEPS = [
'//lib:CORE_DEPS',
+ '//lib:concurrent-trees',
'//incubator/api:onos-incubator-api',
'//apps/routing-api:onos-apps-routing-api',
'//apps/intentsync:onos-apps-intentsync',
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/IntentRequestListener.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/IntentRequestListener.java
similarity index 96%
rename from apps/routing-api/src/main/java/org/onosproject/routing/IntentRequestListener.java
rename to apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/IntentRequestListener.java
index fbb3f5b..b93b3ff 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/IntentRequestListener.java
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/IntentRequestListener.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-present Open Networking Laboratory
+ * 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.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.onosproject.routing;
+package org.onosproject.reactive.routing;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/config/LocalIpPrefixEntry.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/LocalIpPrefixEntry.java
similarity index 96%
rename from apps/routing-api/src/main/java/org/onosproject/routing/config/LocalIpPrefixEntry.java
rename to apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/LocalIpPrefixEntry.java
index 5b2b908..49f336e 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/config/LocalIpPrefixEntry.java
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/LocalIpPrefixEntry.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-present Open Networking Laboratory
+ * 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.
@@ -13,15 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.onosproject.routing.config;
+package org.onosproject.reactive.routing;
import com.google.common.base.MoreObjects;
-
-import java.util.Objects;
-
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
+import java.util.Objects;
+
/**
* Configuration details for an IP prefix entry.
*/
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/config/ReactiveRoutingConfig.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfig.java
similarity index 90%
rename from apps/routing-api/src/main/java/org/onosproject/routing/config/ReactiveRoutingConfig.java
rename to apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfig.java
index aa0b0c7..fd7e519 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/config/ReactiveRoutingConfig.java
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfig.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-present Open Networking Laboratory
+ * 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.
@@ -14,22 +14,20 @@
* limitations under the License.
*/
-package org.onosproject.routing.config;
+package org.onosproject.reactive.routing;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
-
-import java.util.Set;
-
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.config.Config;
-import org.onosproject.routing.config.LocalIpPrefixEntry.IpPrefixType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Set;
+
/**
* Configuration object for prefix config.
*/
@@ -63,7 +61,7 @@
prefixes.add(new LocalIpPrefixEntry(
IpPrefix.valueOf(jsonNode.get(IPPREFIX).asText()),
- IpPrefixType.valueOf(jsonNode.get(TYPE).asText()),
+ LocalIpPrefixEntry.IpPrefixType.valueOf(jsonNode.get(TYPE).asText()),
IpAddress.valueOf(jsonNode.get(GATEWAYIP).asText())));
});
@@ -89,7 +87,7 @@
prefixes.add(new LocalIpPrefixEntry(
IpPrefix.valueOf(jsonNode.get(IPPREFIX).asText()),
- IpPrefixType.valueOf(jsonNode.get(TYPE).asText()),
+ LocalIpPrefixEntry.IpPrefixType.valueOf(jsonNode.get(TYPE).asText()),
IpAddress.valueOf(jsonNode.get(GATEWAYIP).asText())));
});
diff --git a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfiguration.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfiguration.java
new file mode 100644
index 0000000..4308ddb
--- /dev/null
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfiguration.java
@@ -0,0 +1,251 @@
+/*
+ * 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.reactive.routing;
+
+import com.google.common.collect.ImmutableSet;
+import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
+import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
+import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
+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.apache.felix.scr.annotations.Service;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip6Address;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.ConnectPoint;
+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.NetworkConfigService;
+import org.onosproject.net.config.basics.SubjectFactories;
+import org.onosproject.routing.RoutingService;
+import org.onosproject.routing.config.BgpConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Reactive routing configuration manager.
+ */
+@Component(immediate = true)
+@Service
+public class ReactiveRoutingConfiguration implements
+ ReactiveRoutingConfigurationService {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigRegistry registry;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigService configService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected InterfaceService interfaceService;
+
+ private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
+ private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>();
+
+ private InvertedRadixTree<LocalIpPrefixEntry>
+ localPrefixTable4 = new ConcurrentInvertedRadixTree<>(
+ new DefaultByteArrayNodeFactory());
+ private InvertedRadixTree<LocalIpPrefixEntry>
+ localPrefixTable6 = new ConcurrentInvertedRadixTree<>(
+ new DefaultByteArrayNodeFactory());
+
+ private MacAddress virtualGatewayMacAddress;
+ private final InternalNetworkConfigListener configListener =
+ new InternalNetworkConfigListener();
+
+ private ConfigFactory<ApplicationId, ReactiveRoutingConfig>
+ reactiveRoutingConfigFactory =
+ new ConfigFactory<ApplicationId, ReactiveRoutingConfig>(
+ SubjectFactories.APP_SUBJECT_FACTORY,
+ ReactiveRoutingConfig.class, "reactiveRouting") {
+ @Override
+ public ReactiveRoutingConfig createConfig() {
+ return new ReactiveRoutingConfig();
+ }
+ };
+
+ @Activate
+ public void activate() {
+ configService.addListener(configListener);
+ registry.registerConfigFactory(reactiveRoutingConfigFactory);
+ setUpConfiguration();
+ log.info("Reactive routing configuration service started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ registry.unregisterConfigFactory(reactiveRoutingConfigFactory);
+ configService.removeListener(configListener);
+ log.info("Reactive routing configuration service stopped");
+ }
+
+ /**
+ * Set up reactive routing information from configuration.
+ */
+ private void setUpConfiguration() {
+ ReactiveRoutingConfig config = configService.getConfig(
+ coreService.registerApplication(ReactiveRoutingConfigurationService
+ .REACTIVE_ROUTING_APP_ID),
+ ReactiveRoutingConfigurationService.CONFIG_CLASS);
+ if (config == null) {
+ log.warn("No reactive routing config available!");
+ return;
+ }
+ for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) {
+ localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry);
+ gatewayIpAddresses.add(entry.getGatewayIpAddress());
+ }
+ for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) {
+ localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry);
+ gatewayIpAddresses.add(entry.getGatewayIpAddress());
+ }
+
+ virtualGatewayMacAddress = config.virtualGatewayMacAddress();
+
+ // Setup BGP peer connect points
+ ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
+ if (routerAppId == null) {
+ log.info("Router application ID is null!");
+ return;
+ }
+
+ BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class);
+
+ if (bgpConfig == null) {
+ log.info("BGP config is null!");
+ return;
+ } else {
+ bgpPeerConnectPoints =
+ bgpConfig.bgpSpeakers().stream()
+ .flatMap(speaker -> speaker.peers().stream())
+ .map(peer -> interfaceService.getMatchingInterface(peer))
+ .filter(Objects::nonNull)
+ .map(intf -> intf.connectPoint())
+ .collect(Collectors.toSet());
+ }
+ }
+
+ @Override
+ public boolean isIpAddressLocal(IpAddress ipAddress) {
+ if (ipAddress.isIp4()) {
+ return localPrefixTable4.getValuesForKeysPrefixing(
+ createBinaryString(
+ IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH)))
+ .iterator().hasNext();
+ } else {
+ return localPrefixTable6.getValuesForKeysPrefixing(
+ createBinaryString(
+ IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH)))
+ .iterator().hasNext();
+ }
+ }
+
+ @Override
+ public boolean isIpPrefixLocal(IpPrefix ipPrefix) {
+ return (localPrefixTable4.getValueForExactKey(
+ createBinaryString(ipPrefix)) != null ||
+ localPrefixTable6.getValueForExactKey(
+ createBinaryString(ipPrefix)) != null);
+ }
+
+ @Override
+ public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) {
+ return gatewayIpAddresses.contains(ipAddress);
+ }
+
+ @Override
+ public MacAddress getVirtualGatewayMacAddress() {
+ return virtualGatewayMacAddress;
+ }
+
+ @Override
+ public Set<ConnectPoint> getBgpPeerConnectPoints() {
+ return ImmutableSet.copyOf(bgpPeerConnectPoints);
+ }
+
+ /**
+ * Creates the binary string representation of an IP prefix.
+ * The prefix can be either IPv4 or IPv6.
+ * The string length is equal to the prefix length + 1.
+ *
+ * For each string, we put a extra "0" in the front. The purpose of
+ * doing this is to store the default route inside InvertedRadixTree.
+ *
+ * @param ipPrefix the IP prefix to use
+ * @return the binary string representation
+ */
+ private static String createBinaryString(IpPrefix ipPrefix) {
+ if (ipPrefix.prefixLength() == 0) {
+ return "0";
+ }
+
+ byte[] octets = ipPrefix.address().toOctets();
+ StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
+ for (int i = 0; i < ipPrefix.prefixLength(); i++) {
+ int byteOffset = i / Byte.SIZE;
+ int bitOffset = i % Byte.SIZE;
+ int mask = 1 << (Byte.SIZE - 1 - bitOffset);
+ byte value = octets[byteOffset];
+ boolean isSet = ((value & mask) != 0);
+ result.append(isSet ? "1" : "0");
+ }
+
+ return "0" + result.toString();
+ }
+
+ private class InternalNetworkConfigListener implements NetworkConfigListener {
+
+ @Override
+ public void event(NetworkConfigEvent event) {
+ switch (event.type()) {
+ case CONFIG_REGISTERED:
+ break;
+ case CONFIG_UNREGISTERED:
+ break;
+ case CONFIG_ADDED:
+ case CONFIG_UPDATED:
+ case CONFIG_REMOVED:
+ if (event.configClass() == ReactiveRoutingConfigurationService.CONFIG_CLASS) {
+ setUpConfiguration();
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfigurationService.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfigurationService.java
new file mode 100644
index 0000000..513073f
--- /dev/null
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingConfigurationService.java
@@ -0,0 +1,70 @@
+/*
+ * 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.reactive.routing;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Set;
+
+/**
+ * Provides information about the routing configuration.
+ */
+public interface ReactiveRoutingConfigurationService {
+
+ String REACTIVE_ROUTING_APP_ID = "org.onosproject.reactive.routing";
+
+ Class<ReactiveRoutingConfig> CONFIG_CLASS = ReactiveRoutingConfig.class;
+
+
+ MacAddress getVirtualGatewayMacAddress();
+
+ /**
+ * Evaluates whether an IP address is a virtual gateway IP address.
+ *
+ * @param ipAddress the IP address to evaluate
+ * @return true if the IP address is a virtual gateway address, otherwise false
+ */
+ boolean isVirtualGatewayIpAddress(IpAddress ipAddress);
+
+ /**
+ * Evaluates whether an IP address belongs to local SDN network.
+ *
+ * @param ipAddress the IP address to evaluate
+ * @return true if the IP address belongs to local SDN network, otherwise false
+ */
+ boolean isIpAddressLocal(IpAddress ipAddress);
+
+ /**
+ * Evaluates whether an IP prefix belongs to local SDN network.
+ *
+ * @param ipPrefix the IP prefix to evaluate
+ * @return true if the IP prefix belongs to local SDN network, otherwise false
+ */
+ boolean isIpPrefixLocal(IpPrefix ipPrefix);
+
+ /**
+ * Retrieves the entire set of connect points connected to BGP peers in the
+ * network.
+ *
+ * @return the set of connect points connected to BGP peers
+ */
+ Set<ConnectPoint> getBgpPeerConnectPoints();
+
+
+}
diff --git a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingFib.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingFib.java
index e57d9a5..4998ae6 100644
--- a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingFib.java
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/ReactiveRoutingFib.java
@@ -38,7 +38,6 @@
import org.onosproject.net.intent.Key;
import org.onosproject.net.intent.MultiPointToSinglePointIntent;
import org.onosproject.net.intent.constraint.PartialFailureConstraint;
-import org.onosproject.routing.IntentRequestListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/SdnIpReactiveRouting.java b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/SdnIpReactiveRouting.java
index d596342..e2faaff 100644
--- a/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/SdnIpReactiveRouting.java
+++ b/apps/reactive-routing/src/main/java/org/onosproject/reactive/routing/SdnIpReactiveRouting.java
@@ -48,9 +48,7 @@
import org.onosproject.net.packet.PacketContext;
import org.onosproject.net.packet.PacketProcessor;
import org.onosproject.net.packet.PacketService;
-import org.onosproject.routing.IntentRequestListener;
import org.onosproject.intentsync.IntentSynchronizationService;
-import org.onosproject.routing.config.RoutingConfigurationService;
import org.slf4j.Logger;
import java.nio.ByteBuffer;
@@ -89,7 +87,7 @@
protected IntentSynchronizationService intentSynchronizer;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected RoutingConfigurationService config;
+ protected ReactiveRoutingConfigurationService config;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected InterfaceService interfaceService;
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/config/RoutingConfigurationService.java b/apps/routing-api/src/main/java/org/onosproject/routing/config/RoutingConfigurationService.java
index e2ed83f..7c7b0e5 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/config/RoutingConfigurationService.java
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/config/RoutingConfigurationService.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-present Open Networking Laboratory
+ * 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.
@@ -13,58 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.onosproject.routing.config;
-import java.util.Set;
-
-import org.onlab.packet.IpAddress;
-import org.onlab.packet.IpPrefix;
-import org.onlab.packet.MacAddress;
-import org.onosproject.net.ConnectPoint;
-
/**
- * Provides information about the routing configuration.
+ * Routing configuration service. Going away very soon.
+ *
+ * @deprecated in Junco release
*/
+@Deprecated
public interface RoutingConfigurationService {
- String REACTIVE_ROUTING_APP_ID = "org.onosproject.reactive.routing";
-
- Class<ReactiveRoutingConfig> CONFIG_CLASS = ReactiveRoutingConfig.class;
-
-
- MacAddress getVirtualGatewayMacAddress();
-
/**
- * Evaluates whether an IP address is a virtual gateway IP address.
- *
- * @param ipAddress the IP address to evaluate
- * @return true if the IP address is a virtual gateway address, otherwise false
+ * Empty method to pacify checkstyle.
*/
- boolean isVirtualGatewayIpAddress(IpAddress ipAddress);
-
- /**
- * Evaluates whether an IP address belongs to local SDN network.
- *
- * @param ipAddress the IP address to evaluate
- * @return true if the IP address belongs to local SDN network, otherwise false
- */
- boolean isIpAddressLocal(IpAddress ipAddress);
-
- /**
- * Evaluates whether an IP prefix belongs to local SDN network.
- *
- * @param ipPrefix the IP prefix to evaluate
- * @return true if the IP prefix belongs to local SDN network, otherwise false
- */
- boolean isIpPrefixLocal(IpPrefix ipPrefix);
-
- /**
- * Retrieves the entire set of connect points connected to BGP peers in the
- * network.
- *
- * @return the set of connect points connected to BGP peers
- */
- Set<ConnectPoint> getBgpPeerConnectPoints();
-
-
+ default void nothing() {
+ }
}
diff --git a/apps/routing/common/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java b/apps/routing/common/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
index ea22322..c41724f 100644
--- a/apps/routing/common/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
+++ b/apps/routing/common/src/main/java/org/onosproject/routing/config/impl/RoutingConfigurationImpl.java
@@ -16,46 +16,26 @@
package org.onosproject.routing.config.impl;
-import com.google.common.collect.ImmutableSet;
-import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
-import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
-import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
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.apache.felix.scr.annotations.Service;
-import org.onlab.packet.Ip4Address;
-import org.onlab.packet.Ip6Address;
-import org.onlab.packet.IpAddress;
-import org.onlab.packet.IpPrefix;
-import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.incubator.net.intf.InterfaceService;
-import org.onosproject.net.ConnectPoint;
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.NetworkConfigService;
import org.onosproject.net.config.basics.SubjectFactories;
-import org.onosproject.routing.RoutingService;
import org.onosproject.routing.config.BgpConfig;
-import org.onosproject.routing.config.LocalIpPrefixEntry;
-import org.onosproject.routing.config.ReactiveRoutingConfig;
import org.onosproject.routing.config.RouterConfig;
import org.onosproject.routing.config.RoutersConfig;
import org.onosproject.routing.config.RoutingConfigurationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.HashSet;
-import java.util.Objects;
-import java.util.Set;
-import java.util.stream.Collectors;
-
/**
* Implementation of RoutingConfigurationService which reads routing
* configuration from the network configuration service.
@@ -78,20 +58,6 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected InterfaceService interfaceService;
- private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
- private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>();
-
- private InvertedRadixTree<LocalIpPrefixEntry>
- localPrefixTable4 = new ConcurrentInvertedRadixTree<>(
- new DefaultByteArrayNodeFactory());
- private InvertedRadixTree<LocalIpPrefixEntry>
- localPrefixTable6 = new ConcurrentInvertedRadixTree<>(
- new DefaultByteArrayNodeFactory());
-
- private MacAddress virtualGatewayMacAddress;
- private final InternalNetworkConfigListener configListener =
- new InternalNetworkConfigListener();
-
private ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory =
new ConfigFactory<ApplicationId, BgpConfig>(
SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") {
@@ -119,25 +85,11 @@
}
};
- private ConfigFactory<ApplicationId, ReactiveRoutingConfig>
- reactiveRoutingConfigFactory =
- new ConfigFactory<ApplicationId, ReactiveRoutingConfig>(
- SubjectFactories.APP_SUBJECT_FACTORY,
- ReactiveRoutingConfig.class, "reactiveRouting") {
- @Override
- public ReactiveRoutingConfig createConfig() {
- return new ReactiveRoutingConfig();
- }
- };
-
@Activate
public void activate() {
- configService.addListener(configListener);
registry.registerConfigFactory(bgpConfigFactory);
registry.registerConfigFactory(routerConfigFactory);
registry.registerConfigFactory(routersConfigFactory);
- registry.registerConfigFactory(reactiveRoutingConfigFactory);
- setUpConfiguration();
log.info("Routing configuration service started");
}
@@ -146,144 +98,7 @@
registry.unregisterConfigFactory(bgpConfigFactory);
registry.unregisterConfigFactory(routerConfigFactory);
registry.unregisterConfigFactory(routersConfigFactory);
- registry.unregisterConfigFactory(reactiveRoutingConfigFactory);
- configService.removeListener(configListener);
log.info("Routing configuration service stopped");
}
- /**
- * Set up reactive routing information from configuration.
- */
- private void setUpConfiguration() {
- ReactiveRoutingConfig config = configService.getConfig(
- coreService.registerApplication(RoutingConfigurationService
- .REACTIVE_ROUTING_APP_ID),
- RoutingConfigurationService.CONFIG_CLASS);
- if (config == null) {
- log.warn("No reactive routing config available!");
- return;
- }
- for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) {
- localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry);
- gatewayIpAddresses.add(entry.getGatewayIpAddress());
- }
- for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) {
- localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry);
- gatewayIpAddresses.add(entry.getGatewayIpAddress());
- }
-
- virtualGatewayMacAddress = config.virtualGatewayMacAddress();
-
- // Setup BGP peer connect points
- ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
- if (routerAppId == null) {
- log.info("Router application ID is null!");
- return;
- }
-
- BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class);
-
- if (bgpConfig == null) {
- log.info("BGP config is null!");
- return;
- } else {
- bgpPeerConnectPoints =
- bgpConfig.bgpSpeakers().stream()
- .flatMap(speaker -> speaker.peers().stream())
- .map(peer -> interfaceService.getMatchingInterface(peer))
- .filter(Objects::nonNull)
- .map(intf -> intf.connectPoint())
- .collect(Collectors.toSet());
- }
- }
-
- @Override
- public boolean isIpAddressLocal(IpAddress ipAddress) {
- if (ipAddress.isIp4()) {
- return localPrefixTable4.getValuesForKeysPrefixing(
- createBinaryString(
- IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH)))
- .iterator().hasNext();
- } else {
- return localPrefixTable6.getValuesForKeysPrefixing(
- createBinaryString(
- IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH)))
- .iterator().hasNext();
- }
- }
-
- @Override
- public boolean isIpPrefixLocal(IpPrefix ipPrefix) {
- return (localPrefixTable4.getValueForExactKey(
- createBinaryString(ipPrefix)) != null ||
- localPrefixTable6.getValueForExactKey(
- createBinaryString(ipPrefix)) != null);
- }
-
- @Override
- public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) {
- return gatewayIpAddresses.contains(ipAddress);
- }
-
- @Override
- public MacAddress getVirtualGatewayMacAddress() {
- return virtualGatewayMacAddress;
- }
-
- @Override
- public Set<ConnectPoint> getBgpPeerConnectPoints() {
- return ImmutableSet.copyOf(bgpPeerConnectPoints);
- }
-
- /**
- * Creates the binary string representation of an IP prefix.
- * The prefix can be either IPv4 or IPv6.
- * The string length is equal to the prefix length + 1.
- *
- * For each string, we put a extra "0" in the front. The purpose of
- * doing this is to store the default route inside InvertedRadixTree.
- *
- * @param ipPrefix the IP prefix to use
- * @return the binary string representation
- */
- private static String createBinaryString(IpPrefix ipPrefix) {
- if (ipPrefix.prefixLength() == 0) {
- return "0";
- }
-
- byte[] octets = ipPrefix.address().toOctets();
- StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
- for (int i = 0; i < ipPrefix.prefixLength(); i++) {
- int byteOffset = i / Byte.SIZE;
- int bitOffset = i % Byte.SIZE;
- int mask = 1 << (Byte.SIZE - 1 - bitOffset);
- byte value = octets[byteOffset];
- boolean isSet = ((value & mask) != 0);
- result.append(isSet ? "1" : "0");
- }
-
- return "0" + result.toString();
- }
-
- private class InternalNetworkConfigListener implements NetworkConfigListener {
-
- @Override
- public void event(NetworkConfigEvent event) {
- switch (event.type()) {
- case CONFIG_REGISTERED:
- break;
- case CONFIG_UNREGISTERED:
- break;
- case CONFIG_ADDED:
- case CONFIG_UPDATED:
- case CONFIG_REMOVED:
- if (event.configClass() == RoutingConfigurationService.CONFIG_CLASS) {
- setUpConfiguration();
- }
- break;
- default:
- break;
- }
- }
- }
}