2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.southbound;
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;
21 import org.opendaylight.ovsdb.lib.OvsdbClient;
22 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
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.ovsdb.schema.openvswitch.Manager;
29 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
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.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntryBuilder;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 import com.google.common.base.Joiner;
61 import com.google.common.base.Preconditions;
62 import com.google.common.base.Splitter;
63 import com.google.common.collect.ImmutableBiMap;
65 public class SouthboundMapper {
66 private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
67 private static final String N_CONNECTIONS_STR = "n_connections";
69 private static NodeId createNodeId(OvsdbConnectionInstance client) {
70 NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
71 return key.getNodeId();
75 public static IpAddress createIpAddress(InetAddress address) {
77 if (address instanceof Inet4Address) {
78 ip = createIpAddress((Inet4Address)address);
79 } else if (address instanceof Inet6Address) {
80 ip = createIpAddress((Inet6Address)address);
85 public static IpAddress createIpAddress(Inet4Address address) {
86 Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
87 return new IpAddress(ipv4);
90 public static IpAddress createIpAddress(Inet6Address address) {
91 Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
92 return new IpAddress(ipv6);
95 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
96 return InstanceIdentifier
97 .create(NetworkTopology.class)
98 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
99 .child(Node.class,new NodeKey(nodeId));
102 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbConnectionInstance client,Bridge bridge) {
103 InstanceIdentifier<Node> iid;
104 if (bridge.getExternalIdsColumn() != null
105 && bridge.getExternalIdsColumn().getData() != null
106 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
107 String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
108 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
110 String nodeString = client.getNodeKey().getNodeId().getValue()
111 + "/bridge/" + bridge.getName();
112 NodeId nodeId = new NodeId(new Uri(nodeString));
113 NodeKey nodeKey = new NodeKey(nodeId);
114 iid = InstanceIdentifier.builder(NetworkTopology.class)
115 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
116 .child(Node.class,nodeKey)
122 public static InstanceIdentifier<Node> createInstanceIdentifier(
123 OvsdbConnectionInstance client, Controller controller, String bridgeName) {
124 InstanceIdentifier<Node> iid;
125 if (controller.getExternalIdsColumn() != null
126 && controller.getExternalIdsColumn().getData() != null
127 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
128 String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
129 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
131 // TODO retrieve bridge name
132 String nodeString = client.getNodeKey().getNodeId().getValue()
133 + "/bridge/" + bridgeName;
134 NodeId nodeId = new NodeId(new Uri(nodeString));
135 NodeKey nodeKey = new NodeKey(nodeId);
136 iid = InstanceIdentifier.builder(NetworkTopology.class)
137 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
138 .child(Node.class,nodeKey)
144 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
145 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
146 return nodeKey.getNodeId();
149 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
150 if (ip.getIpv4Address() != null) {
151 return InetAddress.getByName(ip.getIpv4Address().getValue());
152 } else if (ip.getIpv6Address() != null) {
153 return InetAddress.getByName(ip.getIpv6Address().getValue());
155 throw new UnknownHostException("IP Address has no value");
159 public static DatapathId createDatapathId(Bridge bridge) {
160 Preconditions.checkNotNull(bridge);
161 if (bridge.getDatapathIdColumn() == null) {
164 return createDatapathId(bridge.getDatapathIdColumn().getData());
168 public static DatapathId createDatapathId(Set<String> dpids) {
169 Preconditions.checkNotNull(dpids);
170 if (dpids.isEmpty()) {
173 String[] dpidArray = new String[dpids.size()];
174 dpids.toArray(dpidArray);
175 return createDatapathId(dpidArray[0]);
179 public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
180 String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
182 if (mdsalbridge.getDatapathType() != null) {
183 if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
184 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
186 throw new IllegalArgumentException("Unknown datapath type "
187 + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
193 public static Class<? extends DatapathTypeBase> createDatapathType(String type) {
194 Preconditions.checkNotNull(type);
195 if (type.isEmpty()) {
196 return DatapathTypeSystem.class;
198 ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
199 SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
200 return mapper.get(type);
204 public static DatapathId createDatapathId(String dpid) {
205 Preconditions.checkNotNull(dpid);
207 if (dpid.matches("^[0-9a-fA-F]{16}")) {
208 Splitter splitter = Splitter.fixedLength(2);
209 Joiner joiner = Joiner.on(":");
210 datapath = new DatapathId(joiner.join(splitter.split(dpid)));
212 datapath = new DatapathId(dpid);
217 public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
218 Set<String> protocols = new HashSet<>();
219 if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
220 for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
221 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
222 protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
224 throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
231 public static Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
232 Preconditions.checkNotNull(type);
233 return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
236 public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
237 Preconditions.checkNotNull(mdsaltype);
238 ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
239 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
240 return mapper.get(mdsaltype);
243 public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
244 Set<String> protocols = null;
246 protocols = bridge.getProtocolsColumn().getData();
247 } catch (SchemaVersionMismatchException e) {
248 // We don't care about the exception stack trace here
249 LOG.warn("protocols not supported by this version of ovsdb: {}", e.getMessage());
251 List<ProtocolEntry> protocolList = new ArrayList<>();
252 if (protocols != null && protocols.size() > 0) {
253 ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
254 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
255 for (String protocol : protocols) {
256 if (protocol != null && mapper.get(protocol) != null) {
257 protocolList.add(new ProtocolEntryBuilder().
258 setProtocol(mapper.get(protocol)).build());
266 * Create the {@link ControllerEntry} list given an OVSDB {@link Bridge}
267 * and {@link Controller} rows.
269 * @param bridge the {@link Bridge} to update
270 * @param updatedControllerRows the list of {@link Controller} controllers with updates
271 * @return list of {@link ControllerEntry} entries
273 public static List<ControllerEntry> createControllerEntries(Bridge bridge,
274 Map<UUID, Controller> updatedControllerRows) {
276 LOG.debug("createControllerEntries Bridge: {}\n, updatedControllerRows: {}",
277 bridge, updatedControllerRows);
278 final Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
279 final List<ControllerEntry> controllerEntries = new ArrayList<>();
280 for (UUID controllerUUID : controllerUUIDs ) {
281 final Controller controller = updatedControllerRows.get(controllerUUID);
282 addControllerEntries(controllerEntries, controller);
284 LOG.debug("controllerEntries: {}", controllerEntries);
285 return controllerEntries;
289 * Create the {@link ControllerEntry} list given an MDSAL {@link Node} bridge
290 * and {@link Controller} rows.
292 * @param bridgeNode the {@link Node} to update
293 * @param updatedControllerRows the list of {@link Controller} controllers with updates
294 * @return list of {@link ControllerEntry} entries
296 public static List<ControllerEntry> createControllerEntries(Node bridgeNode,
297 Map<UUID, Controller> updatedControllerRows) {
299 LOG.debug("createControllerEntries Bridge 2: {}\n, updatedControllerRows: {}",
300 bridgeNode, updatedControllerRows);
301 final List<ControllerEntry> controllerEntriesCreated = new ArrayList<>();
302 final OvsdbBridgeAugmentation ovsdbBridgeAugmentation =
303 bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
304 if (ovsdbBridgeAugmentation == null) {
305 return controllerEntriesCreated;
308 final List<ControllerEntry> controllerEntries = ovsdbBridgeAugmentation.getControllerEntry();
309 if (controllerEntries != null) {
310 for (ControllerEntry controllerEntry : controllerEntries) {
311 final Controller controller = updatedControllerRows.get(
312 new UUID(controllerEntry.getControllerUuid().getValue()));
313 addControllerEntries(controllerEntriesCreated, controller);
316 LOG.debug("controllerEntries: {}", controllerEntriesCreated);
317 return controllerEntriesCreated;
321 * Add the OVSDB {@link Controller} updates to the MDSAL {@link ControllerEntry} list.
323 * @param controllerEntries the list of {@link ControllerEntry} to update
324 * @param controller the updated OVSDB {@link Controller}
326 public static void addControllerEntries(List<ControllerEntry> controllerEntries,
327 final Controller controller) {
329 if (controller != null && controller.getTargetColumn() != null) {
330 final String targetString = controller.getTargetColumn().getData();
331 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
332 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
333 .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
335 controllerEntries.add(new ControllerEntryBuilder()
336 .setTarget(new Uri(targetString))
337 .setIsConnected(controller.getIsConnectedColumn().getData())
338 .setControllerUuid(uuid).build());
342 public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
343 List<ControllerEntry> controllerEntries = omn.getControllerEntry();
344 Map<UUID,Controller> controllerMap = new HashMap<>();
345 if (controllerEntries != null && !controllerEntries.isEmpty()) {
346 for (ControllerEntry controllerEntry : controllerEntries) {
347 String controllerNamedUUID = "Controller_" + getRandomUUID();
348 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
349 controller.setTarget(controllerEntry.getTarget().getValue());
350 controllerMap.put(new UUID(controllerNamedUUID), controller);
353 return controllerMap;
356 public static String getRandomUUID() {
357 return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
359 public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
360 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
361 connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
362 connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
363 connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
364 connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
365 return connectionInfoBuilder.build();
368 public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
369 ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
370 connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
371 connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
372 return connectionInfoBuilder.build();
376 * Create the {@link ManagerEntry} list given an OVSDB {@link OpenVSwitch}
377 * and {@link Manager} rows.
379 * @param ovsdbNode the {@link OpenVSwitch} to update
380 * @param updatedManagerRows the list of {@link Manager} managers with updates
381 * @return list of {@link ManagerEntry} entries
383 public static List<ManagerEntry> createManagerEntries(OpenVSwitch ovsdbNode,
384 Map<UUID, Manager> updatedManagerRows) {
386 LOG.debug("createManagerEntries OpenVSwitch: {}\n, updatedManagerRows: {}",
387 ovsdbNode, updatedManagerRows);
388 final Set<UUID> managerUUIDs = ovsdbNode.getManagerOptionsColumn().getData();
389 final List<ManagerEntry> managerEntries = new ArrayList<>();
390 for (UUID managerUUID : managerUUIDs ) {
391 final Manager manager = updatedManagerRows.get(managerUUID);
392 addManagerEntries(managerEntries, manager);
394 LOG.debug("managerEntries: {}", managerEntries);
395 return managerEntries;
399 * Create the {@link ManagerEntry} list given an MDSAL {@link Node} ovsdbNode
400 * and {@link Manager} rows.
402 * @param ovsdbNode the {@link Node} to update
403 * @param updatedManagerRows the list of {@link Manager} managers with updates
404 * @return list of {@link ManagerEntry} entries
406 public static List<ManagerEntry> createManagerEntries(Node ovsdbNode,
407 Map<Uri, Manager> updatedManagerRows) {
409 LOG.debug("createManagerEntries based on OVSDB Node: {}\n, updatedManagerRows: {}",
410 ovsdbNode, updatedManagerRows);
411 final List<ManagerEntry> managerEntriesCreated = new ArrayList<>();
412 final OvsdbNodeAugmentation ovsdbNodeAugmentation =
413 ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
414 if (ovsdbNodeAugmentation == null) {
415 return managerEntriesCreated;
418 final List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
419 if (managerEntries != null) {
420 for (ManagerEntry managerEntry : managerEntries) {
421 final Manager manager = updatedManagerRows.get(managerEntry.getTarget());
422 addManagerEntries(managerEntriesCreated, manager);
425 LOG.debug("managerEntries: {}", managerEntriesCreated);
426 return managerEntriesCreated;
430 * Add the OVSDB {@link Manager} updates to the MDSAL {@link ManagerEntry} list.
432 * @param managerEntries the list of {@link ManagerEntry} to update
433 * @param manager the updated OVSDB {@link Manager}
435 public static void addManagerEntries(List<ManagerEntry> managerEntries,
436 final Manager manager) {
438 if (manager != null && manager.getTargetColumn() != null) {
439 long numberOfConnections = 0;
440 final String targetString = manager.getTargetColumn().getData();
442 final Map<String, String> statusAttributeMap = manager.getStatusColumn().getData();
443 if (statusAttributeMap.containsKey(N_CONNECTIONS_STR)) {
444 String numberOfConnectionValueStr = statusAttributeMap.get(N_CONNECTIONS_STR);
445 numberOfConnections = Integer.parseInt(numberOfConnectionValueStr);
447 final boolean isConnected = manager.getIsConnectedColumn().getData();
449 numberOfConnections = 1;
452 managerEntries.add(new ManagerEntryBuilder()
453 .setTarget(new Uri(targetString))
454 .setNumberOfConnections(numberOfConnections)
455 .setConnected(manager.getIsConnectedColumn().getData()).build());
459 public static InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
460 InstanceIdentifier<Node> iid = null;
461 if (ovs.getExternalIdsColumn() != null
462 && ovs.getExternalIdsColumn().getData() != null
463 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
464 String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
465 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
467 String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
468 + ovs.getUuid().toString();
469 NodeId nodeId = new NodeId(new Uri(nodeString));
470 NodeKey nodeKey = new NodeKey(nodeId);
471 iid = InstanceIdentifier.builder(NetworkTopology.class)
472 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
473 .child(Node.class,nodeKey)