blob: 824e3e2a7662da7da935d11f7c12347696519d7d [file] [log] [blame]
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08001package net.floodlightcontroller.util;
2
3import net.floodlightcontroller.util.IPv4;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08004import net.floodlightcontroller.util.serializers.IPv4NetDeserializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005import net.floodlightcontroller.util.serializers.IPv4NetSerializer;
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 IPv4 network address.
13 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080014@JsonDeserialize(using=IPv4NetDeserializer.class)
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080015@JsonSerialize(using=IPv4NetSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080016public class IPv4Net {
17 private IPv4 address; // The IPv4 address
18 private short prefixLen; // The prefix length
19
20 /**
21 * Default constructor.
22 */
23 public IPv4Net() {
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 IPv4Net(IPv4 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 IPv4Net(String value) {
44 String[] splits = value.split("/");
45 if (splits.length != 2) {
46 throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " +
47 "address and a prefix length separated by '/'");
48 }
49 this.address = new IPv4(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 IPv4Net address.
55 *
56 * @return the address value of the IPv4Net address.
57 */
58 public IPv4 address() { return address; }
59
60 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080061 * Set the address value of the IPv4Net address.
62 *
63 * @param address the address to use.
64 */
65 public void setAddress(IPv4 address) {
66 this.address = address;
67 }
68
69 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080070 * Get the prefix length value of the IPv4Net address.
71 *
72 * @return the prefix length value of the IPv4Net 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 IPv4Net 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 IPv4Net address.
87 *
88 * @param address the address to use.
89 * @param prefixLen the prefix length to use.
90 */
91 public void setValue(IPv4 address, short prefixLen) {
92 this.address = address;
93 this.prefixLen = prefixLen;
94 }
95
96 /**
97 * Convert the IPv4Net value to an "address/prefixLen" string.
98 *
99 * @return the IPv4Net 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}