2 * Copyright (c) 2014 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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.net.InetAddress;
15 import java.net.NetworkInterface;
16 import java.net.SocketException;
17 import java.util.Enumeration;
18 import java.util.concurrent.ExecutionException;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public final class SouthboundUtil {
37 private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
38 private static final String SCHEMA_VERSION_MISMATCH =
39 "{} column for {} table is not supported by this version of the {} schema: {}";
41 private SouthboundUtil() {
42 // Prevent instantiating a utility class
45 public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
46 Preconditions.checkNotNull(mn);
48 OvsdbNodeRef ref = mn.getManagedBy();
49 if (ref != null && ref.getValue() != null) {
50 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
51 @SuppressWarnings("unchecked")
52 // Note: erasure makes this safe in combination with the typecheck below
53 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
55 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
56 LogicalDatastoreType.OPERATIONAL, path);
58 Optional<Node> optional = nf.get();
59 if (optional != null && optional.isPresent()) {
60 OvsdbNodeAugmentation ovsdbNode = null;
61 Node node = optional.get();
62 if (node instanceof OvsdbNodeAugmentation) {
63 ovsdbNode = (OvsdbNodeAugmentation) node;
64 } else if (node != null) {
65 ovsdbNode = node.augmentation(OvsdbNodeAugmentation.class);
67 if (ovsdbNode != null) {
68 return Optional.of(ovsdbNode);
70 LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
71 + "that OvsdbNode does not exist", mn, ref.getValue());
72 return Optional.absent();
75 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
76 return Optional.absent();
79 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
80 return Optional.absent();
82 } catch (InterruptedException | ExecutionException e) {
83 LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
84 return Optional.absent();
88 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
89 ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
90 Optional<D> node = Optional.absent();
92 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
93 } catch (final ReadFailedException e) {
94 LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
100 static String getLocalControllerHostIpAddress() {
101 String ipaddress = null;
103 Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
104 if (ifaces != null) {
105 while (ifaces.hasMoreElements()) {
106 NetworkInterface iface = ifaces.nextElement();
108 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
109 InetAddress inetAddr = inetAddrs.nextElement();
110 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
111 ipaddress = inetAddr.getHostAddress();
117 LOG.warn("Local Host don't have any associated IP address");
119 } catch (SocketException e) {
120 LOG.warn("Exception while fetching local host ip address ",e);
125 public static String getControllerTarget(Node ovsdbNode) {
126 String target = null;
127 String ipAddr = null;
128 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.augmentation(OvsdbNodeAugmentation.class);
129 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
130 LOG.info("connectionInfo: {}", connectionInfo);
131 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
132 ipAddr = connectionInfo.getLocalIp().stringValue();
134 if (ipAddr == null) {
135 ipAddr = getLocalControllerHostIpAddress();
138 if (ipAddr != null) {
139 target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
140 + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
146 public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
147 return connectionInfo.getRemoteIp().stringValue() + ":" + connectionInfo.getRemotePort().getValue();
150 public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
151 LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());