Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host; |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * Represents a single IP address information on an interface. |
| 28 | * |
| 29 | * TODO: |
| 30 | * - Add computation for the default broadcast address if it is not |
| 31 | * specified |
| 32 | * - Add explicit checks that each IP address or prefix belong to the |
| 33 | * same IP version: IPv4/IPv6. |
| 34 | * - Inside the copy constructor we should use copy constructors for each |
| 35 | * field |
| 36 | */ |
| 37 | public class InterfaceIpAddress { |
| 38 | private final IpAddress ipAddress; |
| 39 | private final IpPrefix subnetAddress; |
| 40 | private final IpAddress broadcastAddress; |
| 41 | private final IpAddress peerAddress; |
| 42 | |
| 43 | /** |
| 44 | * Copy constructor. |
| 45 | * |
| 46 | * @param other the object to copy from |
| 47 | */ |
| 48 | public InterfaceIpAddress(InterfaceIpAddress other) { |
| 49 | // TODO: we should use copy constructors for each field |
| 50 | this.ipAddress = other.ipAddress; |
| 51 | this.subnetAddress = other.subnetAddress; |
| 52 | this.broadcastAddress = other.broadcastAddress; |
| 53 | this.peerAddress = other.peerAddress; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Constructor for a given IP address and a subnet address. |
| 58 | * |
| 59 | * @param ipAddress the IP address |
| 60 | * @param subnetAddress the IP subnet address |
| 61 | */ |
| 62 | public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) { |
| 63 | this.ipAddress = checkNotNull(ipAddress); |
| 64 | this.subnetAddress = checkNotNull(subnetAddress); |
| 65 | // TODO: Recompute the default broadcast address from the subnet |
| 66 | // address |
| 67 | this.broadcastAddress = null; |
| 68 | this.peerAddress = null; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Constructor for a given IP address and a subnet address. |
| 73 | * |
| 74 | * @param ipAddress the IP address |
| 75 | * @param subnetAddress the IP subnet address |
| 76 | * @param broadcastAddress the IP broadcast address. It can be used |
| 77 | * to specify non-default broadcast address |
| 78 | */ |
| 79 | public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, |
| 80 | IpAddress broadcastAddress) { |
| 81 | this.ipAddress = checkNotNull(ipAddress); |
| 82 | this.subnetAddress = checkNotNull(subnetAddress); |
| 83 | this.broadcastAddress = broadcastAddress; |
| 84 | this.peerAddress = null; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Constructor for a given IP address and a subnet address. |
| 89 | * |
| 90 | * @param ipAddress the IP address |
| 91 | * @param subnetAddress the IP subnet address |
| 92 | * @param broadcastAddress the IP broadcast address. It can be used |
| 93 | * to specify non-default broadcast address. It should be null for |
| 94 | * point-to-point interfaces with a peer address |
| 95 | * @param peerAddress the peer IP address for point-to-point interfaces |
| 96 | */ |
| 97 | public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, |
| 98 | IpAddress broadcastAddress, |
| 99 | IpAddress peerAddress) { |
| 100 | this.ipAddress = checkNotNull(ipAddress); |
| 101 | this.subnetAddress = checkNotNull(subnetAddress); |
| 102 | this.broadcastAddress = broadcastAddress; |
| 103 | this.peerAddress = peerAddress; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Gets the IP address. |
| 108 | * |
| 109 | * @return the IP address |
| 110 | */ |
| 111 | public IpAddress ipAddress() { |
| 112 | return ipAddress; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets the IP subnet address. |
| 117 | * |
| 118 | * @return the IP subnet address |
| 119 | */ |
| 120 | public IpPrefix subnetAddress() { |
| 121 | return subnetAddress; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Gets the subnet IP broadcast address. |
| 126 | * |
| 127 | * @return the subnet IP broadcast address |
| 128 | */ |
| 129 | public IpAddress broadcastAddress() { |
| 130 | return broadcastAddress; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets the IP point-to-point interface peer address. |
| 135 | * |
| 136 | * @return the IP point-to-point interface peer address |
| 137 | */ |
| 138 | public IpAddress peerAddress() { |
| 139 | return peerAddress; |
| 140 | } |
| 141 | |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Converts a CIDR string literal to an interface IP address. |
| 144 | * E.g. 10.0.0.1/24 |
| 145 | * |
| 146 | * @param value an IP address value in string form |
| 147 | * @return an interface IP address |
| 148 | * @throws IllegalArgumentException if the argument is invalid |
| 149 | */ |
| 150 | public static InterfaceIpAddress valueOf(String value) { |
| 151 | String[] splits = value.split("/"); |
| 152 | checkArgument(splits.length == 2, "Invalid IP address and prefix length format"); |
| 153 | |
| 154 | // NOTE: IpPrefix will mask-out the bits after the prefix length. |
| 155 | IpPrefix subnet = IpPrefix.valueOf(value); |
| 156 | IpAddress addr = IpAddress.valueOf(splits[0]); |
| 157 | return new InterfaceIpAddress(addr, subnet); |
| 158 | } |
| 159 | |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 160 | @Override |
| 161 | public boolean equals(Object other) { |
| 162 | if (other == this) { |
| 163 | return true; |
| 164 | } |
| 165 | if (!(other instanceof InterfaceIpAddress)) { |
| 166 | return false; |
| 167 | } |
| 168 | InterfaceIpAddress otherAddr = (InterfaceIpAddress) other; |
| 169 | |
| 170 | return Objects.equals(this.ipAddress, otherAddr.ipAddress) |
| 171 | && Objects.equals(this.subnetAddress, otherAddr.subnetAddress) |
| 172 | && Objects.equals(this.broadcastAddress, |
| 173 | otherAddr.broadcastAddress) |
| 174 | && Objects.equals(this.peerAddress, otherAddr.peerAddress); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public int hashCode() { |
| 179 | return Objects.hash(ipAddress, subnetAddress, broadcastAddress, |
| 180 | peerAddress); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String toString() { |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 185 | /*return toStringHelper(this).add("ipAddress", ipAddress) |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 186 | .add("subnetAddress", subnetAddress) |
| 187 | .add("broadcastAddress", broadcastAddress) |
| 188 | .add("peerAddress", peerAddress) |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 189 | .omitNullValues().toString();*/ |
| 190 | return ipAddress.toString() + "/" + subnetAddress.prefixLength(); |
Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 191 | } |
| 192 | } |