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.util.Enumeration;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class SouthboundUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
35 private static InstanceIdentifierCodec instanceIdentifierCodec;
37 public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
38 instanceIdentifierCodec = iidc;
41 public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
42 return instanceIdentifierCodec.serialize(iid);
45 public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
46 InstanceIdentifier<?> result = null;
48 result = instanceIdentifierCodec.bindingDeserializer(iidString);
49 } catch (DeserializationException e) {
50 LOG.warn("Unable to deserialize iidString", e);
56 public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
57 Preconditions.checkNotNull(mn);
59 OvsdbNodeRef ref = mn.getManagedBy();
60 if (ref != null && ref.getValue() != null) {
61 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
62 @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
63 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
65 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
66 LogicalDatastoreType.OPERATIONAL, path);
68 Optional<Node> optional = nf.get();
69 if (optional != null && optional.isPresent()) {
70 OvsdbNodeAugmentation ovsdbNode = null;
71 if (optional.get() instanceof Node) {
72 ovsdbNode = optional.get().getAugmentation(OvsdbNodeAugmentation.class);
73 } else if (optional.get() instanceof OvsdbNodeAugmentation) {
74 ovsdbNode = (OvsdbNodeAugmentation) optional.get();
76 if (ovsdbNode != null) {
77 return Optional.of(ovsdbNode);
79 LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
80 + "that OvsdbNode does not exist", mn, ref.getValue());
81 return Optional.absent();
84 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
85 return Optional.absent();
88 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
89 return Optional.absent();
91 } catch (Exception e) {
92 LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
93 return Optional.absent();
97 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
98 ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
99 Optional<D> node = Optional.absent();
101 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
102 } catch (final ReadFailedException e) {
103 LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
108 private static String getLocalControllerHostIpAddress() {
109 String ipaddress = null;
111 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
112 ifaces.hasMoreElements();) {
113 NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
115 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
116 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
117 if (!inetAddr.isLoopbackAddress()) {
118 if (inetAddr.isSiteLocalAddress()) {
119 ipaddress = inetAddr.getHostAddress();
125 } catch (Exception e) {
126 LOG.warn("Exception while fetching local host ip address ",e);
131 public static String getControllerTarget(Node ovsdbNode) {
132 String target = null;
133 String ipAddr = null;
134 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
135 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
136 LOG.info("connectionInfo: {}", connectionInfo);
137 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
138 ipAddr = new String(connectionInfo.getLocalIp().getValue());
140 if (ipAddr == null) {
141 ipAddr = getLocalControllerHostIpAddress();
144 if (ipAddr != null) {
145 target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
146 + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;