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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.Preconditions;
34 import com.google.common.util.concurrent.CheckedFuture;
36 public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoCloseable {
37 Map<OvsdbClientKey,OvsdbConnectionInstance> clients = new ConcurrentHashMap<OvsdbClientKey,OvsdbConnectionInstance>();
38 private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionManager.class);
40 private DataBroker db;
41 private TransactionInvoker txInvoker;
43 public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker) {
45 this.txInvoker = txInvoker;
49 public void connected(final OvsdbClient externalClient) {
50 LOG.info("OVSDB Connection from {}:{}",externalClient.getConnectionInfo().getRemoteAddress(),
51 externalClient.getConnectionInfo().getRemotePort());
52 OvsdbClientKey key = new OvsdbClientKey(externalClient);
53 OvsdbConnectionInstance client = new OvsdbConnectionInstance(key,externalClient,txInvoker);
54 clients.put(key, client);
58 public void disconnected(OvsdbClient client) {
59 LOG.info("OVSDB Disconnect from {}:{}",client.getConnectionInfo().getRemoteAddress(),
60 client.getConnectionInfo().getRemotePort());
61 OvsdbClientKey key = new OvsdbClientKey(client);
62 txInvoker.invoke(new OvsdbNodeRemoveCommand(key,null,null));
66 public OvsdbClient connect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
67 // TODO handle case where we already have a connection
68 // TODO use transaction chains to handle ordering issues between disconnected and connected when writing to the operational store
69 InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getIp());
70 OvsdbClient client = OvsdbConnectionService.getService().connect(ip, ovsdbNode.getPort().getValue().intValue());
71 connected(client); // For connections from the controller to the ovs instance, the library doesn't call this method for us
75 public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
76 OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
77 OvsdbClient client = clients.get(key);
84 public void close() throws Exception {
85 for(OvsdbClient client: clients.values()) {
90 public OvsdbConnectionInstance getConnectionInstance(OvsdbClientKey key) {
91 return clients.get(key);
94 public OvsdbConnectionInstance getConnectionInstance(IpPortLocator loc) {
95 Preconditions.checkNotNull(loc);
96 return getConnectionInstance(new OvsdbClientKey(loc));
99 public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
100 Preconditions.checkNotNull(mn);
102 OvsdbNodeRef ref = mn.getManagedBy();
104 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
105 CheckedFuture<?, ReadFailedException> nf = transaction.read(LogicalDatastoreType.OPERATIONAL, ref.getValue());
107 Object obj = nf.get();
108 if(obj instanceof Node) {
109 OvsdbNodeAugmentation ovsdbNode = ((Node)obj).getAugmentation(OvsdbNodeAugmentation.class);
110 if(ovsdbNode !=null) {
111 return getConnectionInstance(ovsdbNode);
113 LOG.warn("OvsdbManagedNode {} claims to be managed by {} but that OvsdbNode does not exist",mn,ref.getValue());
117 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}",obj);
121 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}",mn);
124 } catch (Exception e) {
125 LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}",mn, e);
130 public OvsdbConnectionInstance getConnectionInstance(Node node) {
131 Preconditions.checkNotNull(node);
132 OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
133 OvsdbBridgeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbBridgeAugmentation.class);
134 if(ovsdbNode != null) {
135 return getConnectionInstance(ovsdbNode);
136 } else if (ovsdbManagedNode != null) {
137 return getConnectionInstance(ovsdbManagedNode);
139 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
144 public OvsdbClient getClient(OvsdbClientKey key) {
145 return getConnectionInstance(key);
148 public OvsdbClient getClient(IpPortLocator loc) {
149 return getConnectionInstance(loc);
152 public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
153 return getConnectionInstance(mn);
156 public OvsdbClient getClient(Node node) {
157 return getConnectionInstance(node);