Merge "Fix for bridge other config in mdsal."
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionManager.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14
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;
32
33 import com.google.common.base.Optional;
34 import com.google.common.base.Preconditions;
35 import com.google.common.util.concurrent.CheckedFuture;
36
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);
41
42     private DataBroker db;
43     private TransactionInvoker txInvoker;
44
45     public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker) {
46         this.db = db;
47         this.txInvoker = txInvoker;
48     }
49
50     @Override
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);
57     }
58
59     @Override
60     public void disconnected(OvsdbClient client) {
61         LOG.info("OVSDB Disconnect from {}:{}",client.getConnectionInfo().getRemoteAddress(),
62                 client.getConnectionInfo().getRemotePort());
63         ConnectionInfo key = SouthboundMapper.createConnectionInfo(client);
64         txInvoker.invoke(new OvsdbNodeRemoveCommand(key,null,null));
65         clients.remove(key);
66     }
67
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.getConnectionInfo().getRemoteIp());
73         OvsdbClient client = OvsdbConnectionService.getService().connect(ip,
74                 ovsdbNode.getConnectionInfo().getRemotePort().getValue().intValue());
75         // For connections from the controller to the ovs instance, the library doesn't call
76         // this method for us
77         connected(client);
78         return client;
79     }
80
81     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
82         OvsdbClient client = getConnectionInstance(ovsdbNode.getConnectionInfo());
83         if (client != null) {
84             client.disconnect();
85         }
86     }
87
88     @Override
89     public void close() throws Exception {
90         for (OvsdbClient client: clients.values()) {
91             client.disconnect();
92         }
93     }
94
95     private void putConnectionInstance(ConnectionInfo key,OvsdbConnectionInstance instance) {
96         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
97         clients.put(connectionInfo, instance);
98     }
99
100     public OvsdbConnectionInstance getConnectionInstance(ConnectionInfo key) {
101         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
102         return clients.get(connectionInfo);
103     }
104
105     public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
106         Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
107         if (optional.isPresent()) {
108             return getConnectionInstance(optional.get().getConnectionInfo());
109         } else {
110             return null;
111         }
112     }
113
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.getConnectionInfo());
120         } else if (ovsdbManagedNode != null) {
121             return getConnectionInstance(ovsdbManagedNode);
122         } else {
123             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
124             return null;
125         }
126     }
127
128     public OvsdbConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
129         try {
130             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
131             CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
132                     LogicalDatastoreType.OPERATIONAL, nodePath);
133             transaction.close();
134             Optional<Node> optional = nodeFuture.get();
135             if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
136                 return this.getConnectionInstance(optional.get());
137             } else {
138                 LOG.warn("Found non-topological node {} on path {}",optional);
139                 return null;
140             }
141         } catch (Exception e) {
142             LOG.warn("Failed to get Ovsdb Node {}",nodePath, e);
143             return null;
144         }
145     }
146
147     public OvsdbClient getClient(ConnectionInfo connectionInfo) {
148         return getConnectionInstance(connectionInfo);
149     }
150
151     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
152         return getConnectionInstance(mn);
153     }
154
155     public OvsdbClient getClient(Node node) {
156         return getConnectionInstance(node);
157     }
158 }