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