blob: 52e6535c548bb7a461344231d5c60e1cb4f37929 [file] [log] [blame]
HIGUCHI Yuta356086e2013-06-12 15:21:19 -07001package net.onrc.onos.ofcontroller.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
HIGUCHI Yutaf086d8a2013-06-12 15:26:35 -07003import net.onrc.onos.ofcontroller.util.serializers.IPv4NetDeserializer;
4import net.onrc.onos.ofcontroller.util.serializers.IPv4NetSerializer;
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 IPv4 network address.
11 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080012@JsonDeserialize(using=IPv4NetDeserializer.class)
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080013@JsonSerialize(using=IPv4NetSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014public class IPv4Net {
15 private IPv4 address; // The IPv4 address
16 private short prefixLen; // The prefix length
17
18 /**
19 * Default constructor.
20 */
21 public IPv4Net() {
22 this.prefixLen = 0;
23 }
24
25 /**
26 * Constructor for a given address and prefix length.
27 *
28 * @param address the address to use.
29 * @param prefixLen the prefix length to use.
30 */
31 public IPv4Net(IPv4 address, short prefixLen) {
32 this.address = address;
33 this.prefixLen = prefixLen;
34 }
35
36 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080037 * Constructor from a string.
38 *
39 * @param value the value to use.
40 */
41 public IPv4Net(String value) {
42 String[] splits = value.split("/");
43 if (splits.length != 2) {
44 throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " +
45 "address and a prefix length separated by '/'");
46 }
47 this.address = new IPv4(splits[0]);
48 this.prefixLen = Short.decode(splits[1]);
49 }
50
51 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080052 * Get the address value of the IPv4Net address.
53 *
54 * @return the address value of the IPv4Net address.
55 */
56 public IPv4 address() { return address; }
57
58 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080059 * Set the address value of the IPv4Net address.
60 *
61 * @param address the address to use.
62 */
63 public void setAddress(IPv4 address) {
64 this.address = address;
65 }
66
67 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080068 * Get the prefix length value of the IPv4Net address.
69 *
70 * @return the prefix length value of the IPv4Net address.
71 */
72 public short prefixLen() { return prefixLen; }
73
74 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080075 * Set the prefix length value of the IPv4Net address.
76 *
77 * @param prefixLen the prefix length to use.
78 */
79 public void setPrefixLen(short prefixLen) {
80 this.prefixLen = prefixLen;
81 }
82
83 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080084 * Set the value of the IPv4Net address.
85 *
86 * @param address the address to use.
87 * @param prefixLen the prefix length to use.
88 */
89 public void setValue(IPv4 address, short prefixLen) {
90 this.address = address;
91 this.prefixLen = prefixLen;
92 }
93
94 /**
95 * Convert the IPv4Net value to an "address/prefixLen" string.
96 *
97 * @return the IPv4Net value as an "address/prefixLen" string.
98 */
99 @Override
100 public String toString() {
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800101 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800102 }
103}