Merge "Revert "BUG-2613: Migrating Openflow Specific NSF from controller project...
[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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
11
12 /**
13  * @author msunal
14  *
15  */
16 public final class IpConverter {
17
18     public static long Ipv4AddressToLong(Ipv4Address ipv4Address) {
19         String ipAddress = ipv4Address.getValue();
20         long result = 0;
21         String[] atoms = ipAddress.split("\\.");
22         for (int i = 3; i >= 0; i--) {
23             result |= (Long.parseLong(atoms[3 - i]) << (i * 8));
24         }
25         return result & 0xFFFFFFFF;
26     }
27
28     public static Ipv4Address longToIpv4Address(long ip) {
29         long tmpIp = ip;
30         StringBuilder sb = new StringBuilder(15);
31         for (int i = 0; i < 4; i++) {
32             sb.insert(0, Long.toString(tmpIp & 0xff));
33             if (i < 3) {
34                 sb.insert(0, '.');
35             }
36             tmpIp >>= 8;
37         }
38         return new Ipv4Address(sb.toString());
39     }
40
41 }