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.overlay.rev150105.IpPortLocator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
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<OvsdbClientKey,OvsdbConnectionInstance> clients
39 = new ConcurrentHashMap<OvsdbClientKey,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 OvsdbClientKey key = new OvsdbClientKey(externalClient);
55 OvsdbConnectionInstance client = new OvsdbConnectionInstance(key,externalClient,txInvoker);
56 clients.put(key, client);
60 public void disconnected(OvsdbClient client) {
61 LOG.info("OVSDB Disconnect from {}:{}",client.getConnectionInfo().getRemoteAddress(),
62 client.getConnectionInfo().getRemotePort());
63 OvsdbClientKey key = new OvsdbClientKey(client);
64 txInvoker.invoke(new OvsdbNodeRemoveCommand(key,null,null));
68 public OvsdbClient connect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
69 // TODO handle case where we already have a connection
70 // TODO use transaction chains to handle ordering issues between disconnected
71 // and connected when writing to the operational store
72 InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getIp());
73 OvsdbClient client = OvsdbConnectionService.getService().connect(ip,
74 ovsdbNode.getPort().getValue().intValue());
75 // For connections from the controller to the ovs instance, the library doesn't call
81 public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
82 OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
83 OvsdbClient client = clients.get(key);
90 public void close() throws Exception {
91 for (OvsdbClient client: clients.values()) {
96 public OvsdbConnectionInstance getConnectionInstance(OvsdbClientKey key) {
97 return clients.get(key);
100 public OvsdbConnectionInstance getConnectionInstance(IpPortLocator loc) {
101 Preconditions.checkNotNull(loc);
102 return getConnectionInstance(new OvsdbClientKey(loc));
105 public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
106 Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
107 if (optional.isPresent()) {
108 return getConnectionInstance(optional.get());
114 public OvsdbConnectionInstance getConnectionInstance(Node node) {
115 Preconditions.checkNotNull(node);
116 OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
117 OvsdbBridgeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbBridgeAugmentation.class);
118 if (ovsdbNode != null) {
119 return getConnectionInstance(ovsdbNode);
120 } else if (ovsdbManagedNode != null) {
121 return getConnectionInstance(ovsdbManagedNode);
123 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
128 public OvsdbConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
130 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
131 CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
132 LogicalDatastoreType.OPERATIONAL, nodePath);
134 Optional<Node> optional = nodeFuture.get();
135 if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
136 return this.getConnectionInstance(optional.get());
138 LOG.warn("Found non-topological node {} on path {}",optional);
141 } catch (Exception e) {
142 LOG.warn("Failed to get Ovsdb Node {}",nodePath, e);
147 public OvsdbClient getClient(OvsdbClientKey key) {
148 return getConnectionInstance(key);
151 public OvsdbClient getClient(IpPortLocator loc) {
152 return getConnectionInstance(loc);
155 public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
156 return getConnectionInstance(mn);
159 public OvsdbClient getClient(Node node) {
160 return getConnectionInstance(node);