Merge "Fixed the log level."
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / IpConverter.java
1 /**
2  * Copyright (c) 2014 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 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor;
9
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.Lists;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
13
14 import java.util.List;
15 import java.util.ListIterator;
16
17 /**
18  * @author msunal
19  *
20  */
21 public final class IpConverter {
22
23     public static long Ipv4AddressToLong(Ipv4Address ipv4Address) {
24         long result = 0 ;
25         Iterable<String> splitted = Splitter.on('.')
26                 .trimResults()
27                 .omitEmptyStrings()
28                 .split(ipv4Address.getValue());
29
30         List<String> splittedAddress = Lists.newArrayList(splitted.iterator());
31         int maxIndex = splittedAddress.size() - 1;
32         ListIterator<String> listIter = splittedAddress.listIterator();
33         while(listIter.hasNext()) {
34             String current = listIter.next();
35             int i = splittedAddress.indexOf(current);
36             result |= (Long.parseLong(current) << ((maxIndex-i) * 8));
37         }
38         return result & 0xFFFFFFFF;
39     }
40
41     public static Ipv4Address longToIpv4Address(long ip) {
42         long tmpIp = ip;
43         StringBuilder sb = new StringBuilder(15);
44         for (int i = 0; i < 4; i++) {
45             sb.insert(0, Long.toString(tmpIp & 0xff));
46             if (i < 3) {
47                 sb.insert(0, '.');
48             }
49             tmpIp >>= 8;
50         }
51         return new Ipv4Address(sb.toString());
52     }
53
54 }