Bug 6692: remove InstanceIdentifierCodec instance from SBU
[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.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;
18
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.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SouthboundUtil {
35
36     private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
37     private static final String SCHEMA_VERSION_MISMATCH =
38             "{} column for {} table is not supported by this version of the {} schema: {}";
39
40     private SouthboundUtil() {
41         // Prevent instantiating a utility class
42     }
43
44     public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
45         Preconditions.checkNotNull(mn);
46         try {
47             OvsdbNodeRef ref = mn.getManagedBy();
48             if (ref != null && ref.getValue() != null) {
49                 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
50                 @SuppressWarnings("unchecked")
51                 // Note: erasure makes this safe in combination with the typecheck below
52                 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
53
54                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
55                         LogicalDatastoreType.OPERATIONAL, path);
56                 transaction.close();
57                 Optional<Node> optional = nf.get();
58                 if (optional != null && optional.isPresent()) {
59                     OvsdbNodeAugmentation ovsdbNode = null;
60                     Node node = optional.get();
61                     if (node instanceof OvsdbNodeAugmentation) {
62                         ovsdbNode = (OvsdbNodeAugmentation) node;
63                     } else if (node != null) {
64                         ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
65                     }
66                     if (ovsdbNode != null) {
67                         return Optional.of(ovsdbNode);
68                     } else {
69                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
70                                 + "that OvsdbNode does not exist", mn, ref.getValue());
71                         return Optional.absent();
72                     }
73                 } else {
74                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
75                     return Optional.absent();
76                 }
77             } else {
78                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
79                 return Optional.absent();
80             }
81         } catch (InterruptedException | ExecutionException e) {
82             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
83             return Optional.absent();
84         }
85     }
86
87     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
88             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
89         Optional<D> node = Optional.absent();
90         try {
91             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
92         } catch (final ReadFailedException e) {
93             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
94         }
95         return node;
96     }
97
98     private static String getLocalControllerHostIpAddress() {
99         String ipaddress = null;
100         try {
101             Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
102             if (ifaces != null) {
103                 while (ifaces.hasMoreElements()) {
104                     NetworkInterface iface = ifaces.nextElement();
105
106                     for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
107                         InetAddress inetAddr = inetAddrs.nextElement();
108                         if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
109                             ipaddress = inetAddr.getHostAddress();
110                             break;
111                         }
112                     }
113                 }
114             } else {
115                 LOG.warn("Local Host don't have any associated IP address");
116             }
117         } catch (SocketException e) {
118             LOG.warn("Exception while fetching local host ip address ",e);
119         }
120         return ipaddress;
121     }
122
123     public static String getControllerTarget(Node ovsdbNode) {
124         String target = null;
125         String ipAddr = null;
126         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
127         ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
128         LOG.info("connectionInfo: {}", connectionInfo);
129         if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
130             ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
131         }
132         if (ipAddr == null) {
133             ipAddr = getLocalControllerHostIpAddress();
134         }
135
136         if (ipAddr != null) {
137             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
138                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
139         }
140
141         return target;
142     }
143
144     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
145         return String.valueOf(
146                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
147     }
148
149     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
150         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());
151     }
152 }