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