Expose methods for testing
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound;
9
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;
19
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;
34
35 public final class SouthboundUtil {
36
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: {}";
40
41     private SouthboundUtil() {
42         // Prevent instantiating a utility class
43     }
44
45     public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
46         Preconditions.checkNotNull(mn);
47         try {
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();
54
55                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
56                         LogicalDatastoreType.OPERATIONAL, path);
57                 transaction.close();
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);
66                     }
67                     if (ovsdbNode != null) {
68                         return Optional.of(ovsdbNode);
69                     } else {
70                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
71                                 + "that OvsdbNode does not exist", mn, ref.getValue());
72                         return Optional.absent();
73                     }
74                 } else {
75                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
76                     return Optional.absent();
77                 }
78             } else {
79                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
80                 return Optional.absent();
81             }
82         } catch (InterruptedException | ExecutionException e) {
83             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
84             return Optional.absent();
85         }
86     }
87
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();
91         try {
92             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
93         } catch (final ReadFailedException e) {
94             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
95         }
96         return node;
97     }
98
99     @VisibleForTesting
100     static String getLocalControllerHostIpAddress() {
101         String ipaddress = null;
102         try {
103             Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
104             if (ifaces != null) {
105                 while (ifaces.hasMoreElements()) {
106                     NetworkInterface iface = ifaces.nextElement();
107
108                     for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
109                         InetAddress inetAddr = inetAddrs.nextElement();
110                         if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
111                             ipaddress = inetAddr.getHostAddress();
112                             break;
113                         }
114                     }
115                 }
116             } else {
117                 LOG.warn("Local Host don't have any associated IP address");
118             }
119         } catch (SocketException e) {
120             LOG.warn("Exception while fetching local host ip address ",e);
121         }
122         return ipaddress;
123     }
124
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 = String.valueOf(connectionInfo.getLocalIp().getValue());
133         }
134         if (ipAddr == null) {
135             ipAddr = getLocalControllerHostIpAddress();
136         }
137
138         if (ipAddr != null) {
139             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
140                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
141         }
142
143         return target;
144     }
145
146     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
147         return String.valueOf(
148                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
149     }
150
151     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
152         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());
153     }
154 }