Merge "Initial write for bridge."
[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.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;
33
34 import com.google.common.base.Optional;
35 import com.google.common.base.Preconditions;
36 import com.google.common.util.concurrent.CheckedFuture;
37
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);
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         OvsdbClientKey key = new OvsdbClientKey(externalClient);
55         OvsdbConnectionInstance client = new OvsdbConnectionInstance(key,externalClient,txInvoker);
56         clients.put(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         OvsdbClientKey key = new OvsdbClientKey(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 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
74         return client;
75     }
76
77     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
78         OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
79         OvsdbClient client = clients.get(key);
80         if (client != null) {
81             client.disconnect();
82         }
83     }
84
85     @Override
86     public void close() throws Exception {
87         for(OvsdbClient client: clients.values()) {
88             client.disconnect();
89         }
90     }
91
92     public OvsdbConnectionInstance getConnectionInstance(OvsdbClientKey key) {
93         return clients.get(key);
94     }
95
96     public OvsdbConnectionInstance getConnectionInstance(IpPortLocator loc) {
97         Preconditions.checkNotNull(loc);
98         return getConnectionInstance(new OvsdbClientKey(loc));
99     }
100
101     public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
102         Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
103         if(optional.isPresent()) {
104             return getConnectionInstance(optional.get());
105         } else {
106             return null;
107         }
108     }
109
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);
118         } else {
119             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
120             return null;
121         }
122     }
123
124     public OvsdbClient getClient(OvsdbClientKey key) {
125         return getConnectionInstance(key);
126     }
127
128     public OvsdbClient getClient(IpPortLocator loc) {
129         return getConnectionInstance(loc);
130     }
131
132     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
133         return getConnectionInstance(mn);
134     }
135
136     public OvsdbClient getClient(Node node) {
137         return getConnectionInstance(node);
138     }
139 }