Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 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 | */ |
Hyunsun Moon | f948020 | 2016-04-14 16:13:42 -0700 | [diff] [blame^] | 16 | package org.onosproject.cordvtn.api; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * Representation of a network address, which consists of IP address and prefix. |
| 28 | */ |
| 29 | public final class NetworkAddress { |
| 30 | private final IpAddress ip; |
| 31 | private final IpPrefix prefix; |
| 32 | |
| 33 | /** |
| 34 | * Constructor for a given IP address and prefix. |
| 35 | * |
| 36 | * @param ip ip address |
| 37 | * @param prefix ip prefix |
| 38 | */ |
| 39 | public NetworkAddress(IpAddress ip, IpPrefix prefix) { |
| 40 | this.ip = ip; |
| 41 | this.prefix = prefix; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Converts a CIDR notation string into a network address. |
| 46 | * |
| 47 | * @param cidr cidr |
| 48 | * @return network address |
| 49 | * @throws IllegalArgumentException if the cidr is not valid |
| 50 | */ |
| 51 | public static NetworkAddress valueOf(String cidr) { |
| 52 | checkArgument(cidr.contains("/")); |
| 53 | |
| 54 | IpAddress ipAddress = IpAddress.valueOf(cidr.split("/")[0]); |
| 55 | IpPrefix ipPrefix = IpPrefix.valueOf(cidr); |
| 56 | |
| 57 | return new NetworkAddress(ipAddress, ipPrefix); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the IP address value of the network address. |
| 62 | * |
| 63 | * @return ip address |
| 64 | */ |
| 65 | public IpAddress ip() { |
| 66 | return this.ip; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the IP prefix value of the network address. |
| 71 | * |
| 72 | * @return ip prefix |
| 73 | */ |
| 74 | public IpPrefix prefix() { |
| 75 | return this.prefix; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Converts a network address to a CIDR notation. |
| 80 | * |
| 81 | * @return cidr notation string |
| 82 | */ |
| 83 | public String cidr() { |
| 84 | return ip.toString() + "/" + prefix.prefixLength(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object obj) { |
| 89 | if (this == obj) { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | if (obj instanceof NetworkAddress) { |
| 94 | NetworkAddress that = (NetworkAddress) obj; |
| 95 | if (Objects.equals(ip, that.ip) && Objects.equals(prefix, that.prefix)) { |
| 96 | return true; |
| 97 | } |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hash(ip, prefix); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("IpAddress", ip) |
| 111 | .add("IpPrefix", prefix) |
| 112 | .toString(); |
| 113 | } |
| 114 | } |