8180007c777e07fa5a1412bd726451093c7ec99c
[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.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;
31
32 import com.google.common.base.Preconditions;
33 import com.google.common.util.concurrent.CheckedFuture;
34
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);
38
39     DataBroker db;
40
41     public OvsdbConnectionManager(DataBroker db) {
42         this.db = db;
43     }
44
45     @Override
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
56         transaction.submit();
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         WriteTransaction transaction = db.newWriteOnlyTransaction();
65         transaction.delete(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier());
66         // TODO - Check the future and retry if needed
67         transaction.submit();
68         clients.remove(key);
69     }
70
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
80         return client;
81     }
82
83     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
84         OvsdbClientKey key = new OvsdbClientKey(ovsdbNode.getIp(), ovsdbNode.getPort());
85         OvsdbClient client = clients.get(key);
86         if (client != null) {
87             client.disconnect();
88             disconnected(client);
89         }
90     }
91
92     @Override
93     public void close() throws Exception {
94         for(OvsdbClient client: clients.values()) {
95             client.disconnect();
96         }
97     }
98
99     public OvsdbClient getClient(OvsdbClientKey key) {
100         return clients.get(key);
101     }
102
103     public OvsdbClient getClient(IpPortLocator loc) {
104         Preconditions.checkNotNull(loc);
105         return getClient(new OvsdbClientKey(loc));
106     }
107
108     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
109         Preconditions.checkNotNull(mn);
110         try {
111             OvsdbNodeRef ref = mn.getManagedBy();
112             if(ref != null) {
113                 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
114                 CheckedFuture<?, ReadFailedException> nf = transaction.read(LogicalDatastoreType.OPERATIONAL, ref.getValue());
115                 transaction.close();
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);
121                     } else {
122                         LOG.warn("OvsdbManagedNode {} claims to be managed by {} but that OvsdbNode does not exist",mn,ref.getValue());
123                         return null;
124                     }
125                 } else {
126                     LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}",obj);
127                     return null;
128                 }
129             } else {
130                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}",mn);
131                 return null;
132             }
133          } catch (Exception e) {
134              LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}",mn, e);
135              return null;
136          }
137     }
138
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);
147         } else {
148             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
149             return null;
150         }
151     }
152 }