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.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.net.InetAddress;
14 import java.net.NetworkInterface;
15 import java.net.SocketException;
16 import java.util.Enumeration;
17 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public 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 static InstanceIdentifierCodec instanceIdentifierCodec;
43 private SouthboundUtil() {
44 // Prevent instantiating a utility class
47 public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
48 instanceIdentifierCodec = iidc;
51 public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
52 return instanceIdentifierCodec;
55 public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
56 return instanceIdentifierCodec.serialize(iid);
59 public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
60 InstanceIdentifier<?> result = null;
62 result = instanceIdentifierCodec.bindingDeserializer(iidString);
63 } catch (DeserializationException e) {
64 LOG.warn("Unable to deserialize iidString", e);
70 public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
71 Preconditions.checkNotNull(mn);
73 OvsdbNodeRef ref = mn.getManagedBy();
74 if (ref != null && ref.getValue() != null) {
75 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
76 @SuppressWarnings("unchecked")
77 // Note: erasure makes this safe in combination with the typecheck below
78 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
80 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
81 LogicalDatastoreType.OPERATIONAL, path);
83 Optional<Node> optional = nf.get();
84 if (optional != null && optional.isPresent()) {
85 OvsdbNodeAugmentation ovsdbNode = null;
86 Node node = optional.get();
87 if (node instanceof OvsdbNodeAugmentation) {
88 ovsdbNode = (OvsdbNodeAugmentation) node;
89 } else if (node != null) {
90 ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
92 if (ovsdbNode != null) {
93 return Optional.of(ovsdbNode);
95 LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
96 + "that OvsdbNode does not exist", mn, ref.getValue());
97 return Optional.absent();
100 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
101 return Optional.absent();
104 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
105 return Optional.absent();
107 } catch (InterruptedException | ExecutionException e) {
108 LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
109 return Optional.absent();
113 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
114 ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
115 Optional<D> node = Optional.absent();
117 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
118 } catch (final ReadFailedException e) {
119 LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
124 private static String getLocalControllerHostIpAddress() {
125 String ipaddress = null;
127 Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
128 if (ifaces != null) {
129 while (ifaces.hasMoreElements()) {
130 NetworkInterface iface = ifaces.nextElement();
132 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
133 InetAddress inetAddr = inetAddrs.nextElement();
134 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
135 ipaddress = inetAddr.getHostAddress();
141 LOG.warn("Local Host don't have any associated IP address");
143 } catch (SocketException e) {
144 LOG.warn("Exception while fetching local host ip address ",e);
149 public static String getControllerTarget(Node ovsdbNode) {
150 String target = null;
151 String ipAddr = null;
152 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
153 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
154 LOG.info("connectionInfo: {}", connectionInfo);
155 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
156 ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
158 if (ipAddr == null) {
159 ipAddr = getLocalControllerHostIpAddress();
162 if (ipAddr != null) {
163 target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
164 + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
170 public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
171 return String.valueOf(
172 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
175 public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
176 LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());