blob: 9ce247c65240f4b9ebf8f41e3e6bedd21298976c [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 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070026 * Copy constructor.
27 *
28 * @param other the object to copy from.
29 */
30 public IPv4Net(IPv4Net other) {
31 if (other.address != null)
32 this.address = new IPv4(other.address);
33 this.prefixLen = other.prefixLen;
34 }
35
36 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080037 * Constructor for a given address and prefix length.
38 *
39 * @param address the address to use.
40 * @param prefixLen the prefix length to use.
41 */
42 public IPv4Net(IPv4 address, short prefixLen) {
43 this.address = address;
44 this.prefixLen = prefixLen;
45 }
46
47 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080048 * Constructor from a string.
49 *
50 * @param value the value to use.
51 */
52 public IPv4Net(String value) {
53 String[] splits = value.split("/");
54 if (splits.length != 2) {
55 throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " +
56 "address and a prefix length separated by '/'");
57 }
58 this.address = new IPv4(splits[0]);
59 this.prefixLen = Short.decode(splits[1]);
60 }
61
62 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080063 * Get the address value of the IPv4Net address.
64 *
65 * @return the address value of the IPv4Net address.
66 */
67 public IPv4 address() { return address; }
68
69 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080070 * Set the address value of the IPv4Net address.
71 *
72 * @param address the address to use.
73 */
74 public void setAddress(IPv4 address) {
75 this.address = address;
76 }
77
78 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080079 * Get the prefix length value of the IPv4Net address.
80 *
81 * @return the prefix length value of the IPv4Net address.
82 */
83 public short prefixLen() { return prefixLen; }
84
85 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080086 * Set the prefix length value of the IPv4Net address.
87 *
88 * @param prefixLen the prefix length to use.
89 */
90 public void setPrefixLen(short prefixLen) {
91 this.prefixLen = prefixLen;
92 }
93
94 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080095 * Set the value of the IPv4Net address.
96 *
97 * @param address the address to use.
98 * @param prefixLen the prefix length to use.
99 */
100 public void setValue(IPv4 address, short prefixLen) {
101 this.address = address;
102 this.prefixLen = prefixLen;
103 }
104
105 /**
106 * Convert the IPv4Net value to an "address/prefixLen" string.
107 *
108 * @return the IPv4Net value as an "address/prefixLen" string.
109 */
110 @Override
111 public String toString() {
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800112 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800113 }
114}