2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.lisp.util;
10 import com.google.common.base.Preconditions;
11 import com.google.common.net.InetAddresses;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import java.nio.ByteBuffer;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.InstanceId;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public final class MaskUtil {
36 private static final Logger LOG = LoggerFactory.getLogger(MaskUtil.class);
37 private static final short IPV4_MAX_MASK = 32;
38 private static final short IPV6_MAX_MASK = 128;
40 // Utility class, should not be instantiated
44 public static boolean isMaskable(Address address) {
45 if (address instanceof Ipv4Prefix || address instanceof Ipv6Prefix || address instanceof SourceDestKey) {
47 } else if (address instanceof InstanceId) {
48 return isMaskable(((InstanceId)address).getInstanceId().getAddress());
53 public static boolean isMaskable(SimpleAddress address) {
54 if (address.getIpPrefix() != null) {
60 private static final int slashPosition(final String prefix) {
61 final int slash = prefix.lastIndexOf('/');
62 Preconditions.checkArgument(slash >= 0, "Argument %s does not contain a slash", prefix);
66 private static String getPrefixAddress(final String prefix) {
67 return prefix.substring(0, slashPosition(prefix));
70 private static String getPrefixMask(final String prefix) {
71 return prefix.substring(slashPosition(prefix) + 1);
74 private static String[] splitPrefix(final String prefix) {
75 final int slash = slashPosition(prefix);
76 return new String[] { prefix.substring(0, slash), prefix.substring(slash + 1) };
79 public static Eid normalize(Eid eid, short mask) {
80 Address address = eid.getAddress();
82 if (address instanceof Ipv4Prefix) {
83 final String addr = getPrefixAddress(((Ipv4Prefix)address).getIpv4Prefix().getValue());
84 InetAddress normalized = normalizeIP(InetAddresses.forString(addr), mask);
85 return LispAddressUtil.asIpv4PrefixEid(eid, (Inet4Address)normalized, mask);
86 } else if (address instanceof Ipv6Prefix) {
87 final String addr = getPrefixAddress(((Ipv6Prefix)address).getIpv6Prefix().getValue());
88 InetAddress normalized = normalizeIP(InetAddresses.forString(addr), mask);
89 return LispAddressUtil.asIpv6PrefixEid(eid, (Inet6Address)normalized, mask);
90 } else if (address instanceof InstanceId) {
91 // TODO - not absolutely necessary, but should be implemented
94 } catch (UnknownHostException e) {
95 LOG.warn("Failed to normalize EID {} with mask {}, returning original EID", eid, mask, e);
100 public static Eid normalize(Eid eid) {
101 Address address = eid.getAddress();
103 if (address instanceof Ipv4Prefix) {
104 String[] v4prefix = splitPrefix(((Ipv4Prefix)address).getIpv4Prefix().getValue());
105 short mask = Short.parseShort(v4prefix[1]);
106 InetAddress normalized = normalizeIP(InetAddresses.forString(v4prefix[0]), mask);
107 return LispAddressUtil.asIpv4PrefixEid(eid, (Inet4Address) normalized, mask);
108 } else if (address instanceof Ipv6Prefix) {
109 String[] v6prefix = splitPrefix(((Ipv6Prefix)address).getIpv6Prefix().getValue());
110 short mask = Short.parseShort(v6prefix[1]);
111 InetAddress normalized = normalizeIP(InetAddresses.forString(v6prefix[0]), mask);
112 return LispAddressUtil.asIpv6PrefixEid(eid, (Inet6Address) normalized, mask);
113 } else if (address instanceof Ipv4) {
114 return LispAddressUtil.asIpv4PrefixEid(((Ipv4) address).getIpv4(), eid.getVirtualNetworkId());
115 } else if (address instanceof Ipv6) {
116 return LispAddressUtil.asIpv6PrefixEid(((Ipv6) address).getIpv6(), eid.getVirtualNetworkId());
117 } else if (address instanceof InstanceId) {
118 // TODO - not absolutely necessary, but should be implemented
120 } else if (address instanceof SourceDestKey) {
121 return normalizeSrcDst(eid);
122 } else if (address instanceof ServicePath) {
123 // Build new Service Path eid with service index set to 0
124 long spi = ((ServicePath) address).getServicePath().getServicePathId().getValue();
125 long vni = eid.getVirtualNetworkId() != null ? eid.getVirtualNetworkId().getValue() : -1;
126 return LispAddressUtil.asServicePathEid(vni, spi, (short)0);
128 } catch (UnknownHostException e) {
129 LOG.warn("Failed to normalize EID {}, returning original", eid, e);
134 private static Eid normalizeSrcDst(Eid eid) throws UnknownHostException {
135 final SimpleAddress normalizedSrc = normalizeSimpleAddress(
136 ((SourceDestKey) eid.getAddress()).getSourceDestKey().getSource());
137 final SimpleAddress normalizedDst = normalizeSimpleAddress(
138 ((SourceDestKey) eid.getAddress()).getSourceDestKey().getDest());
139 return LispAddressUtil.asSrcDstEid(new SourceDestKeyBuilder()
140 .setSource(normalizedSrc).setDest(normalizedDst).build(), eid.getVirtualNetworkId());
143 private static SimpleAddress normalizeSimpleAddress(SimpleAddress address) throws UnknownHostException {
144 if (address.getIpPrefix() == null) {
147 return new SimpleAddress(normalizeIpPrefix(address.getIpPrefix()));
150 private static IpPrefix normalizeIpPrefix(IpPrefix address) throws UnknownHostException {
151 String[] prefix = splitPrefix(String.valueOf(address.getValue()));
152 short mask = Short.parseShort(prefix[1]);
154 return ipPrefixFor(normalizeIP(InetAddresses.forString(prefix[0]), mask), mask);
157 // This is a missing method from IetfInetUtil. Will try to upstream there and remove from here
158 private static IpPrefix ipPrefixFor(InetAddress address, int maskLength) {
159 if (address instanceof Inet4Address) {
160 return new IpPrefix(IetfInetUtil.INSTANCE.ipv4PrefixFor(address, maskLength));
161 } else if (address instanceof Inet6Address) {
162 return new IpPrefix(IetfInetUtil.INSTANCE.ipv6PrefixFor(address, maskLength));
164 throw new IllegalArgumentException("Unhandled address " + address);
168 private static InetAddress normalizeIP(InetAddress address, int maskLength) throws UnknownHostException {
169 ByteBuffer byteRepresentation = ByteBuffer.wrap(address.getAddress());
170 byte b = (byte) 0xff;
171 int mask = maskLength;
172 for (int i = 0; i < byteRepresentation.array().length; i++) {
174 byteRepresentation.put(i, (byte) (b & byteRepresentation.get(i)));
175 } else if (mask > 0) {
176 byteRepresentation.put(i, (byte) ((byte) (b << (8 - mask)) & byteRepresentation.get(i)));
178 byteRepresentation.put(i, (byte) (0 & byteRepresentation.get(i)));
183 return InetAddress.getByAddress(byteRepresentation.array());
186 public static int getMaxMask(Address address) {
187 if (address instanceof Ipv4 || address instanceof Ipv4Prefix) {
188 return IPV4_MAX_MASK;
189 } else if (address instanceof Ipv6 || address instanceof Ipv6Prefix) {
190 return IPV6_MAX_MASK;
196 public static short getMaskForAddress(SimpleAddress address) {
197 if (address.getIpPrefix() == null) {
200 return getMaskForIpPrefix(address.getIpPrefix());
203 private static String getIpPrefixString(IpPrefix prefix) {
204 if (prefix.getIpv4Prefix() != null) {
205 return prefix.getIpv4Prefix().getValue();
206 } else if (prefix.getIpv6Prefix() != null) {
207 return prefix.getIpv6Prefix().getValue();
209 throw new IllegalArgumentException("Invalid prefix " + prefix);
213 public static short getMaskForIpPrefix(IpPrefix prefix) {
214 return Short.parseShort(getPrefixMask(getIpPrefixString(prefix)));
217 public static String getAddressStringForIpPrefix(IpPrefix prefix) {
218 return getPrefixAddress(getIpPrefixString(prefix));
221 public static String getAddressStringForIpv4Prefix(Ipv4Prefix prefix) {
222 return getPrefixAddress(prefix.getIpv4Prefix().getValue());
225 public static String getAddressStringForIpv6Prefix(Ipv6Prefix prefix) {
226 return getPrefixAddress(prefix.getIpv6Prefix().getValue());
229 public static short getMaskForAddress(Address address) {
230 if (address instanceof Ipv4) {
231 return IPV4_MAX_MASK;
232 } else if (address instanceof Ipv6) {
233 return IPV6_MAX_MASK;
234 } else if (address instanceof Ipv4Prefix) {
235 return Short.parseShort(getPrefixMask(((Ipv4Prefix)address).getIpv4Prefix().getValue()));
236 } else if (address instanceof Ipv6Prefix) {
237 return Short.parseShort(getPrefixMask(((Ipv6Prefix)address).getIpv6Prefix().getValue()));
238 } else if (address instanceof InstanceId) {
239 return getMaskForAddress(((InstanceId)address).getInstanceId().getAddress());