blob: b9a475cd0e342baf8e3a7289300dae0207cfba46 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070019package org.onlab.packet;
20
21import java.util.Objects;
22
23/**
24 * The class representing an IPv6 network address.
25 * This class is immutable.
26 */
27public final class Ip6Prefix {
28 private final Ip6Address address; // The IPv6 address
29 private final short prefixLen; // The prefix length
30
31 /**
32 * Default constructor.
33 */
34 public Ip6Prefix() {
35 this.address = new Ip6Address();
36 this.prefixLen = 0;
37 }
38
39 /**
40 * Copy constructor.
41 *
42 * @param other the object to copy from
43 */
44 public Ip6Prefix(Ip6Prefix other) {
45 this.address = new Ip6Address(other.address);
46 this.prefixLen = other.prefixLen;
47 }
48
49 /**
50 * Constructor for a given address and prefix length.
51 *
52 * @param address the address to use
53 * @param prefixLen the prefix length to use
54 */
55 public Ip6Prefix(Ip6Address address, short prefixLen) {
56 this.address = Ip6Address.makeMaskedAddress(address, prefixLen);
57 this.prefixLen = prefixLen;
58 }
59
60 /**
61 * Constructs an IPv6 prefix from a string representation of the
62 * prefix.
63 *<p>
64 * Example: "1111:2222::/32"
65 *
66 * @param value the value to use
67 */
68 public Ip6Prefix(String value) {
69 String[] splits = value.split("/");
70 if (splits.length != 2) {
71 throw new IllegalArgumentException("Specified IPv6 prefix must contain an IPv6 " +
72 "address and a prefix length separated by '/'");
73 }
74 this.prefixLen = Short.decode(splits[1]);
75 this.address = Ip6Address.makeMaskedAddress(new Ip6Address(splits[0]),
76 this.prefixLen);
77 }
78
79 /**
80 * Gets the address value of the IPv6 prefix.
81 *
82 * @return the address value of the IPv6 prefix
83 */
84 public Ip6Address getAddress() {
85 return address;
86 }
87
88 /**
89 * Gets the prefix length value of the IPv6 prefix.
90 *
91 * @return the prefix length value of the IPv6 prefix
92 */
93 public short getPrefixLen() {
94 return prefixLen;
95 }
96
97 /**
98 * Converts the IPv6 prefix value to an "address/prefixLen" string.
99 *
100 * @return the IPv6 prefix value as an "address/prefixLen" string
101 */
102 @Override
103 public String toString() {
104 return this.address.toString() + "/" + this.prefixLen;
105 }
106
107 /**
108 * Compares the value of two Ip6Prefix objects.
109 * <p/>
110 * Note the value of the IPv6 address is compared directly between the
111 * objects, and must match exactly for the objects to be considered equal.
112 * This may result in objects which represent the same IP prefix being
113 * classified as unequal, because the unsignificant bits of the address
114 * field don't match (the bits to the right of the prefix length).
115 * <p/>
116 * TODO Change this behavior so that objects that represent the same prefix
117 * are classified as equal according to this equals method.
118 *
119 * @see Object#equals(Object)
120 */
121 @Override
122 public boolean equals(Object other) {
123 if (other == this) {
124 return true;
125 }
126
127 if (!(other instanceof Ip6Prefix)) {
128 return false;
129 }
130
131 Ip6Prefix otherIp6Prefix = (Ip6Prefix) other;
132
133 return Objects.equals(this.address, otherIp6Prefix.address)
134 && this.prefixLen == otherIp6Prefix.prefixLen;
135 }
136
137 @Override
138 public int hashCode() {
139 return Objects.hash(address, prefixLen);
140 }
141}