Pavlin Radoslavov

* Added methods IpAddress.getIp4Address() and IpAddress.getIp6Address()

  to get the Ip4Address and Ip6Address view of the IpAddress.

* Added methods IpPrefix.getIp4Prefix() and IpPrefix.getIp6Prefix()
  to get the Ip4Prefix and Ip6Prefix view of the IpPrefix.

Added the corresponding unit tests as well.
...@@ -80,6 +80,42 @@ public class IpAddress implements Comparable<IpAddress> { ...@@ -80,6 +80,42 @@ public class IpAddress implements Comparable<IpAddress> {
80 } 80 }
81 81
82 /** 82 /**
83 + * Gets the {@link Ip4Address} view of the IP address.
84 + *
85 + * @return the {@link Ip4Address} view of the IP address if it is IPv4,
86 + * otherwise null
87 + */
88 + public Ip4Address getIp4Address() {
89 + if (version() != Ip4Address.VERSION) {
90 + return null;
91 + }
92 +
93 + // Return this object itself if it is already instance of Ip4Address
94 + if (this instanceof Ip4Address) {
95 + return (Ip4Address) this;
96 + }
97 + return Ip4Address.valueOf(octets);
98 + }
99 +
100 + /**
101 + * Gets the {@link Ip6Address} view of the IP address.
102 + *
103 + * @return the {@link Ip6Address} view of the IP address if it is IPv6,
104 + * otherwise null
105 + */
106 + public Ip6Address getIp6Address() {
107 + if (version() != Ip6Address.VERSION) {
108 + return null;
109 + }
110 +
111 + // Return this object itself if it is already instance of Ip6Address
112 + if (this instanceof Ip6Address) {
113 + return (Ip6Address) this;
114 + }
115 + return Ip6Address.valueOf(octets);
116 + }
117 +
118 + /**
83 * Returns the IP address as a byte array. 119 * Returns the IP address as a byte array.
84 * 120 *
85 * @return a byte array 121 * @return a byte array
......
...@@ -75,6 +75,42 @@ public class IpPrefix { ...@@ -75,6 +75,42 @@ public class IpPrefix {
75 } 75 }
76 76
77 /** 77 /**
78 + * Gets the {@link Ip4Prefix} view of the IP prefix.
79 + *
80 + * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4,
81 + * otherwise null
82 + */
83 + public Ip4Prefix getIp4Prefix() {
84 + if (version() != Ip4Prefix.VERSION) {
85 + return null;
86 + }
87 +
88 + // Return this object itself if it is already instance of Ip4Prefix
89 + if (this instanceof Ip4Prefix) {
90 + return (Ip4Prefix) this;
91 + }
92 + return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength);
93 + }
94 +
95 + /**
96 + * Gets the {@link Ip6Prefix} view of the IP prefix.
97 + *
98 + * @return the {@link Ip6Prefix} view of the IP prefix if it is IPv6,
99 + * otherwise null
100 + */
101 + public Ip6Prefix getIp6Prefix() {
102 + if (version() != Ip6Prefix.VERSION) {
103 + return null;
104 + }
105 +
106 + // Return this object itself if it is already instance of Ip6Prefix
107 + if (this instanceof Ip6Prefix) {
108 + return (Ip6Prefix) this;
109 + }
110 + return Ip6Prefix.valueOf(address.getIp6Address(), prefixLength);
111 + }
112 +
113 + /**
78 * Converts an integer and a prefix length into an IPv4 prefix. 114 * Converts an integer and a prefix length into an IPv4 prefix.
79 * 115 *
80 * @param address an integer representing the IPv4 address 116 * @param address an integer representing the IPv4 address
......
...@@ -23,6 +23,7 @@ import org.junit.Test; ...@@ -23,6 +23,7 @@ import org.junit.Test;
23 import java.net.InetAddress; 23 import java.net.InetAddress;
24 24
25 import static org.hamcrest.Matchers.is; 25 import static org.hamcrest.Matchers.is;
26 +import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertThat; 27 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue; 28 import static org.junit.Assert.assertTrue;
28 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; 29 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
...@@ -77,6 +78,44 @@ public class IpAddressTest { ...@@ -77,6 +78,44 @@ public class IpAddressTest {
77 } 78 }
78 79
79 /** 80 /**
81 + * Tests getting the Ip4Address and Ip6Address view of the IP address.
82 + */
83 + @Test
84 + public void testGetIp4AndIp6AddressView() {
85 + IpAddress ipAddress;
86 + Ip4Address ip4Address;
87 + Ip6Address ip6Address;
88 +
89 + // Pure IPv4 IpAddress
90 + ipAddress = IpAddress.valueOf("1.2.3.4");
91 + ip4Address = ipAddress.getIp4Address();
92 + ip6Address = ipAddress.getIp6Address();
93 + assertThat(ip4Address.toString(), is("1.2.3.4"));
94 + assertNull(ip6Address);
95 +
96 + // IPv4 IpAddress that is Ip4Address
97 + ipAddress = Ip4Address.valueOf("1.2.3.4");
98 + ip4Address = ipAddress.getIp4Address();
99 + ip6Address = ipAddress.getIp6Address();
100 + assertThat(ip4Address.toString(), is("1.2.3.4"));
101 + assertNull(ip6Address);
102 +
103 + // Pure IPv6 IpAddress
104 + ipAddress = IpAddress.valueOf("1111:2222::");
105 + ip4Address = ipAddress.getIp4Address();
106 + ip6Address = ipAddress.getIp6Address();
107 + assertNull(ip4Address);
108 + assertThat(ip6Address.toString(), is("1111:2222::"));
109 +
110 + // IPv6 IpAddress that is Ip6Address
111 + ipAddress = Ip6Address.valueOf("1111:2222::");
112 + ip4Address = ipAddress.getIp4Address();
113 + ip6Address = ipAddress.getIp6Address();
114 + assertNull(ip4Address);
115 + assertThat(ip6Address.toString(), is("1111:2222::"));
116 + }
117 +
118 + /**
80 * Tests returning an IPv4 address as a byte array. 119 * Tests returning an IPv4 address as a byte array.
81 */ 120 */
82 @Test 121 @Test
......
...@@ -21,8 +21,9 @@ import org.junit.Test; ...@@ -21,8 +21,9 @@ import org.junit.Test;
21 21
22 import static org.hamcrest.Matchers.equalTo; 22 import static org.hamcrest.Matchers.equalTo;
23 import static org.hamcrest.Matchers.is; 23 import static org.hamcrest.Matchers.is;
24 -import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertFalse;
25 +import static org.junit.Assert.assertNull;
26 +import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue; 27 import static org.junit.Assert.assertTrue;
27 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; 28 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
28 29
...@@ -145,6 +146,44 @@ public class IpPrefixTest { ...@@ -145,6 +146,44 @@ public class IpPrefixTest {
145 } 146 }
146 147
147 /** 148 /**
149 + * Tests getting the Ip4Prefix and Ip6Prefix view of the IP prefix.
150 + */
151 + @Test
152 + public void testGetIp4AndIp6PrefixView() {
153 + IpPrefix ipPrefix;
154 + Ip4Prefix ip4Prefix;
155 + Ip6Prefix ip6Prefix;
156 +
157 + // Pure IPv4 IpPrefix
158 + ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
159 + ip4Prefix = ipPrefix.getIp4Prefix();
160 + ip6Prefix = ipPrefix.getIp6Prefix();
161 + assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
162 + assertNull(ip6Prefix);
163 +
164 + // IPv4 IpPrefix that is Ip4Prefix
165 + ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
166 + ip4Prefix = ipPrefix.getIp4Prefix();
167 + ip6Prefix = ipPrefix.getIp6Prefix();
168 + assertThat(ip4Prefix.toString(), is("1.2.3.0/24"));
169 + assertNull(ip6Prefix);
170 +
171 + // Pure IPv6 IpPrefix
172 + ipPrefix = IpPrefix.valueOf("1111:2222::/64");
173 + ip4Prefix = ipPrefix.getIp4Prefix();
174 + ip6Prefix = ipPrefix.getIp6Prefix();
175 + assertNull(ip4Prefix);
176 + assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
177 +
178 + // IPv6 IpPrefix that is Ip6Prefix
179 + ipPrefix = Ip6Prefix.valueOf("1111:2222::/64");
180 + ip4Prefix = ipPrefix.getIp4Prefix();
181 + ip6Prefix = ipPrefix.getIp6Prefix();
182 + assertNull(ip4Prefix);
183 + assertThat(ip6Prefix.toString(), is("1111:2222::/64"));
184 + }
185 +
186 + /**
148 * Tests valueOf() converter for IPv4 integer value. 187 * Tests valueOf() converter for IPv4 integer value.
149 */ 188 */
150 @Test 189 @Test
......