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