Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | package org.onosproject.lisp.ctl.impl.util; |
| 18 | |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 21 | |
| 22 | /** |
| 23 | * A LISP map utility class includes various useful methods. |
| 24 | */ |
| 25 | public final class LispMapUtil { |
| 26 | |
| 27 | /** |
| 28 | * Prevents object instantiation from external. |
| 29 | */ |
| 30 | private LispMapUtil() { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Generates CIDR style string from EID record. |
| 35 | * |
| 36 | * @param eidRecord EID record |
| 37 | * @return CIDR style string |
| 38 | */ |
| 39 | public static String cidrfy(LispEidRecord eidRecord) { |
| 40 | StringBuilder sb = new StringBuilder(); |
| 41 | sb.append(eidRecord.getPrefix().toString()); |
| 42 | sb.append("/"); |
| 43 | sb.append(eidRecord.getMaskLength()); |
| 44 | return sb.toString(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Checks whether the EID record is included in the given EID record. |
| 49 | * |
| 50 | * @param origin the EID record to be compared |
| 51 | * @param compare the EID record to compare |
| 52 | * @return boolean result |
| 53 | */ |
| 54 | public static boolean isInRange(LispEidRecord origin, LispEidRecord compare) { |
| 55 | |
| 56 | IpPrefix originIpPrefix = IpPrefix.valueOf(cidrfy(origin)); |
| 57 | IpPrefix compareIpPrefix = IpPrefix.valueOf(cidrfy(compare)); |
| 58 | |
| 59 | return originIpPrefix.contains(compareIpPrefix); |
| 60 | } |
| 61 | } |