Fix Sonar (soon Checkstyle) TrailingComment
[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")
74                 // Note: erasure makes this safe in combination with the typecheck below
75                 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
76
77                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
78                         LogicalDatastoreType.OPERATIONAL, path);
79                 transaction.close();
80                 Optional<Node> optional = nf.get();
81                 if (optional != null && optional.isPresent()) {
82                     OvsdbNodeAugmentation ovsdbNode = null;
83                     Node node = optional.get();
84                     if (node instanceof OvsdbNodeAugmentation) {
85                         ovsdbNode = (OvsdbNodeAugmentation) node;
86                     } else if (node != null) {
87                         ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
88                     }
89                     if (ovsdbNode != null) {
90                         return Optional.of(ovsdbNode);
91                     } else {
92                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
93                                 + "that OvsdbNode does not exist", mn, ref.getValue());
94                         return Optional.absent();
95                     }
96                 } else {
97                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
98                     return Optional.absent();
99                 }
100             } else {
101                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
102                 return Optional.absent();
103             }
104         } catch (Exception e) {
105             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
106             return Optional.absent();
107         }
108     }
109
110     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
111             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
112         Optional<D> node = Optional.absent();
113         try {
114             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
115         } catch (final ReadFailedException e) {
116             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
117         }
118         return node;
119     }
120
121     private static String getLocalControllerHostIpAddress() {
122         String ipaddress = null;
123         try {
124             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
125                  ifaces.hasMoreElements();) {
126                 NetworkInterface iface = ifaces.nextElement();
127
128                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
129                     InetAddress inetAddr = inetAddrs.nextElement();
130                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
131                         ipaddress = inetAddr.getHostAddress();
132                         break;
133                     }
134                 }
135             }
136         } catch (Exception e) {
137             LOG.warn("Exception while fetching local host ip address ",e);
138         }
139         return ipaddress;
140     }
141
142     public static String getControllerTarget(Node ovsdbNode) {
143         String target = null;
144         String ipAddr = null;
145         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
146         ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
147         LOG.info("connectionInfo: {}", connectionInfo);
148         if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
149             ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
150         }
151         if (ipAddr == null) {
152             ipAddr = getLocalControllerHostIpAddress();
153         }
154
155         if (ipAddr != null) {
156             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
157                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
158         }
159
160         return target;
161     }
162
163     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
164         return String.valueOf(
165                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
166     }
167
168     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
169         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());
170     }
171 }