Merge "Bug 4789 - allowed address pair doesn't have port id"
[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.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SouthboundUtil {
35
36     private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
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     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteNode(
121             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
122         boolean result = false;
123         transaction.delete(LogicalDatastoreType.OPERATIONAL, connectionIid);
124         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
125         try {
126             future.checkedGet();
127             result = true;
128         } catch (TransactionCommitFailedException e) {
129             LOG.warn("Failed to delete {} ", connectionIid, e);
130         }
131         return result;
132     }
133
134     private static String getLocalControllerHostIpAddress() {
135         String ipaddress = null;
136         try {
137             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
138                  ifaces.hasMoreElements();) {
139                 NetworkInterface iface = ifaces.nextElement();
140
141                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
142                     InetAddress inetAddr = inetAddrs.nextElement();
143                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
144                         ipaddress = inetAddr.getHostAddress();
145                         break;
146                     }
147                 }
148             }
149         } catch (Exception e) {
150             LOG.warn("Exception while fetching local host ip address ",e);
151         }
152         return ipaddress;
153     }
154
155     public static String getControllerTarget(Node ovsdbNode) {
156         String target = null;
157         String ipAddr = null;
158         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
159         ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
160         LOG.info("connectionInfo: {}", connectionInfo);
161         if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
162             ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
163         }
164         if (ipAddr == null) {
165             ipAddr = getLocalControllerHostIpAddress();
166         }
167
168         if (ipAddr != null) {
169             target = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
170                     + ipAddr + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
171         }
172
173         return target;
174     }
175
176     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
177         return String.valueOf(
178                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
179     }
180 }