blob: b6f7d6715db76b8f28a426b22e81f14d0d5c3024 [file] [log] [blame]
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08001package net.floodlightcontroller.util;
2
3import net.floodlightcontroller.util.IPv6;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08004import net.floodlightcontroller.util.serializers.IPv6NetDeserializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005import net.floodlightcontroller.util.serializers.IPv6NetSerializer;
6
7import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080010
11/**
12 * The class representing an IPv6 network address.
13 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080014@JsonDeserialize(using=IPv6NetDeserializer.class)
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080015@JsonSerialize(using=IPv6NetSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080016public class IPv6Net {
17 private IPv6 address; // The IPv6 address
18 private short prefixLen; // The prefix length
19
20 /**
21 * Default constructor.
22 */
23 public IPv6Net() {
24 this.prefixLen = 0;
25 }
26
27 /**
28 * Constructor for a given address and prefix length.
29 *
30 * @param address the address to use.
31 * @param prefixLen the prefix length to use.
32 */
33 public IPv6Net(IPv6 address, short prefixLen) {
34 this.address = address;
35 this.prefixLen = prefixLen;
36 }
37
38 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080039 * Constructor from a string.
40 *
41 * @param value the value to use.
42 */
43 public IPv6Net(String value) {
44 String[] splits = value.split("/");
45 if (splits.length != 2) {
46 throw new IllegalArgumentException("Specified IPv6Net address must contain an IPv6 " +
47 "address and a prefix length separated by '/'");
48 }
49 this.address = new IPv6(splits[0]);
50 this.prefixLen = Short.decode(splits[1]);
51 }
52
53 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080054 * Get the address value of the IPv6Net address.
55 *
56 * @return the address value of the IPv6Net address.
57 */
58 public IPv6 address() { return address; }
59
60 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080061 * Set the address value of the IPv6Net address.
62 *
63 * @param address the address to use.
64 */
65 public void setAddress(IPv6 address) {
66 this.address = address;
67 }
68
69 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080070 * Get the prefix length value of the IPv6Net address.
71 *
72 * @return the prefix length value of the IPv6Net address.
73 */
74 public short prefixLen() { return prefixLen; }
75
76 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080077 * Set the prefix length value of the IPv6Net address.
78 *
79 * @param prefixLen the prefix length to use.
80 */
81 public void setPrefixLen(short prefixLen) {
82 this.prefixLen = prefixLen;
83 }
84
85 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080086 * Set the value of the IPv6Net address.
87 *
88 * @param address the address to use.
89 * @param prefixLen the prefix length to use.
90 */
91 public void setValue(IPv6 address, short prefixLen) {
92 this.address = address;
93 this.prefixLen = prefixLen;
94 }
95
96 /**
97 * Convert the IPv6Net value to an "address/prefixLen" string.
98 *
99 * @return the IPv6Net value as an "address/prefixLen" string.
100 */
101 @Override
102 public String toString() {
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800103 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800104 }
105}