2 * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
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;
20 * Created by Martin Bobak <mbobak@cisco.com> on 5.3.2015.
22 public final class IpConversionUtil {
24 public static final String PREFIX_SEPARATOR = "/";
25 public static final Splitter PREFIX_SPLITTER = Splitter.on('/');
27 private IpConversionUtil() {
28 throw new IllegalStateException("This class should not be instantiated.");
31 public static Iterator<String> splitToParts(final Ipv4Prefix ipv4Prefix) {
32 return PREFIX_SPLITTER.split(ipv4Prefix.getValue()).iterator();
35 public static Iterator<String> splitToParts(final Ipv4Address ipv4Address) {
36 return PREFIX_SPLITTER.split(ipv4Address.getValue()).iterator();
39 public static Iterator<String> splitToParts(final Ipv6Address ipv6Address) {
40 return PREFIX_SPLITTER.split(ipv6Address.getValue()).iterator();
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()));
50 retval = new Ipv4Prefix(address + PREFIX_SEPARATOR + 32);
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);
61 retval = new Ipv4Prefix(address + PREFIX_SEPARATOR + 32);
65 public static Integer extractPrefix(Ipv4Address ipv4Address) {
66 Iterator<String> addressParts = splitToParts(ipv4Address);
68 Integer retval = null;
69 if (addressParts.hasNext()) {
70 retval = Integer.parseInt(addressParts.next());
74 public static Integer extractPrefix(Ipv6Address ipv6Address) {
75 Iterator<String> addressParts = splitToParts(ipv6Address);
77 Integer retval = null;
78 if (addressParts.hasNext()) {
79 retval = Integer.parseInt(addressParts.next());
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++) {
93 oneCount = oneCount - 8;
99 mask[count] = (byte) (256 - Math.pow(2, 8 - byteBits));
104 public static Ipv6Address extractIpv6Address(final Ipv6Prefix ipv6Prefix) {
105 Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv6Prefix.getValue()).iterator();
106 return new Ipv6Address(addressParts.next());
109 public static Integer extractIpv6Prefix(final Ipv6Prefix ipv6Prefix) {
110 Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv6Prefix.getValue()).iterator();
113 Integer prefix = null;
114 if (addressParts.hasNext()) {
115 prefix = Integer.parseInt(addressParts.next());