2ebbbc3b32e0d073887a95533feda8a40eb087e1
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / 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.southbound;
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.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.opendaylight.ovsdb.lib.OvsdbClient;
22 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
27 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 import com.google.common.base.Joiner;
59 import com.google.common.base.Preconditions;
60 import com.google.common.base.Splitter;
61 import com.google.common.collect.ImmutableBiMap;
62
63 public class SouthboundMapper {
64     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
65
66     public static Node createNode(OvsdbClient client) {
67         NodeBuilder nodeBuilder = new NodeBuilder();
68         nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
69         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
70         return nodeBuilder.build();
71     }
72     public static Node createNode(OvsdbClientKey key) {
73         NodeBuilder nodeBuilder = new NodeBuilder();
74         nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
75         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
76         return nodeBuilder.build();
77     }
78
79     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
80         return createOvsdbAugmentation(new OvsdbClientKey(client));
81     }
82
83     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
84         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
85         ovsdbNodeBuilder.setIp(key.getIp());
86         ovsdbNodeBuilder.setPort(key.getPort());
87         return ovsdbNodeBuilder.build();
88     }
89
90     public static IpAddress createIpAddress(InetAddress address) {
91         IpAddress ip = null;
92         if (address instanceof Inet4Address) {
93             ip = createIpAddress((Inet4Address)address);
94         } else if (address instanceof Inet6Address) {
95             ip = createIpAddress((Inet6Address)address);
96         }
97         return ip;
98     }
99
100     public static IpAddress createIpAddress(Inet4Address address) {
101         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
102         return new IpAddress(ipv4);
103     }
104
105     public static IpAddress createIpAddress(Inet6Address address) {
106         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
107         return new IpAddress(ipv6);
108     }
109
110     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
111         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
112                 new PortNumber(client.getConnectionInfo().getRemotePort()));
113     }
114
115     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
116         InstanceIdentifier<Node> nodePath = InstanceIdentifier
117                 .create(NetworkTopology.class)
118                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
119                 .child(Node.class,new NodeKey(nodeId));
120         return nodePath;
121     }
122
123     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,OvsdbBridgeName bridgeName) {
124         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
125     }
126
127     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,Bridge bridge) {
128         String managedNodePathString = bridge
129                 .getExternalIdsColumn()
130                 .getData()
131                 .get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
132         InstanceIdentifier<Node> managedNodePath = null;
133         if (managedNodePathString != null) {
134             managedNodePath = (InstanceIdentifier<Node>) SouthboundUtil
135                     .deserializeInstanceIdentifier(managedNodePathString);
136         }
137         if (managedNodePath == null) {
138             managedNodePath = SouthboundMapper.createInstanceIdentifier(key,new OvsdbBridgeName(bridge.getName()));
139         }
140         return managedNodePath;
141     }
142
143     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
144         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
145         return nodeKey.getNodeId();
146     }
147
148     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key) {
149         return createInstanceIdentifier(key.getIp(), key.getPort());
150     }
151
152     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
153         InstanceIdentifier<Node> path = InstanceIdentifier
154                 .create(NetworkTopology.class)
155                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
156                 .child(Node.class,createNodeKey(ip,port));
157         LOG.info("Created ovsdb path: {}",path);
158         return path;
159     }
160
161     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
162         return new NodeKey(createNodeId(ip,port));
163     }
164
165     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
166         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
167                 new PortNumber(connectionInfo.getRemotePort()));
168     }
169
170     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
171         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
172                 new PortNumber(connectionInfo.getRemotePort()),
173                 bridgeName);
174     }
175
176     public static NodeId createManagedNodeId(OvsdbClientKey key, OvsdbBridgeName bridgeName) {
177         return createManagedNodeId(key.getIp(),key.getPort(),bridgeName);
178     }
179
180     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
181         return new NodeId(createNodeId(ip,port).getValue()
182                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
183     }
184
185     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
186         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
187                 + new String(ip.getValue()) + ":" + port.getValue();
188         Uri uri = new Uri(uriString);
189         NodeId nodeId = new NodeId(uri);
190         return nodeId;
191     }
192
193     public static TpId createTerminationPointId(OvsdbConnectionInfo connectionInfo,
194                                                 OvsdbBridgeName bridgeName, String tpName) {
195         return createTerminationPointId(createIpAddress(connectionInfo.getRemoteAddress()),
196                 new PortNumber(connectionInfo.getRemotePort()),
197                 bridgeName, tpName);
198     }
199
200     public static TpId createTerminationPointId(OvsdbClientKey key, OvsdbBridgeName bridgeName, String tpName) {
201         return createTerminationPointId(key.getIp(),key.getPort(),bridgeName, tpName);
202     }
203
204     public static TpId createTerminationPointId(IpAddress ip, PortNumber port,
205                                                 OvsdbBridgeName bridgeName, String tpName) {
206         return new TpId(createNodeId(ip,port).getValue()
207                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue()
208                 + "/" + SouthboundConstants.TP_URI_PREFIX + "/" + tpName);
209     }
210
211     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
212         if (ip.getIpv4Address() != null) {
213             return InetAddress.getByName(ip.getIpv4Address().getValue());
214         } else if (ip.getIpv6Address() != null) {
215             return InetAddress.getByName(ip.getIpv6Address().getValue());
216         } else {
217             throw new UnknownHostException("IP Address has no value");
218         }
219     }
220
221     public static DatapathId createDatapathId(Bridge bridge) {
222         Preconditions.checkNotNull(bridge);
223         if (bridge.getDatapathIdColumn() == null) {
224             return null;
225         } else {
226             return createDatapathId(bridge.getDatapathIdColumn().getData());
227         }
228     }
229
230     public static DatapathId createDatapathId(Set<String> dpids) {
231         Preconditions.checkNotNull(dpids);
232         if (dpids.isEmpty()) {
233             return null;
234         } else {
235             String[] dpidArray = new String[dpids.size()];
236             dpids.toArray(dpidArray);
237             return createDatapathId(dpidArray[0]);
238         }
239     }
240
241     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
242         String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
243
244         if (mdsalbridge.getDatapathType() != null) {
245             if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
246                 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
247             } else {
248                 throw new IllegalArgumentException("Unknown datapath type "
249                         + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
250             }
251         }
252         return datapathtype;
253     }
254
255     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
256         Preconditions.checkNotNull(type);
257         if (type.isEmpty()) {
258             return DatapathTypeSystem.class;
259         } else {
260             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
261                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
262             return mapper.get(type);
263         }
264     }
265
266     public static DatapathId createDatapathId(String dpid) {
267         Preconditions.checkNotNull(dpid);
268         DatapathId datapath;
269         if (dpid.matches("^[0-9a-fA-F]{16}")) {
270             Splitter splitter = Splitter.fixedLength(2);
271             Joiner joiner = Joiner.on(":");
272             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
273         } else {
274             datapath = new DatapathId(dpid);
275         }
276         return datapath;
277     }
278
279     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
280         Set<String> protocols = new HashSet<String>();
281         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
282             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
283                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
284                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
285                 } else {
286                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
287                 }
288             }
289         }
290         return protocols;
291     }
292
293     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
294         Preconditions.checkNotNull(type);
295         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
296     }
297
298     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
299         Preconditions.checkNotNull(mdsaltype);
300         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
301                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
302         return mapper.get(mdsaltype);
303     }
304
305     public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
306         Set<String> protocols = bridge.getProtocolsColumn().getData();
307         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
308         if (protocols != null && protocols.size() > 0) {
309             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
310                     SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
311             for (String protocol : protocols) {
312                 if (protocol != null && mapper.get(protocol) != null) {
313                     protocolList.add(new ProtocolEntryBuilder().
314                             setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
315                 }
316             }
317         }
318         return protocolList;
319     }
320
321     public static List<ControllerEntry> createControllerEntries(Bridge bridge,Map<UUID,
322             Controller> updatedControllerRows) {
323         LOG.debug("Bridge: {}, updatedControllerRows: {}",bridge,updatedControllerRows);
324         Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
325         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
326         for (UUID controllerUUID : controllerUUIDs ) {
327             Controller controller = updatedControllerRows.get(controllerUUID);
328             if (controller != null && controller.getTargetColumn() != null
329                     && controller.getTargetColumn() != null) {
330                 String targetString = controller.getTargetColumn().getData();
331                 controllerEntries.add(new ControllerEntryBuilder().setTarget(new Uri(targetString)).build());
332             }
333         }
334         return controllerEntries;
335     }
336
337     public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
338         List<ControllerEntry> controllerEntries = omn.getControllerEntry();
339         Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
340         if (controllerEntries != null && !controllerEntries.isEmpty()) {
341             int index = 0;
342             for (ControllerEntry controllerEntry : controllerEntries) {
343                 String controllerNamedUUID = "Controller_" + omn.getBridgeName().getValue() + index++;
344                 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
345                 controller.setTarget(controllerEntry.getTarget().getValue());
346                 controllerMap.put(new UUID(controllerNamedUUID), controller);
347             }
348         }
349         return controllerMap;
350     }
351 }