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 import java.net.UnknownHostException;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Joiner;
44 import com.google.common.base.Preconditions;
45 import com.google.common.base.Splitter;
46 import com.google.common.collect.ImmutableBiMap;
47
48 public class SouthboundMapper {
49     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
50
51     public static Node createNode(ConnectionInfo key) {
52         NodeBuilder nodeBuilder = new NodeBuilder();
53         nodeBuilder.setNodeId(createNodeId(key.getRemoteIp(),key.getRemotePort()));
54         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
55         return nodeBuilder.build();
56     }
57
58     public static OvsdbNodeAugmentation createOvsdbAugmentation(ConnectionInfo key) {
59         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
60         ovsdbNodeBuilder.setConnectionInfo(key);
61         return ovsdbNodeBuilder.build();
62     }
63
64     public static IpAddress createIpAddress(InetAddress address) {
65         IpAddress ip = null;
66         if (address instanceof Inet4Address) {
67             ip = createIpAddress((Inet4Address)address);
68         } else if (address instanceof Inet6Address) {
69             ip = createIpAddress((Inet6Address)address);
70         }
71         return ip;
72     }
73
74     public static IpAddress createIpAddress(Inet4Address address) {
75         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
76         return new IpAddress(ipv4);
77     }
78
79     public static IpAddress createIpAddress(Inet6Address address) {
80         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
81         return new IpAddress(ipv6);
82     }
83
84     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
85         return InstanceIdentifier
86                 .create(NetworkTopology.class)
87                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
88                 .child(Node.class,new NodeKey(nodeId));
89     }
90
91     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key,OvsdbBridgeName bridgeName) {
92         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
93     }
94
95     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
96         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
97         return nodeKey.getNodeId();
98     }
99
100     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key) {
101         return createInstanceIdentifier(key.getRemoteIp(), key.getRemotePort());
102     }
103
104     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
105         InstanceIdentifier<Node> path = InstanceIdentifier
106                 .create(NetworkTopology.class)
107                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
108                 .child(Node.class,createNodeKey(ip,port));
109         LOG.debug("Created ovsdb path: {}",path);
110         return path;
111     }
112
113     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
114         return new NodeKey(createNodeId(ip,port));
115     }
116
117     public static NodeId createManagedNodeId(ConnectionInfo key, OvsdbBridgeName bridgeName) {
118         return createManagedNodeId(key.getRemoteIp(),key.getRemotePort(),bridgeName);
119     }
120
121     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
122         return new NodeId(createNodeId(ip,port).getValue()
123                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
124     }
125
126     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
127         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
128                 + new String(ip.getValue()) + ":" + port.getValue();
129         Uri uri = new Uri(uriString);
130         return new NodeId(uri);
131     }
132
133     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
134         if (ip.getIpv4Address() != null) {
135             return InetAddress.getByName(ip.getIpv4Address().getValue());
136         } else if (ip.getIpv6Address() != null) {
137             return InetAddress.getByName(ip.getIpv6Address().getValue());
138         } else {
139             throw new UnknownHostException("IP Address has no value");
140         }
141     }
142
143     public static DatapathId createDatapathId(Set<String> dpids) {
144         Preconditions.checkNotNull(dpids);
145         if (dpids.isEmpty()) {
146             return null;
147         } else {
148             String[] dpidArray = new String[dpids.size()];
149             dpids.toArray(dpidArray);
150             return createDatapathId(dpidArray[0]);
151         }
152     }
153
154     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
155         String datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class);
156
157         if (mdsalbridge.getDatapathType() != null && !mdsalbridge.getDatapathType().equals(DatapathTypeBase.class)) {
158             datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
159             if (datapathtype == null) {
160                 throw new IllegalArgumentException("Unknown datapath type " + mdsalbridge.getDatapathType().getName());
161             }
162         }
163         return datapathtype;
164     }
165
166     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
167         Preconditions.checkNotNull(type);
168         if (type.isEmpty()) {
169             return DatapathTypeSystem.class;
170         } else {
171             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
172                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
173             return mapper.get(type);
174         }
175     }
176
177     public static DatapathId createDatapathId(String dpid) {
178         Preconditions.checkNotNull(dpid);
179         DatapathId datapath;
180         if (dpid.matches("^[0-9a-fA-F]{16}")) {
181             Splitter splitter = Splitter.fixedLength(2);
182             Joiner joiner = Joiner.on(":");
183             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
184         } else {
185             datapath = new DatapathId(dpid);
186         }
187         return datapath;
188     }
189
190     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
191         Set<String> protocols = new HashSet<>();
192         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
193             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
194                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
195                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
196                 } else {
197                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
198                 }
199             }
200         }
201         return protocols;
202     }
203
204     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
205         Preconditions.checkNotNull(type);
206         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
207     }
208
209     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
210         Preconditions.checkNotNull(mdsaltype);
211         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
212                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
213         return mapper.get(mdsaltype);
214     }
215 }