blob: d38f505b99ed221cd26220adde43001326bfa231 [file] [log] [blame]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -07001package org.onlab.packet;
2
3import java.util.Objects;
4
5/**
6 * The class representing an IPv6 network address.
7 * This class is immutable.
8 */
9public final class Ip6Prefix {
10 private final Ip6Address address; // The IPv6 address
11 private final short prefixLen; // The prefix length
12
13 /**
14 * Default constructor.
15 */
16 public Ip6Prefix() {
17 this.address = new Ip6Address();
18 this.prefixLen = 0;
19 }
20
21 /**
22 * Copy constructor.
23 *
24 * @param other the object to copy from
25 */
26 public Ip6Prefix(Ip6Prefix other) {
27 this.address = new Ip6Address(other.address);
28 this.prefixLen = other.prefixLen;
29 }
30
31 /**
32 * Constructor for a given address and prefix length.
33 *
34 * @param address the address to use
35 * @param prefixLen the prefix length to use
36 */
37 public Ip6Prefix(Ip6Address address, short prefixLen) {
38 this.address = Ip6Address.makeMaskedAddress(address, prefixLen);
39 this.prefixLen = prefixLen;
40 }
41
42 /**
43 * Constructs an IPv6 prefix from a string representation of the
44 * prefix.
45 *<p>
46 * Example: "1111:2222::/32"
47 *
48 * @param value the value to use
49 */
50 public Ip6Prefix(String value) {
51 String[] splits = value.split("/");
52 if (splits.length != 2) {
53 throw new IllegalArgumentException("Specified IPv6 prefix must contain an IPv6 " +
54 "address and a prefix length separated by '/'");
55 }
56 this.prefixLen = Short.decode(splits[1]);
57 this.address = Ip6Address.makeMaskedAddress(new Ip6Address(splits[0]),
58 this.prefixLen);
59 }
60
61 /**
62 * Gets the address value of the IPv6 prefix.
63 *
64 * @return the address value of the IPv6 prefix
65 */
66 public Ip6Address getAddress() {
67 return address;
68 }
69
70 /**
71 * Gets the prefix length value of the IPv6 prefix.
72 *
73 * @return the prefix length value of the IPv6 prefix
74 */
75 public short getPrefixLen() {
76 return prefixLen;
77 }
78
79 /**
80 * Converts the IPv6 prefix value to an "address/prefixLen" string.
81 *
82 * @return the IPv6 prefix value as an "address/prefixLen" string
83 */
84 @Override
85 public String toString() {
86 return this.address.toString() + "/" + this.prefixLen;
87 }
88
89 /**
90 * Compares the value of two Ip6Prefix objects.
91 * <p/>
92 * Note the value of the IPv6 address is compared directly between the
93 * objects, and must match exactly for the objects to be considered equal.
94 * This may result in objects which represent the same IP prefix being
95 * classified as unequal, because the unsignificant bits of the address
96 * field don't match (the bits to the right of the prefix length).
97 * <p/>
98 * TODO Change this behavior so that objects that represent the same prefix
99 * are classified as equal according to this equals method.
100 *
101 * @see Object#equals(Object)
102 */
103 @Override
104 public boolean equals(Object other) {
105 if (other == this) {
106 return true;
107 }
108
109 if (!(other instanceof Ip6Prefix)) {
110 return false;
111 }
112
113 Ip6Prefix otherIp6Prefix = (Ip6Prefix) other;
114
115 return Objects.equals(this.address, otherIp6Prefix.address)
116 && this.prefixLen == otherIp6Prefix.prefixLen;
117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hash(address, prefixLen);
122 }
123}