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