blob: d02fed25ba4291b98484f6af9b53340d0885779c [file] [log] [blame]
Lee Yongjae7c27bb42017-11-17 12:00:45 +09001/*
Jian Li8df54a92018-08-23 17:01:31 +09002 * Copyright 2018-present Open Networking Foundation
Lee Yongjae7c27bb42017-11-17 12:00:45 +09003 *
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/*
17 * local copy of onos/incubator/api/src/main/java/org/onosproject/incubator/net/routing/RouteTools.java
18 * to remove dependency on onos.incubator.routing services, since 2017-08-09.
19 */
20
Jian Li8df54a92018-08-23 17:01:31 +090021package org.onosproject.simplefabric.util;
Lee Yongjae7c27bb42017-11-17 12:00:45 +090022
23import org.onlab.packet.IpPrefix;
24
25/**
26 * Routing tools.
27 */
28public final class RouteTools {
29
30 private RouteTools() {
31 }
32
33 /**
34 * Creates a binary string representation of an IP prefix.
35 *
36 * For each string, we put a extra "0" in the front. The purpose of
37 * doing this is to store the default route inside InvertedRadixTree.
38 *
39 * @param ipPrefix the IP prefix to use
40 * @return the binary string representation
41 */
42 public static String createBinaryString(IpPrefix ipPrefix) {
43 byte[] octets = ipPrefix.address().toOctets();
44 StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
45 result.append("0");
46 for (int i = 0; i < ipPrefix.prefixLength(); i++) {
47 int byteOffset = i / Byte.SIZE;
48 int bitOffset = i % Byte.SIZE;
49 int mask = 1 << (Byte.SIZE - 1 - bitOffset);
50 byte value = octets[byteOffset];
51 boolean isSet = ((value & mask) != 0);
52 result.append(isSet ? "1" : "0");
53 }
54 return result.toString();
55 }
56}