Factor out createBinaryString method
Change-Id: I28366d04db29a313728bd4ef71f002737b51f008
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
index 4308ddb..d4c9d30 100644
--- 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
@@ -51,6 +51,8 @@
import java.util.Set;
import java.util.stream.Collectors;
+import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString;
+
/**
* Reactive routing configuration manager.
*/
@@ -197,36 +199,6 @@
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
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteTools.java b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteTools.java
new file mode 100644
index 0000000..422cb6a
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteTools.java
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+import org.onlab.packet.IpPrefix;
+
+/**
+ * Routing tools.
+ */
+public final class RouteTools {
+
+ private RouteTools() {
+ }
+
+ /**
+ * Creates a binary string representation of an IP prefix.
+ *
+ * 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
+ */
+ public static String createBinaryString(IpPrefix ipPrefix) {
+ byte[] octets = ipPrefix.address().toOctets();
+ StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
+ result.append("0");
+ 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 result.toString();
+ }
+}
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedRouteStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedRouteStore.java
index 56762bf..98c2993 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedRouteStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedRouteStore.java
@@ -52,6 +52,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.incubator.net.routing.RouteEvent.Type.ROUTE_ADDED;
import static org.onosproject.incubator.net.routing.RouteEvent.Type.ROUTE_REMOVED;
+import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString;
/**
* Route store based on distributed storage.
@@ -239,21 +240,6 @@
return localRouteTables.get(routeTableId);
}
- private static String createBinaryString(IpPrefix ipPrefix) {
- byte[] octets = ipPrefix.address().toOctets();
- StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
- result.append("0");
- 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 result.toString();
- }
-
private class RouteTableListener implements MapEventListener<IpPrefix, Route> {
@Override
public void event(MapEvent<IpPrefix, Route> event) {
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/LocalRouteStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/LocalRouteStore.java
index 991c239..46346d7 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/LocalRouteStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/LocalRouteStore.java
@@ -47,6 +47,7 @@
import java.util.concurrent.ConcurrentHashMap;
import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString;
/**
* Route store based on in-memory storage.
@@ -175,22 +176,6 @@
return routeTables.get(routeTableId);
}
- private static String createBinaryString(IpPrefix ipPrefix) {
- byte[] octets = ipPrefix.address().toOctets();
- StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
- result.append("0");
- 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 result.toString();
- }
-
/**
* Route table into which routes can be placed.
*/