blob: 72ec5e990fc1a9f2eceb664c235edd40dbedab98 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Jonathan Hart23701d12014-04-03 10:45:48 -07003import net.onrc.onos.core.util.serializers.IPv6NetDeserializer;
4import net.onrc.onos.core.util.serializers.IPv6NetSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08006import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08008
9/**
10 * The class representing an IPv6 network address.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070011 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012 */
Ray Milkey269ffb92014-04-03 14:43:30 -070013@JsonDeserialize(using = IPv6NetDeserializer.class)
14@JsonSerialize(using = IPv6NetSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070015public final class IPv6Net {
16 private final IPv6 address; // The IPv6 address
17 private final short prefixLen; // The prefix length
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080018
19 /**
20 * Default constructor.
21 */
22 public IPv6Net() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070023 this.address = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070024 this.prefixLen = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080025 }
26
27 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070028 * Copy constructor.
29 *
30 * @param other the object to copy from.
31 */
32 public IPv6Net(IPv6Net other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 if (other.address != null)
34 this.address = new IPv6(other.address);
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070035 else
36 this.address = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070037 this.prefixLen = other.prefixLen;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070038 }
39
40 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080041 * Constructor for a given address and prefix length.
42 *
Ray Milkey269ffb92014-04-03 14:43:30 -070043 * @param address the address to use.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080044 * @param prefixLen the prefix length to use.
45 */
46 public IPv6Net(IPv6 address, short prefixLen) {
Ray Milkey269ffb92014-04-03 14:43:30 -070047 this.address = address;
48 this.prefixLen = prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080049 }
50
51 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080052 * Constructor from a string.
53 *
54 * @param value the value to use.
55 */
56 public IPv6Net(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070057 String[] splits = value.split("/");
58 if (splits.length != 2) {
59 throw new IllegalArgumentException("Specified IPv6Net address must contain an IPv6 " +
60 "address and a prefix length separated by '/'");
61 }
62 this.address = new IPv6(splits[0]);
63 this.prefixLen = Short.decode(splits[1]);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080064 }
65
66 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080067 * Get the address value of the IPv6Net address.
68 *
69 * @return the address value of the IPv6Net address.
70 */
Ray Milkey269ffb92014-04-03 14:43:30 -070071 public IPv6 address() {
72 return address;
73 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080074
75 /**
76 * Get the prefix length value of the IPv6Net address.
77 *
78 * @return the prefix length value of the IPv6Net address.
79 */
Ray Milkey269ffb92014-04-03 14:43:30 -070080 public short prefixLen() {
81 return prefixLen;
82 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080083
84 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080085 * Convert the IPv6Net value to an "address/prefixLen" string.
86 *
87 * @return the IPv6Net value as an "address/prefixLen" string.
88 */
89 @Override
90 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080092 }
93}