blob: ada2d38724113338d8f667856e7e10c253903609 [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
Aditya Vaja56b8b182014-03-11 13:13:58 -070012 public IPAddress<?> getSubnetBroadcastAddress() {
13 return value.or(mask.not());
14 }
15
16 public boolean isSubnetBroadcastAddress(IPAddress<?> candidate) {
17 return getSubnetBroadcastAddress().equals(candidate);
18 }
19
Yotam Harchol4d634682013-09-26 13:21:06 -070020 public static IPAddressWithMask<?> of(String ip) {
Gregor Maier1ff55972013-12-11 02:22:56 -080021 if (ip == null) {
22 throw new NullPointerException("String ip must not be null");
23 }
Yotam Harchol4d634682013-09-26 13:21:06 -070024 if (ip.indexOf('.') != -1)
25 return IPv4AddressWithMask.of(ip);
26 else if (ip.indexOf(':') != -1)
27 return IPv6AddressWithMask.of(ip);
28 else
29 throw new IllegalArgumentException("IP Address not well formed: " + ip);
30 }
31
Gregor Maier1ff55972013-12-11 02:22:56 -080032 @Override
Gregor Maier7f987e62013-12-10 19:34:18 -080033 public String toString() {
34 StringBuilder res = new StringBuilder();
35 res.append(value.toString());
36
37 res.append('/');
Gregor Maier5615b6c2013-12-11 22:29:07 -080038 if (mask.isCidrMask()) {
Gregor Maier7f987e62013-12-10 19:34:18 -080039 // CIDR notation
40 res.append(mask.asCidrMaskLength());
41 } else {
42 // Full address mask
43 res.append(mask.toString());
44 }
45
46 return res.toString();
47 }
48
Yotam Harchol4d634682013-09-26 13:21:06 -070049}