Merge "Initial pass at netvirt model"
[netvirt.git] / plugin-mdsal-adapter / src / main / java / org / opendaylight / ovsdb / plugin / md / Utils.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, 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.ovsdb.plugin.md;
10
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import java.net.Inet4Address;
19 import java.net.InetAddress;
20
21 /**
22  * Utilities to convert Java types to the types specified in the Yang models
23  */
24 public final class Utils {
25
26     static final Logger logger = LoggerFactory.getLogger(Utils.class);
27
28     /**
29      * Returns a {@link org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress}
30      * from a @{link java.net.InetAddress}
31      */
32     public static IpAddress convertIpAddress(InetAddress inetAddress){
33
34         if (inetAddress instanceof Inet4Address){
35             Ipv4Address ipv4Address = new Ipv4Address(inetAddress.getHostAddress());
36             return new IpAddress(ipv4Address);
37         }
38         else {
39             Ipv6Address ipv6Address = new Ipv6Address(inetAddress.getHostAddress());
40             return new IpAddress(ipv6Address);
41         }
42     }
43 }