blob: 52647196012cac0824687c307c01325190bbf61e [file] [log] [blame]
Jonathan Hartc4c2d622017-02-10 14:13:57 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hartc4c2d622017-02-10 14:13:57 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ray Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice;
Jonathan Hartc4c2d622017-02-10 14:13:57 -080018
19import org.onlab.packet.IpPrefix;
20
21/**
22 * Routing tools.
23 */
24public final class RouteTools {
25
26 private RouteTools() {
27 }
28
29 /**
30 * Creates a binary string representation of an IP prefix.
31 *
32 * For each string, we put a extra "0" in the front. The purpose of
33 * doing this is to store the default route inside InvertedRadixTree.
34 *
35 * @param ipPrefix the IP prefix to use
36 * @return the binary string representation
37 */
38 public static String createBinaryString(IpPrefix ipPrefix) {
39 byte[] octets = ipPrefix.address().toOctets();
40 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
41 result.append("0");
42 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
43 int byteOffset = i / Byte.SIZE;
44 int bitOffset = i % Byte.SIZE;
45 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
46 byte value = octets[byteOffset];
47 boolean isSet = ((value & mask) != 0);
48 result.append(isSet ? "1" : "0");
49 }
50 return result.toString();
51 }
52}