blob: a7f3d24608555218be0052be1b7aa545843f8d9d [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.IPv4Deserializer;
4import net.onrc.onos.core.util.serializers.IPv4Serializer;
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;
8
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08009/**
10 * The class representing an IPv4 address.
11 */
Ray Milkey269ffb92014-04-03 14:43:30 -070012@JsonDeserialize(using = IPv4Deserializer.class)
13@JsonSerialize(using = IPv4Serializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014public class IPv4 {
15 private int value;
16
17 /**
18 * Default constructor.
19 */
20 public IPv4() {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 this.value = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080022 }
23
24 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070025 * Copy constructor.
26 *
27 * @param other the object to copy from.
28 */
29 public IPv4(IPv4 other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 this.value = other.value;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070031 }
32
33 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080034 * Constructor from an integer value.
35 *
36 * @param value the value to use.
37 */
38 public IPv4(int value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080040 }
41
42 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080043 * Constructor from a string.
44 *
45 * @param value the value to use.
46 */
47 public IPv4(String value) {
48 String[] splits = value.split("\\.");
49 if (splits.length != 4)
50 throw new IllegalArgumentException("Specified IPv4 address must contain four " +
Ray Milkey269ffb92014-04-03 14:43:30 -070051 "numerical digits separated by '.'");
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080052
53 int result = 0;
54 for (int i = 0; i < 4; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 result |= Integer.valueOf(splits[i]) << ((3 - i) * 8);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080056 }
Ray Milkey269ffb92014-04-03 14:43:30 -070057 this.value = result;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080058 }
59
60 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080061 * Get the value of the IPv4 address.
62 *
63 * @return the value of the IPv4 address.
64 */
Ray Milkey269ffb92014-04-03 14:43:30 -070065 public int value() {
66 return value;
67 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080068
69 /**
70 * Set the value of the IPv4 address.
71 *
72 * @param value the value to set.
73 */
74 public void setValue(int value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080076 }
77
78 /**
79 * Convert the IPv4 value to a '.' separated string.
80 *
81 * @return the IPv4 value as a '.' separated string.
82 */
83 @Override
84 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return ((this.value >> 24) & 0xFF) + "." +
86 ((this.value >> 16) & 0xFF) + "." +
87 ((this.value >> 8) & 0xFF) + "." +
88 (this.value & 0xFF);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080089 }
90}