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