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