Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[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.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public 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 static InstanceIdentifierCodec instanceIdentifierCodec;
42
43     private SouthboundUtil() {
44         // Prevent instantiating a utility class
45     }
46
47     public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
48         instanceIdentifierCodec = iidc;
49     }
50
51     public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
52         return instanceIdentifierCodec;
53     }
54
55     public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
56         return instanceIdentifierCodec.serialize(iid);
57     }
58
59     public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
60         InstanceIdentifier<?> result = null;
61         try {
62             result = instanceIdentifierCodec.bindingDeserializer(iidString);
63         } catch (DeserializationException e) {
64             LOG.warn("Unable to deserialize iidString", e);
65         }
66         return result;
67     }
68
69
70     public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
71         Preconditions.checkNotNull(mn);
72         try {
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();
79
80                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
81                         LogicalDatastoreType.OPERATIONAL, path);
82                 transaction.close();
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);
91                     }
92                     if (ovsdbNode != null) {
93                         return Optional.of(ovsdbNode);
94                     } else {
95                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
96                                 + "that OvsdbNode does not exist", mn, ref.getValue());
97                         return Optional.absent();
98                     }
99                 } else {
100                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
101                     return Optional.absent();
102                 }
103             } else {
104                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
105                 return Optional.absent();
106             }
107         } catch (InterruptedException | ExecutionException e) {
108             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
109             return Optional.absent();
110         }
111     }
112
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();
116         try {
117             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
118         } catch (final ReadFailedException e) {
119             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
120         }
121         return node;
122     }
123
124     private static String getLocalControllerHostIpAddress() {
125         String ipaddress = null;
126         try {
127             Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
128             if (ifaces != null) {
129                 while (ifaces.hasMoreElements()) {
130                     NetworkInterface iface = ifaces.nextElement();
131
132                     for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
133                         InetAddress inetAddr = inetAddrs.nextElement();
134                         if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
135                             ipaddress = inetAddr.getHostAddress();
136                             break;
137                         }
138                     }
139                 }
140             } else {
141                 LOG.warn("Local Host don't have any associated IP address");
142             }
143         } catch (SocketException e) {
144             LOG.warn("Exception while fetching local host ip address ",e);
145         }
146         return ipaddress;
147     }
148
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());
157         }
158         if (ipAddr == null) {
159             ipAddr = getLocalControllerHostIpAddress();
160         }
161
162         if (ipAddr != null) {
163             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
164                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
165         }
166
167         return target;
168     }
169
170     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
171         return String.valueOf(
172                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
173     }
174
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());
177     }
178 }