blob: 5ef301bc6f565cd4190737cc9b6bbd8e4aa71dd0 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
5
6
7/**
Yotam Harchola289d552013-09-16 10:10:40 -07008 * Wrapper around an IPv4Address address
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 *
10 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
11 */
Yotam Harchola289d552013-09-16 10:10:40 -070012public class IPv4Address implements OFValueType<IPv4Address> {
Yotam Harcholf3f11152013-09-05 16:47:16 -070013 static final int LENGTH = 4;
14 private final int rawValue;
Yotam Harcholf3f11152013-09-05 16:47:16 -070015
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070016 private final static int NONE_VAL = 0x0;
17 public final static IPv4Address NONE = new IPv4Address(NONE_VAL);
18
Yotam Harchola289d552013-09-16 10:10:40 -070019 public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF);
20 public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000);
21
22 private IPv4Address(final int rawValue) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070023 this.rawValue = rawValue;
24 }
25
Yotam Harchola289d552013-09-16 10:10:40 -070026 public static IPv4Address of(final byte[] address) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 if (address.length != LENGTH) {
28 throw new IllegalArgumentException(
Yotam Harchola289d552013-09-16 10:10:40 -070029 "Invalid byte array length for IPv4Address address: " + address);
Yotam Harcholf3f11152013-09-05 16:47:16 -070030 }
31
32 int raw =
33 (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16
34 | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0;
Yotam Harchola289d552013-09-16 10:10:40 -070035 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 }
37
Yotam Harchola289d552013-09-16 10:10:40 -070038 public static IPv4Address of(final int raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070039 if(raw == NONE_VAL)
40 return NONE;
Yotam Harchola289d552013-09-16 10:10:40 -070041 return new IPv4Address(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070042 }
43
Yotam Harchola289d552013-09-16 10:10:40 -070044 public static IPv4Address of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070045 int start = 0;
46 int shift = 24;
47
48 int raw = 0;
49 while (shift >= 0) {
50 int end = string.indexOf('.', start);
51 if (end == start || !((shift > 0) ^ (end < 0)))
52 throw new IllegalArgumentException("IP Address not well formed: " + string);
53
54 String substr =
55 end > 0 ? string.substring(start, end) : string.substring(start);
56 int val = Integer.parseInt(substr);
57 if (val < 0 || val > 255)
58 throw new IllegalArgumentException("IP Address not well formed: " + string);
59
60 raw |= val << shift;
61
62 shift -= 8;
63 start = end + 1;
64 }
Yotam Harchola289d552013-09-16 10:10:40 -070065 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 }
67
68 public int getInt() {
69 return rawValue;
70 }
71
72 volatile byte[] bytesCache = null;
73
74 public byte[] getBytes() {
75 if (bytesCache == null) {
76 synchronized (this) {
77 if (bytesCache == null) {
78 bytesCache =
79 new byte[] { (byte) ((rawValue >>> 24) & 0xFF),
80 (byte) ((rawValue >>> 16) & 0xFF),
81 (byte) ((rawValue >>> 8) & 0xFF),
82 (byte) ((rawValue >>> 0) & 0xFF) };
83 }
84 }
85 }
86 return bytesCache;
87 }
88
89 @Override
90 public int getLength() {
91 return LENGTH;
92 }
93
94 @Override
95 public String toString() {
96 StringBuilder res = new StringBuilder();
97 res.append((rawValue >> 24) & 0xFF).append('.');
98 res.append((rawValue >> 16) & 0xFF).append('.');
99 res.append((rawValue >> 8) & 0xFF).append('.');
100 res.append((rawValue >> 0) & 0xFF);
101 return res.toString();
102 }
103
104 @Override
105 public int hashCode() {
106 final int prime = 31;
107 int result = 1;
108 result = prime * result + rawValue;
109 return result;
110 }
111
112 @Override
113 public boolean equals(final Object obj) {
114 if (this == obj)
115 return true;
116 if (obj == null)
117 return false;
118 if (getClass() != obj.getClass())
119 return false;
Yotam Harchola289d552013-09-16 10:10:40 -0700120 IPv4Address other = (IPv4Address) obj;
Yotam Harcholf3f11152013-09-05 16:47:16 -0700121 if (rawValue != other.rawValue)
122 return false;
123 return true;
124 }
Yotam Harchola289d552013-09-16 10:10:40 -0700125
Yotam Harcholf3f11152013-09-05 16:47:16 -0700126 public void write4Bytes(ChannelBuffer c) {
127 c.writeInt(rawValue);
128 }
Yotam Harchola289d552013-09-16 10:10:40 -0700129
130 public static IPv4Address read4Bytes(ChannelBuffer c) {
131 return IPv4Address.of(c.readInt());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700132 }
133
134 @Override
Yotam Harchola289d552013-09-16 10:10:40 -0700135 public IPv4Address applyMask(IPv4Address mask) {
136 return IPv4Address.of(this.rawValue & mask.rawValue);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700137 }
138
Yotam Harchola289d552013-09-16 10:10:40 -0700139
Yotam Harcholf3f11152013-09-05 16:47:16 -0700140}