Fix Jdk8 compatibility
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / IpConversionUtil.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10
11 import com.google.common.base.Splitter;
12 import io.netty.buffer.ByteBufUtil;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
17 import java.util.Iterator;
18
19 /**
20  * Created by Martin Bobak <mbobak@cisco.com> on 5.3.2015.
21  */
22 public final class IpConversionUtil {
23
24     public static final String PREFIX_SEPARATOR = "/";
25     public static final Splitter PREFIX_SPLITTER = Splitter.on('/');
26
27     private IpConversionUtil() {
28         throw new IllegalStateException("This class should not be instantiated.");
29     }
30
31     public static Iterator<String> splitToParts(final Ipv4Prefix ipv4Prefix) {
32         return PREFIX_SPLITTER.split(ipv4Prefix.getValue()).iterator();
33     }
34
35     public static Iterator<String> splitToParts(final Ipv4Address ipv4Address) {
36         return PREFIX_SPLITTER.split(ipv4Address.getValue()).iterator();
37     }
38
39     public static Iterator<String> splitToParts(final Ipv6Address ipv6Address) {
40         return PREFIX_SPLITTER.split(ipv6Address.getValue()).iterator();
41     }
42
43     public static Ipv4Prefix createPrefix(Ipv4Address ipv4Address){
44         Iterator<String> addressParts = splitToParts(ipv4Address);
45         String address = addressParts.next();
46         Ipv4Prefix retval = null;
47         if (addressParts.hasNext()) {
48             retval = new Ipv4Prefix(address + PREFIX_SEPARATOR + Integer.parseInt(addressParts.next()));
49         } else {
50             retval = new Ipv4Prefix(address + PREFIX_SEPARATOR + 32);
51         }
52         return retval;
53     }
54     public static Ipv4Prefix createPrefix(Ipv4Address ipv4Address, String mask){
55         Iterator<String> addressParts = splitToParts(ipv4Address);
56         String address = addressParts.next();
57         Ipv4Prefix retval = null;
58         if (null != mask && !mask.equals("")) {
59             retval = new Ipv4Prefix(address + mask);
60         } else {
61             retval = new Ipv4Prefix(address + PREFIX_SEPARATOR + 32);
62         }
63         return retval;
64     }
65     public static Integer extractPrefix(Ipv4Address ipv4Address) {
66         Iterator<String> addressParts = splitToParts(ipv4Address);
67         addressParts.next();
68         Integer retval = null;
69         if (addressParts.hasNext()) {
70             retval = Integer.parseInt(addressParts.next());
71         }
72         return retval;
73     }
74     public static Integer extractPrefix(Ipv6Address ipv6Address) {
75         Iterator<String> addressParts = splitToParts(ipv6Address);
76         addressParts.next();
77         Integer retval = null;
78         if (addressParts.hasNext()) {
79             retval = Integer.parseInt(addressParts.next());
80         }
81         return retval;
82     }
83
84
85     public static byte[] convertIpv6PrefixToByteArray(final int prefix) {
86         // TODO: Temporary fix. Has performance impacts.
87         byte[] mask = new byte[16];
88         int oneCount = prefix;
89         for (int count = 0; count < 16; count++) {
90             int byteBits = 0;
91             if (oneCount >= 8) {
92                 byteBits = 8;
93                 oneCount = oneCount - 8;
94             } else {
95                 byteBits = oneCount;
96                 oneCount = 0;
97             }
98
99             mask[count] = (byte) (256 - Math.pow(2, 8 - byteBits));
100         }
101         return mask;
102     }
103
104     public static Ipv6Address extractIpv6Address(final Ipv6Prefix ipv6Prefix) {
105         Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv6Prefix.getValue()).iterator();
106         return new Ipv6Address(addressParts.next());
107     }
108
109     public static Integer extractIpv6Prefix(final Ipv6Prefix ipv6Prefix) {
110         Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv6Prefix.getValue()).iterator();
111         addressParts.next();
112
113         Integer prefix = null;
114         if (addressParts.hasNext()) {
115             prefix = Integer.parseInt(addressParts.next());
116         }
117         return prefix;
118     }
119
120
121 }