ovsdb enable checkstyle on error
[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.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.ovsdb.lib.error.SchemaVersionMismatchException;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SouthboundUtil {
33
34     private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
35     private static final String SCHEMA_VERSION_MISMATCH =
36             "{} column for {} table is not supported by this version of the {} schema: {}";
37
38     private static InstanceIdentifierCodec instanceIdentifierCodec;
39
40     private SouthboundUtil() {
41         // Prevent instantiating a utility class
42     }
43
44     public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
45         instanceIdentifierCodec = iidc;
46     }
47
48     public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
49         return instanceIdentifierCodec;
50     }
51
52     public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
53         return instanceIdentifierCodec.serialize(iid);
54     }
55
56     public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
57         InstanceIdentifier<?> result = null;
58         try {
59             result = instanceIdentifierCodec.bindingDeserializer(iidString);
60         } catch (DeserializationException e) {
61             LOG.warn("Unable to deserialize iidString", e);
62         }
63         return result;
64     }
65
66
67     public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
68         Preconditions.checkNotNull(mn);
69         try {
70             OvsdbNodeRef ref = mn.getManagedBy();
71             if (ref != null && ref.getValue() != null) {
72                 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
73                 @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
74                 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
75
76                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
77                         LogicalDatastoreType.OPERATIONAL, path);
78                 transaction.close();
79                 Optional<Node> optional = nf.get();
80                 if (optional != null && optional.isPresent()) {
81                     OvsdbNodeAugmentation ovsdbNode = null;
82                     Node node = optional.get();
83                     if (node instanceof OvsdbNodeAugmentation) {
84                         ovsdbNode = (OvsdbNodeAugmentation) node;
85                     } else if (node != null) {
86                         ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
87                     }
88                     if (ovsdbNode != null) {
89                         return Optional.of(ovsdbNode);
90                     } else {
91                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
92                                 + "that OvsdbNode does not exist", mn, ref.getValue());
93                         return Optional.absent();
94                     }
95                 } else {
96                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
97                     return Optional.absent();
98                 }
99             } else {
100                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
101                 return Optional.absent();
102             }
103         } catch (Exception e) {
104             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
105             return Optional.absent();
106         }
107     }
108
109     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
110             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
111         Optional<D> node = Optional.absent();
112         try {
113             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
114         } catch (final ReadFailedException e) {
115             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
116         }
117         return node;
118     }
119
120     private static String getLocalControllerHostIpAddress() {
121         String ipaddress = null;
122         try {
123             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
124                  ifaces.hasMoreElements();) {
125                 NetworkInterface iface = ifaces.nextElement();
126
127                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
128                     InetAddress inetAddr = inetAddrs.nextElement();
129                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
130                         ipaddress = inetAddr.getHostAddress();
131                         break;
132                     }
133                 }
134             }
135         } catch (Exception e) {
136             LOG.warn("Exception while fetching local host ip address ",e);
137         }
138         return ipaddress;
139     }
140
141     public static String getControllerTarget(Node ovsdbNode) {
142         String target = null;
143         String ipAddr = null;
144         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
145         ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
146         LOG.info("connectionInfo: {}", connectionInfo);
147         if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
148             ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
149         }
150         if (ipAddr == null) {
151             ipAddr = getLocalControllerHostIpAddress();
152         }
153
154         if (ipAddr != null) {
155             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
156                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
157         }
158
159         return target;
160     }
161
162     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
163         return String.valueOf(
164                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
165     }
166
167     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
168         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());
169     }
170 }