Squashed commit of the following:
[ovsdb.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         InstanceIdentifier<Node> nodePath = InstanceIdentifier
86                 .create(NetworkTopology.class)
87                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
88                 .child(Node.class,new NodeKey(nodeId));
89         return nodePath;
90     }
91
92     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key,OvsdbBridgeName bridgeName) {
93         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
94     }
95
96     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
97         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
98         return nodeKey.getNodeId();
99     }
100
101     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key) {
102         return createInstanceIdentifier(key.getRemoteIp(), key.getRemotePort());
103     }
104
105     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
106         InstanceIdentifier<Node> path = InstanceIdentifier
107                 .create(NetworkTopology.class)
108                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
109                 .child(Node.class,createNodeKey(ip,port));
110         LOG.debug("Created ovsdb path: {}",path);
111         return path;
112     }
113
114     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
115         return new NodeKey(createNodeId(ip,port));
116     }
117
118     public static NodeId createManagedNodeId(ConnectionInfo key, OvsdbBridgeName bridgeName) {
119         return createManagedNodeId(key.getRemoteIp(),key.getRemotePort(),bridgeName);
120     }
121
122     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
123         return new NodeId(createNodeId(ip,port).getValue()
124                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
125     }
126
127     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
128         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
129                 + new String(ip.getValue()) + ":" + port.getValue();
130         Uri uri = new Uri(uriString);
131         NodeId nodeId = new NodeId(uri);
132         return nodeId;
133     }
134
135     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
136         if (ip.getIpv4Address() != null) {
137             return InetAddress.getByName(ip.getIpv4Address().getValue());
138         } else if (ip.getIpv6Address() != null) {
139             return InetAddress.getByName(ip.getIpv6Address().getValue());
140         } else {
141             throw new UnknownHostException("IP Address has no value");
142         }
143     }
144
145     public static DatapathId createDatapathId(Set<String> dpids) {
146         Preconditions.checkNotNull(dpids);
147         if (dpids.isEmpty()) {
148             return null;
149         } else {
150             String[] dpidArray = new String[dpids.size()];
151             dpids.toArray(dpidArray);
152             return createDatapathId(dpidArray[0]);
153         }
154     }
155
156     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
157         String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
158
159         if (mdsalbridge.getDatapathType() != null) {
160             if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
161                 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
162             } else {
163                 throw new IllegalArgumentException("Unknown datapath type "
164                         + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
165             }
166         }
167         return datapathtype;
168     }
169
170     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
171         Preconditions.checkNotNull(type);
172         if (type.isEmpty()) {
173             return DatapathTypeSystem.class;
174         } else {
175             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
176                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
177             return mapper.get(type);
178         }
179     }
180
181     public static DatapathId createDatapathId(String dpid) {
182         Preconditions.checkNotNull(dpid);
183         DatapathId datapath;
184         if (dpid.matches("^[0-9a-fA-F]{16}")) {
185             Splitter splitter = Splitter.fixedLength(2);
186             Joiner joiner = Joiner.on(":");
187             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
188         } else {
189             datapath = new DatapathId(dpid);
190         }
191         return datapath;
192     }
193
194     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
195         Set<String> protocols = new HashSet<String>();
196         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
197             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
198                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
199                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
200                 } else {
201                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
202                 }
203             }
204         }
205         return protocols;
206     }
207
208     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
209         Preconditions.checkNotNull(type);
210         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
211     }
212
213     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
214         Preconditions.checkNotNull(mdsaltype);
215         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
216                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
217         return mapper.get(mdsaltype);
218     }
219 }