Merge "Renaming ovsdb-managed-node-augmentation to ovsdb-bridge-augmentation."
[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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Preconditions;
34 import com.google.common.util.concurrent.CheckedFuture;
35
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);
39
40     private DataBroker db;
41     private TransactionInvoker txInvoker;
42
43     public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker) {
44         this.db = db;
45         this.txInvoker = txInvoker;
46     }
47
48     @Override
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);
55     }
56
57     @Override
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));
63         clients.remove(key);
64     }
65
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
72         return client;
73     }
74
75     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
76         OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
77         OvsdbClient client = clients.get(key);
78         if (client != null) {
79             client.disconnect();
80         }
81     }
82
83     @Override
84     public void close() throws Exception {
85         for(OvsdbClient client: clients.values()) {
86             client.disconnect();
87         }
88     }
89
90     public OvsdbConnectionInstance getConnectionInstance(OvsdbClientKey key) {
91         return clients.get(key);
92     }
93
94     public OvsdbConnectionInstance getConnectionInstance(IpPortLocator loc) {
95         Preconditions.checkNotNull(loc);
96         return getConnectionInstance(new OvsdbClientKey(loc));
97     }
98
99     public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
100         Preconditions.checkNotNull(mn);
101         try {
102             OvsdbNodeRef ref = mn.getManagedBy();
103             if(ref != null) {
104                 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
105                 CheckedFuture<?, ReadFailedException> nf = transaction.read(LogicalDatastoreType.OPERATIONAL, ref.getValue());
106                 transaction.close();
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);
112                     } else {
113                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but that OvsdbNode does not exist",mn,ref.getValue());
114                         return null;
115                     }
116                 } else {
117                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}",obj);
118                     return null;
119                 }
120             } else {
121                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}",mn);
122                 return null;
123             }
124          } catch (Exception e) {
125              LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}",mn, e);
126              return null;
127          }
128     }
129
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);
138         } else {
139             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
140             return null;
141         }
142     }
143
144     public OvsdbClient getClient(OvsdbClientKey key) {
145         return getConnectionInstance(key);
146     }
147
148     public OvsdbClient getClient(IpPortLocator loc) {
149         return getConnectionInstance(loc);
150     }
151
152     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
153         return getConnectionInstance(mn);
154     }
155
156     public OvsdbClient getClient(Node node) {
157         return getConnectionInstance(node);
158     }
159 }