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.implementation.util;
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.nio.ByteBuffer;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv6.Ipv6Address;
20 public class MaskUtil {
22 public static boolean isMaskable(LispAFIAddress address) {
23 if (address instanceof Ipv4Address || address instanceof Ipv6Address) {
29 public static LispAFIAddress normalize(LispAFIAddress address, int mask) {
31 if (address instanceof Ipv4Address) {
32 return LispAFIConvertor.asIPAfiAddress(normalizeIP(Inet4Address.getByName(((Ipv4Address) address).getIpv4Address().getValue()),
33 mask).getHostAddress());
35 if (address instanceof Ipv6Address) {
36 return LispAFIConvertor.asIPv6AfiAddress(normalizeIP(Inet6Address.getByName(((Ipv6Address) address).getIpv6Address().getValue()),
37 mask).getHostAddress());
40 } catch (UnknownHostException e) {
46 private static InetAddress normalizeIP(InetAddress address, int mask) throws UnknownHostException {
47 ByteBuffer byteRepresentation = ByteBuffer.wrap(address.getAddress());
49 for (int i = 0; i < byteRepresentation.array().length; i++) {
51 byteRepresentation.put(i, (byte) (b & byteRepresentation.get(i)));
54 byteRepresentation.put(i, (byte) ((byte) (b << (8 - mask)) & byteRepresentation.get(i)));
56 byteRepresentation.put(i, (byte) (0 & byteRepresentation.get(i)));
61 return InetAddress.getByAddress(byteRepresentation.array());
64 public static int getMaxMask(LispAFIAddress address) {
65 if (address instanceof Ipv4Address) {
68 if (address instanceof Ipv6Address) {