5d46747592125be6da46ff76558a8a756ad02186
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / util / MaskUtil.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.util;
9
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;
15
16 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispIpv4Address;
17 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispIpv6Address;
18 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
19
20 public class MaskUtil {
21
22     public static boolean isMaskable(Address address) {
23         if (address instanceof LispIpv4Address || address instanceof LispIpv6Address) {
24             return true;
25         }
26         return false;
27     }
28
29     public static Address normalize(Address address, int mask) {
30         try {
31             if (address instanceof LispIpv4Address) {
32                 return LispAFIConvertor.asIPAfiAddress(normalizeIP(Inet4Address.getByName(((LispIpv4Address) address).getIpv4Address().getValue()),
33                         mask).getHostAddress());
34             }
35             if (address instanceof LispIpv6Address) {
36                 return LispAFIConvertor.asIPv6AfiAddress(normalizeIP(Inet6Address.getByName(((LispIpv6Address) address).getIpv6Address().getValue()),
37                         mask).getHostAddress());
38             }
39
40         } catch (UnknownHostException e) {
41             return null;
42         }
43         return null;
44     }
45
46     private static InetAddress normalizeIP(InetAddress address, int mask) throws UnknownHostException {
47         ByteBuffer byteRepresentation = ByteBuffer.wrap(address.getAddress());
48         byte b = (byte) 0xff;
49         for (int i = 0; i < byteRepresentation.array().length; i++) {
50             if (mask >= 8)
51                 byteRepresentation.put(i, (byte) (b & byteRepresentation.get(i)));
52
53             else if (mask > 0) {
54                 byteRepresentation.put(i, (byte) ((byte) (b << (8 - mask)) & byteRepresentation.get(i)));
55             } else {
56                 byteRepresentation.put(i, (byte) (0 & byteRepresentation.get(i)));
57             }
58
59             mask -= 8;
60         }
61         return InetAddress.getByAddress(byteRepresentation.array());
62     }
63
64     public static int getMaxMask(Address address) {
65         if (address instanceof LispIpv4Address) {
66             return 32;
67         }
68         if (address instanceof LispIpv6Address) {
69             return 128;
70         }
71         return -1;
72     }
73
74 }