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 InstanceIdentifier<Node> nodePath = InstanceIdentifier
97 .create(NetworkTopology.class)
98 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
99 .child(Node.class,new NodeKey(nodeId));
103 public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbConnectionInstance client,Bridge bridge) {
104 InstanceIdentifier<Node> iid;
105 if (bridge.getExternalIdsColumn() != null
106 && bridge.getExternalIdsColumn().getData() != null
107 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
108 String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
109 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
111 String nodeString = client.getNodeKey().getNodeId().getValue()
112 + "/bridge/" + bridge.getName();
113 NodeId nodeId = new NodeId(new Uri(nodeString));
114 NodeKey nodeKey = new NodeKey(nodeId);
115 iid = InstanceIdentifier.builder(NetworkTopology.class)
116 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
117 .child(Node.class,nodeKey)
123 public static InstanceIdentifier<Node> createInstanceIdentifier(
124 OvsdbConnectionInstance client, Controller controller, String bridgeName) {
125 InstanceIdentifier<Node> iid;
126 if (controller.getExternalIdsColumn() != null
127 && controller.getExternalIdsColumn().getData() != null
128 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
129 String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
130 iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
132 // TODO retrieve bridge name
133 String nodeString = client.getNodeKey().getNodeId().getValue()
134 + "/bridge/" + bridgeName;
135 NodeId nodeId = new NodeId(new Uri(nodeString));
136 NodeKey nodeKey = new NodeKey(nodeId);
137 iid = InstanceIdentifier.builder(NetworkTopology.class)
138 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
139 .child(Node.class,nodeKey)
145 public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
146 NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
147 return nodeKey.getNodeId();
150 public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
151 if (ip.getIpv4Address() != null) {
152 return InetAddress.getByName(ip.getIpv4Address().getValue());
153 } else if (ip.getIpv6Address() != null) {
154 return InetAddress.getByName(ip.getIpv6Address().getValue());
156 throw new UnknownHostException("IP Address has no value");
160 public static DatapathId createDatapathId(Bridge bridge) {
161 Preconditions.checkNotNull(bridge);
162 if (bridge.getDatapathIdColumn() == null) {
165 return createDatapathId(bridge.getDatapathIdColumn().getData());
169 public static DatapathId createDatapathId(Set<String> dpids) {
170 Preconditions.checkNotNull(dpids);
171 if (dpids.isEmpty()) {
174 String[] dpidArray = new String[dpids.size()];
175 dpids.toArray(dpidArray);
176 return createDatapathId(dpidArray[0]);
180 public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
181 String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
183 if (mdsalbridge.getDatapathType() != null) {
184 if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
185 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
187 throw new IllegalArgumentException("Unknown datapath type "
188 + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
194 public static Class<? extends DatapathTypeBase> createDatapathType(String type) {
195 Preconditions.checkNotNull(type);
196 if (type.isEmpty()) {
197 return DatapathTypeSystem.class;
199 ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
200 SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
201 return mapper.get(type);
205 public static DatapathId createDatapathId(String dpid) {
206 Preconditions.checkNotNull(dpid);
208 if (dpid.matches("^[0-9a-fA-F]{16}")) {
209 Splitter splitter = Splitter.fixedLength(2);
210 Joiner joiner = Joiner.on(":");
211 datapath = new DatapathId(joiner.join(splitter.split(dpid)));
213 datapath = new DatapathId(dpid);
218 public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
219 Set<String> protocols = new HashSet<>();
220 if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
221 for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
222 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
223 protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
225 throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
232 public static Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
233 Preconditions.checkNotNull(type);
234 return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
237 public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
238 Preconditions.checkNotNull(mdsaltype);
239 ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
240 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
241 return mapper.get(mdsaltype);
244 public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
245 Set<String> protocols = null;
247 protocols = bridge.getProtocolsColumn().getData();
248 } catch (SchemaVersionMismatchException e) {
249 LOG.warn("protocols not supported by this version of ovsdb", e);
251 List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
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((Class<? extends OvsdbBridgeProtocolBase>) 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<ControllerEntry>();
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<ControllerEntry>();
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<ManagerEntry>();
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<ManagerEntry>();
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 = (String)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());