Merge "Avoid exceptions with IPv6 subnets"
[netvirt.git] / openstack / net-virt-it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / it / SouthboundMapper.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 package org.opendaylight.ovsdb.openstack.netvirt.it;
9
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Preconditions;
36
37 public class SouthboundMapper {
38     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
39
40     public static Node createNode(ConnectionInfo key) {
41         NodeBuilder nodeBuilder = new NodeBuilder();
42         nodeBuilder.setNodeId(createNodeId(key.getRemoteIp(),key.getRemotePort()));
43         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
44         return nodeBuilder.build();
45     }
46
47     public static OvsdbNodeAugmentation createOvsdbAugmentation(ConnectionInfo key) {
48         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
49         ovsdbNodeBuilder.setConnectionInfo(key);
50         return ovsdbNodeBuilder.build();
51     }
52
53     public static IpAddress createIpAddress(InetAddress address) {
54         IpAddress ip = null;
55         if (address instanceof Inet4Address) {
56             ip = createIpAddress((Inet4Address)address);
57         } else if (address instanceof Inet6Address) {
58             ip = createIpAddress((Inet6Address)address);
59         }
60         return ip;
61     }
62
63     public static IpAddress createIpAddress(Inet4Address address) {
64         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
65         return new IpAddress(ipv4);
66     }
67
68     public static IpAddress createIpAddress(Inet6Address address) {
69         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
70         return new IpAddress(ipv6);
71     }
72
73     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
74         return InstanceIdentifier
75                 .create(NetworkTopology.class)
76                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
77                 .child(Node.class,new NodeKey(nodeId));
78     }
79
80     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key,OvsdbBridgeName bridgeName) {
81         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
82     }
83
84     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
85         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
86         return nodeKey.getNodeId();
87     }
88
89     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key) {
90         return createInstanceIdentifier(key.getRemoteIp(), key.getRemotePort());
91     }
92
93     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
94         InstanceIdentifier<Node> path = InstanceIdentifier
95                 .create(NetworkTopology.class)
96                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
97                 .child(Node.class,createNodeKey(ip,port));
98         LOG.debug("Created ovsdb path: {}",path);
99         return path;
100     }
101
102     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
103         return new NodeKey(createNodeId(ip,port));
104     }
105
106     public static NodeId createManagedNodeId(ConnectionInfo key, OvsdbBridgeName bridgeName) {
107         return createManagedNodeId(key.getRemoteIp(),key.getRemotePort(),bridgeName);
108     }
109
110     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
111         return new NodeId(createNodeId(ip,port).getValue()
112                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
113     }
114
115     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
116         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
117                 + String.valueOf(ip.getValue()) + ":" + port.getValue();
118         Uri uri = new Uri(uriString);
119         return new NodeId(uri);
120     }
121
122     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
123         Preconditions.checkNotNull(type);
124         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
125     }
126 }