2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.southbound;
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
13 import java.util.concurrent.ConcurrentHashMap;
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;
33 import com.google.common.base.Optional;
34 import com.google.common.base.Preconditions;
35 import com.google.common.util.concurrent.CheckedFuture;
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);
42 private DataBroker db;
43 private TransactionInvoker txInvoker;
45 public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker) {
47 this.txInvoker = txInvoker;
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);
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));
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
82 public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
83 OvsdbClient client = getConnectionInstance(ovsdbNode.getConnectionInfo());
90 public void close() throws Exception {
91 for (OvsdbClient client: clients.values()) {
96 private void putConnectionInstance(ConnectionInfo key,OvsdbConnectionInstance instance) {
97 ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
98 clients.put(connectionInfo, instance);
101 public OvsdbConnectionInstance getConnectionInstance(ConnectionInfo key) {
102 ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
103 return clients.get(connectionInfo);
106 public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
107 Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
108 if (optional.isPresent()) {
109 return getConnectionInstance(optional.get().getConnectionInfo());
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);
124 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
129 public OvsdbConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
131 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
132 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
133 LogicalDatastoreType.OPERATIONAL, nodePath);
135 Optional<Node> optional = nodeFuture.get();
136 if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
137 return this.getConnectionInstance(optional.get());
139 LOG.warn("Found non-topological node {} on path {}",optional);
142 } catch (Exception e) {
143 LOG.warn("Failed to get Ovsdb Node {}",nodePath, e);
148 public OvsdbClient getClient(ConnectionInfo connectionInfo) {
149 return getConnectionInstance(connectionInfo);
152 public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
153 return getConnectionInstance(mn);
156 public OvsdbClient getClient(Node node) {
157 return getConnectionInstance(node);