blob: d0632c5bb057e29d8087384848de52d4efa0c9e4 [file] [log] [blame]
Yotam Harchol4d634682013-09-26 13:21:06 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchol4d634682013-09-26 13:21:06 -07003
4public abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> {
5
6 protected IPAddressWithMask(F value, F mask) {
7 super(value, mask);
8 }
9
Yotam Harcholeb023dc2013-09-26 15:45:44 -070010 public abstract IPVersion getIpVersion();
Yotam Harchol4d634682013-09-26 13:21:06 -070011
12 public static IPAddressWithMask<?> of(String ip) {
13 if (ip.indexOf('.') != -1)
14 return IPv4AddressWithMask.of(ip);
15 else if (ip.indexOf(':') != -1)
16 return IPv6AddressWithMask.of(ip);
17 else
18 throw new IllegalArgumentException("IP Address not well formed: " + ip);
19 }
20
Gregor Maier7f987e62013-12-10 19:34:18 -080021 public String toString() {
22 StringBuilder res = new StringBuilder();
23 res.append(value.toString());
24
25 res.append('/');
26 if (mask.asCidrMaskLength() != -1) {
27 // CIDR notation
28 res.append(mask.asCidrMaskLength());
29 } else {
30 // Full address mask
31 res.append(mask.toString());
32 }
33
34 return res.toString();
35 }
36
Yotam Harchol4d634682013-09-26 13:21:06 -070037}