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.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.ovsdb.lib.OvsdbClient;
21 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
22 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.IpPortLocator;
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.OvsdbManagedNodeAugmentation;
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.OvsdbNodeRef;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.Preconditions;
33 import com.google.common.util.concurrent.CheckedFuture;
35 public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoCloseable {
36 Map<OvsdbClientKey,OvsdbConnectionInstance> clients = new ConcurrentHashMap<OvsdbClientKey,OvsdbConnectionInstance>();
37 private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionManager.class);
41 public OvsdbConnectionManager(DataBroker db) {
46 public void connected(OvsdbClient externalClient) {
47 LOG.info("OVSDB Connection from {}:{}",externalClient.getConnectionInfo().getRemoteAddress(),
48 externalClient.getConnectionInfo().getRemotePort());
49 OvsdbClientKey key = new OvsdbClientKey(externalClient);
50 OvsdbConnectionInstance client = new OvsdbConnectionInstance(key,externalClient);
51 clients.put(key, client);
52 WriteTransaction transaction = db.newWriteOnlyTransaction();
53 transaction.put(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier(),
54 SouthboundMapper.createNode(client));
55 // TODO - Check the future and retry if needed
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 WriteTransaction transaction = db.newWriteOnlyTransaction();
65 transaction.delete(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier());
66 // TODO - Check the future and retry if needed
71 public OvsdbClient connect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
72 // TODO handle case where we already have a connection
73 // TODO use transaction chains to handle ordering issues between disconnected and connected when writing to the operational store
74 InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getIp());
75 OvsdbClient client = OvsdbConnectionService.getService().connect(ip, ovsdbNode.getPort().getValue().intValue());
76 OvsdbClientKey key = new OvsdbClientKey(client);
77 OvsdbConnectionInstance instance = new OvsdbConnectionInstance(key,client);
78 clients.put(key, instance);
79 connected(client); // For connections from the controller to the ovs instance, the library doesn't call this method for us
83 public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
84 OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
85 OvsdbClient client = clients.get(key);
93 public void close() throws Exception {
94 for(OvsdbClient client: clients.values()) {
99 public OvsdbClient getClient(OvsdbClientKey key) {
100 return clients.get(key);
103 public OvsdbClient getClient(IpPortLocator loc) {
104 Preconditions.checkNotNull(loc);
105 return getClient(new OvsdbClientKey(loc));
108 public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
109 Preconditions.checkNotNull(mn);
111 OvsdbNodeRef ref = mn.getManagedBy();
113 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
114 CheckedFuture<?, ReadFailedException> nf = transaction.read(LogicalDatastoreType.OPERATIONAL, ref.getValue());
116 Object obj = nf.get();
117 if(obj instanceof Node) {
118 OvsdbNodeAugmentation ovsdbNode = ((Node)obj).getAugmentation(OvsdbNodeAugmentation.class);
119 if(ovsdbNode !=null) {
120 return getClient(ovsdbNode);
122 LOG.warn("OvsdbManagedNode {} claims to be managed by {} but that OvsdbNode does not exist",mn,ref.getValue());
126 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}",obj);
130 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}",mn);
133 } catch (Exception e) {
134 LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}",mn, e);
139 public OvsdbClient getClient(Node node) {
140 Preconditions.checkNotNull(node);
141 OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
142 OvsdbManagedNodeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbManagedNodeAugmentation.class);
143 if(ovsdbNode != null) {
144 return getClient(ovsdbNode);
145 } else if (ovsdbManagedNode != null) {
146 return getClient(ovsdbManagedNode);
148 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);