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.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Optional;
35 import com.google.common.base.Preconditions;
36 import com.google.common.util.concurrent.CheckedFuture;
38 public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoCloseable {
39 Map<OvsdbClientKey,OvsdbConnectionInstance> clients = 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 and connected when writing to the operational store
71 InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getIp());
72 OvsdbClient client = OvsdbConnectionService.getService().connect(ip, ovsdbNode.getPort().getValue().intValue());
73 connected(client); // For connections from the controller to the ovs instance, the library doesn't call this method for us
77 public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
78 OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
79 OvsdbClient client = clients.get(key);
86 public void close() throws Exception {
87 for(OvsdbClient client: clients.values()) {
92 public OvsdbConnectionInstance getConnectionInstance(OvsdbClientKey key) {
93 return clients.get(key);
96 public OvsdbConnectionInstance getConnectionInstance(IpPortLocator loc) {
97 Preconditions.checkNotNull(loc);
98 return getConnectionInstance(new OvsdbClientKey(loc));
101 public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
102 Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
103 if(optional.isPresent()) {
104 return getConnectionInstance(optional.get());
110 public OvsdbConnectionInstance getConnectionInstance(Node node) {
111 Preconditions.checkNotNull(node);
112 OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
113 OvsdbBridgeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbBridgeAugmentation.class);
114 if(ovsdbNode != null) {
115 return getConnectionInstance(ovsdbNode);
116 } else if (ovsdbManagedNode != null) {
117 return getConnectionInstance(ovsdbManagedNode);
119 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
124 public OvsdbClient getClient(OvsdbClientKey key) {
125 return getConnectionInstance(key);
128 public OvsdbClient getClient(IpPortLocator loc) {
129 return getConnectionInstance(loc);
132 public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
133 return getConnectionInstance(mn);
136 public OvsdbClient getClient(Node node) {
137 return getConnectionInstance(node);