blob: 654e72c2bd60981627a1bb0e3983363cfe51a5b6 [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) {
Gregor Maier1ff55972013-12-11 02:22:56 -080013 if (ip == null) {
14 throw new NullPointerException("String ip must not be null");
15 }
Yotam Harchol4d634682013-09-26 13:21:06 -070016 if (ip.indexOf('.') != -1)
17 return IPv4AddressWithMask.of(ip);
18 else if (ip.indexOf(':') != -1)
19 return IPv6AddressWithMask.of(ip);
20 else
21 throw new IllegalArgumentException("IP Address not well formed: " + ip);
22 }
23
Gregor Maier1ff55972013-12-11 02:22:56 -080024 @Override
Gregor Maier7f987e62013-12-10 19:34:18 -080025 public String toString() {
26 StringBuilder res = new StringBuilder();
27 res.append(value.toString());
28
29 res.append('/');
30 if (mask.asCidrMaskLength() != -1) {
31 // CIDR notation
32 res.append(mask.asCidrMaskLength());
33 } else {
34 // Full address mask
35 res.append(mask.toString());
36 }
37
38 return res.toString();
39 }
40
Yotam Harchol4d634682013-09-26 13:21:06 -070041}