Added class InterfaceIpAddress and the corresponding unit test.
That class can be used to represent the IP address information on an interface.
Showing
2 changed files
with
157 additions
and
0 deletions
| 1 | +package org.onlab.onos.net.host; | ||
| 2 | + | ||
| 3 | +import java.util.Objects; | ||
| 4 | +import org.onlab.packet.IpAddress; | ||
| 5 | +import org.onlab.packet.IpPrefix; | ||
| 6 | + | ||
| 7 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
| 8 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Represents a single IP address information on an interface. | ||
| 12 | + * | ||
| 13 | + * TODO: | ||
| 14 | + * - Add computation for the default broadcast address if it is not | ||
| 15 | + * specified | ||
| 16 | + * - Add explicit checks that each IP address or prefix belong to the | ||
| 17 | + * same IP version: IPv4/IPv6. | ||
| 18 | + * - Inside the copy constructor we should use copy constructors for each | ||
| 19 | + * field | ||
| 20 | + */ | ||
| 21 | +public class InterfaceIpAddress { | ||
| 22 | + private final IpAddress ipAddress; | ||
| 23 | + private final IpPrefix subnetAddress; | ||
| 24 | + private final IpAddress broadcastAddress; | ||
| 25 | + private final IpAddress peerAddress; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * Copy constructor. | ||
| 29 | + * | ||
| 30 | + * @param other the object to copy from | ||
| 31 | + */ | ||
| 32 | + public InterfaceIpAddress(InterfaceIpAddress other) { | ||
| 33 | + // TODO: we should use copy constructors for each field | ||
| 34 | + this.ipAddress = other.ipAddress; | ||
| 35 | + this.subnetAddress = other.subnetAddress; | ||
| 36 | + this.broadcastAddress = other.broadcastAddress; | ||
| 37 | + this.peerAddress = other.peerAddress; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Constructor for a given IP address and a subnet address. | ||
| 42 | + * | ||
| 43 | + * @param ipAddress the IP address | ||
| 44 | + * @param subnetAddress the IP subnet address | ||
| 45 | + */ | ||
| 46 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) { | ||
| 47 | + this.ipAddress = checkNotNull(ipAddress); | ||
| 48 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
| 49 | + // TODO: Recompute the default broadcast address from the subnet | ||
| 50 | + // address | ||
| 51 | + this.broadcastAddress = null; | ||
| 52 | + this.peerAddress = null; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Constructor for a given IP address and a subnet address. | ||
| 57 | + * | ||
| 58 | + * @param ipAddress the IP address | ||
| 59 | + * @param subnetAddress the IP subnet address | ||
| 60 | + * @param broadcastAddress the IP broadcast address. It can be used | ||
| 61 | + * to specify non-default broadcast address | ||
| 62 | + */ | ||
| 63 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, | ||
| 64 | + IpAddress broadcastAddress) { | ||
| 65 | + this.ipAddress = checkNotNull(ipAddress); | ||
| 66 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
| 67 | + this.broadcastAddress = broadcastAddress; | ||
| 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. It should be null for | ||
| 78 | + * point-to-point interfaces with a peer address | ||
| 79 | + * @param peerAddress the peer IP address for point-to-point interfaces | ||
| 80 | + */ | ||
| 81 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, | ||
| 82 | + IpAddress broadcastAddress, | ||
| 83 | + IpAddress peerAddress) { | ||
| 84 | + this.ipAddress = checkNotNull(ipAddress); | ||
| 85 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
| 86 | + this.broadcastAddress = broadcastAddress; | ||
| 87 | + this.peerAddress = peerAddress; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * Gets the IP address. | ||
| 92 | + * | ||
| 93 | + * @return the IP address | ||
| 94 | + */ | ||
| 95 | + public IpAddress ipAddress() { | ||
| 96 | + return ipAddress; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + /** | ||
| 100 | + * Gets the IP subnet address. | ||
| 101 | + * | ||
| 102 | + * @return the IP subnet address | ||
| 103 | + */ | ||
| 104 | + public IpPrefix subnetAddress() { | ||
| 105 | + return subnetAddress; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * Gets the subnet IP broadcast address. | ||
| 110 | + * | ||
| 111 | + * @return the subnet IP broadcast address | ||
| 112 | + */ | ||
| 113 | + public IpAddress broadcastAddress() { | ||
| 114 | + return broadcastAddress; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * Gets the IP point-to-point interface peer address. | ||
| 119 | + * | ||
| 120 | + * @return the IP point-to-point interface peer address | ||
| 121 | + */ | ||
| 122 | + public IpAddress peerAddress() { | ||
| 123 | + return peerAddress; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Override | ||
| 127 | + public boolean equals(Object other) { | ||
| 128 | + if (other == this) { | ||
| 129 | + return true; | ||
| 130 | + } | ||
| 131 | + if (!(other instanceof InterfaceIpAddress)) { | ||
| 132 | + return false; | ||
| 133 | + } | ||
| 134 | + InterfaceIpAddress otherAddr = (InterfaceIpAddress) other; | ||
| 135 | + | ||
| 136 | + return Objects.equals(this.ipAddress, otherAddr.ipAddress) | ||
| 137 | + && Objects.equals(this.subnetAddress, otherAddr.subnetAddress) | ||
| 138 | + && Objects.equals(this.broadcastAddress, | ||
| 139 | + otherAddr.broadcastAddress) | ||
| 140 | + && Objects.equals(this.peerAddress, otherAddr.peerAddress); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + @Override | ||
| 144 | + public int hashCode() { | ||
| 145 | + return Objects.hash(ipAddress, subnetAddress, broadcastAddress, | ||
| 146 | + peerAddress); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + @Override | ||
| 150 | + public String toString() { | ||
| 151 | + return toStringHelper(this).add("ipAddress", ipAddress) | ||
| 152 | + .add("subnetAddress", subnetAddress) | ||
| 153 | + .add("broadcastAddress", broadcastAddress) | ||
| 154 | + .add("peerAddress", peerAddress) | ||
| 155 | + .omitNullValues().toString(); | ||
| 156 | + } | ||
| 157 | +} |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment