Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 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 | |
| 21 | package org.onosproject.simplefabric; |
| 22 | |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | |
| 25 | /** |
| 26 | * Routing tools. |
| 27 | */ |
| 28 | public 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 | } |