blob: 7cd80993ef5df80f02ea1d472267391ebd6b44ac [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -07001package org.projectfloodlight.openflow.types;
2
alshabib86ac11c2014-08-14 16:14:41 -07003import com.google.common.base.Preconditions;
4
alshabib1f44e8e2014-08-14 15:19:57 -07005
6public abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> {
7
8 protected IPAddressWithMask(F value, F mask) {
9 super(value, mask);
10 }
11
12 public abstract IPVersion getIpVersion();
alshabib86ac11c2014-08-14 16:14:41 -070013
14 public abstract boolean contains(IPAddress<?> ip);
alshabib1f44e8e2014-08-14 15:19:57 -070015
16 public F getSubnetBroadcastAddress() {
17 if (!mask.isCidrMask()) {
18 throw new IllegalArgumentException("Mask Invalid " + mask +
19 " cannot get subnet for non CIDR mask");
20 }
21 return value.or(mask.not());
22 }
23
24 public boolean isSubnetBroadcastAddress(F candidate) {
25 return getSubnetBroadcastAddress().equals(candidate);
26 }
27
28 public static IPAddressWithMask<?> of(String ip) {
alshabib86ac11c2014-08-14 16:14:41 -070029 Preconditions.checkNotNull(ip, "string ip must not be null");
30
alshabib1f44e8e2014-08-14 15:19:57 -070031 if (ip.indexOf('.') != -1)
32 return IPv4AddressWithMask.of(ip);
33 else if (ip.indexOf(':') != -1)
34 return IPv6AddressWithMask.of(ip);
35 else
36 throw new IllegalArgumentException("IP Address not well formed: " + ip);
37 }
38
39 @Override
40 public String toString() {
41 StringBuilder res = new StringBuilder();
42 res.append(value.toString());
43
44 res.append('/');
45 if (mask.isCidrMask()) {
46 // CIDR notation
47 res.append(mask.asCidrMaskLength());
48 } else {
49 // Full address mask
50 res.append(mask.toString());
51 }
52
53 return res.toString();
54 }
alshabib1f44e8e2014-08-14 15:19:57 -070055}