Fix race condition in initial connection.
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionManager.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 java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.ovsdb.lib.OvsdbClient;
20 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
21 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
22 import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeRemoveCommand;
23 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
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.OvsdbBridgeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Optional;
34 import com.google.common.base.Preconditions;
35 import com.google.common.util.concurrent.CheckedFuture;
36
37 public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoCloseable {
38     Map<ConnectionInfo,OvsdbConnectionInstance> clients
39         = new ConcurrentHashMap<ConnectionInfo,OvsdbConnectionInstance>();
40     private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionManager.class);
41
42     private DataBroker db;
43     private TransactionInvoker txInvoker;
44
45     public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker) {
46         this.db = db;
47         this.txInvoker = txInvoker;
48     }
49
50     @Override
51     public void connected(final OvsdbClient externalClient) {
52         LOG.info("OVSDB Connection from {}:{}",externalClient.getConnectionInfo().getRemoteAddress(),
53                 externalClient.getConnectionInfo().getRemotePort());
54         ConnectionInfo key = SouthboundMapper.createConnectionInfo(externalClient);
55         OvsdbConnectionInstance client = new OvsdbConnectionInstance(key,externalClient,txInvoker);
56         putConnectionInstance(key, client);
57         client.init();
58     }
59
60     @Override
61     public void disconnected(OvsdbClient client) {
62         LOG.info("OVSDB Disconnect from {}:{}",client.getConnectionInfo().getRemoteAddress(),
63                 client.getConnectionInfo().getRemotePort());
64         ConnectionInfo key = SouthboundMapper.createConnectionInfo(client);
65         txInvoker.invoke(new OvsdbNodeRemoveCommand(key,null,null));
66         clients.remove(key);
67     }
68
69     public OvsdbClient connect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
70         // TODO handle case where we already have a connection
71         // TODO use transaction chains to handle ordering issues between disconnected
72         // and connected when writing to the operational store
73         InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getConnectionInfo().getRemoteIp());
74         OvsdbClient client = OvsdbConnectionService.getService().connect(ip,
75                 ovsdbNode.getConnectionInfo().getRemotePort().getValue().intValue());
76         // For connections from the controller to the ovs instance, the library doesn't call
77         // this method for us
78         connected(client);
79         return client;
80     }
81
82     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
83         OvsdbClient client = getConnectionInstance(ovsdbNode.getConnectionInfo());
84         if (client != null) {
85             client.disconnect();
86         }
87     }
88
89     @Override
90     public void close() throws Exception {
91         for (OvsdbClient client: clients.values()) {
92             client.disconnect();
93         }
94     }
95
96     private void putConnectionInstance(ConnectionInfo key,OvsdbConnectionInstance instance) {
97         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
98         clients.put(connectionInfo, instance);
99     }
100
101     public OvsdbConnectionInstance getConnectionInstance(ConnectionInfo key) {
102         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
103         return clients.get(connectionInfo);
104     }
105
106     public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
107         Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
108         if (optional.isPresent()) {
109             return getConnectionInstance(optional.get().getConnectionInfo());
110         } else {
111             return null;
112         }
113     }
114
115     public OvsdbConnectionInstance getConnectionInstance(Node node) {
116         Preconditions.checkNotNull(node);
117         OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
118         OvsdbBridgeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbBridgeAugmentation.class);
119         if (ovsdbNode != null) {
120             return getConnectionInstance(ovsdbNode.getConnectionInfo());
121         } else if (ovsdbManagedNode != null) {
122             return getConnectionInstance(ovsdbManagedNode);
123         } else {
124             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
125             return null;
126         }
127     }
128
129     public OvsdbConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
130         try {
131             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
132             CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
133                     LogicalDatastoreType.OPERATIONAL, nodePath);
134             transaction.close();
135             Optional<Node> optional = nodeFuture.get();
136             if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
137                 return this.getConnectionInstance(optional.get());
138             } else {
139                 LOG.warn("Found non-topological node {} on path {}",optional);
140                 return null;
141             }
142         } catch (Exception e) {
143             LOG.warn("Failed to get Ovsdb Node {}",nodePath, e);
144             return null;
145         }
146     }
147
148     public OvsdbClient getClient(ConnectionInfo connectionInfo) {
149         return getConnectionInstance(connectionInfo);
150     }
151
152     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
153         return getConnectionInstance(mn);
154     }
155
156     public OvsdbClient getClient(Node node) {
157         return getConnectionInstance(node);
158     }
159 }