2 * Copyright (c) 2015 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
9 package org.opendaylight.ovsdb.southbound;
11 import java.net.UnknownHostException;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.ovsdb.lib.OvsdbClient;
24 import org.opendaylight.ovsdb.southbound.ovsdb.transact.BridgeOperationalState;
25 import org.opendaylight.ovsdb.southbound.ovsdb.transact.DataChangesManagedByOvsdbNodeEvent;
26 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactCommandAggregator;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Preconditions;
41 import com.google.common.base.Predicates;
42 import com.google.common.collect.Maps;
44 public class OvsdbDataChangeListener implements DataChangeListener, AutoCloseable {
46 private ListenerRegistration<DataChangeListener> registration;
47 private OvsdbConnectionManager cm;
48 private DataBroker db;
49 private static final Logger LOG = LoggerFactory.getLogger(OvsdbDataChangeListener.class);
51 OvsdbDataChangeListener(DataBroker db, OvsdbConnectionManager cm) {
52 LOG.info("Registering OvsdbNodeDataChangeListener");
55 InstanceIdentifier<Node> path = InstanceIdentifier
56 .create(NetworkTopology.class)
57 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
60 db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, DataChangeScope.SUBTREE);
65 public void close() throws Exception {
70 public void onDataChanged(
71 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
72 LOG.trace("onDataChanged: {}", changes);
73 // Connect first if we have to:
76 // Second update connections if we have to
77 updateConnections(changes);
79 // Then handle updates to the actual data
82 // Finally disconnect if we need to
87 LOG.trace("onDataChanged: exit");
90 private void updateData(
91 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
92 for (Entry<InstanceIdentifier<Node>, OvsdbConnectionInstance> connectionInstanceEntry :
93 connectionInstancesFromChanges(changes).entrySet()) {
94 OvsdbConnectionInstance connectionInstance = connectionInstanceEntry.getValue();
95 connectionInstance.transact(new TransactCommandAggregator(
96 new BridgeOperationalState(db, changes),
97 new DataChangesManagedByOvsdbNodeEvent(
98 connectionInstance.getInstanceIdentifier(),
103 private void disconnect(
104 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
105 Map<InstanceIdentifier<?>, DataObject> originalDataObject = changes.getOriginalData();
106 Set<InstanceIdentifier<?>> iiD = changes.getRemovedPaths();
107 for (InstanceIdentifier instanceIdentifier : iiD) {
108 if (originalDataObject.get(instanceIdentifier) instanceof OvsdbNodeAugmentation) {
110 cm.disconnect((OvsdbNodeAugmentation) originalDataObject.get(instanceIdentifier));
111 } catch (UnknownHostException e) {
112 LOG.warn("Failed to disconnect ovsdbNode", e);
118 private void updateConnections(
119 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
120 for (Entry<InstanceIdentifier<?>, DataObject> updated : changes.getUpdatedData().entrySet()) {
121 if (updated.getValue() instanceof OvsdbNodeAugmentation) {
122 OvsdbNodeAugmentation value = (OvsdbNodeAugmentation) updated.getValue();
123 OvsdbClient client = cm.getClient(value.getConnectionInfo());
124 if (client == null) {
125 for (Entry<InstanceIdentifier<?>, DataObject> original : changes.getOriginalData().entrySet()) {
126 if (original.getValue() instanceof OvsdbNodeAugmentation) {
128 cm.disconnect((OvsdbNodeAugmentation) original.getValue());
129 cm.connect((InstanceIdentifier<Node>) original.getKey(),value);
130 } catch (UnknownHostException e) {
131 LOG.warn("Failed to disconnect to ovsdbNode", e);
140 private void connect(
141 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
142 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.getCreatedData().entrySet()) {
143 // TODO validate we have the correct kind of InstanceIdentifier
144 if (created.getValue() instanceof OvsdbNodeAugmentation) {
146 cm.connect((InstanceIdentifier<Node>) created.getKey(),
147 (OvsdbNodeAugmentation) created.getValue());
148 } catch (UnknownHostException e) {
149 LOG.warn("Failed to connect to ovsdbNode", e);
156 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
157 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.getCreatedData().entrySet()) {
158 if (created.getValue() instanceof OvsdbNodeAugmentation) {
159 OvsdbNodeAugmentation ovsdbNode = (OvsdbNodeAugmentation)created.getValue();
160 cm.init(ovsdbNode.getConnectionInfo());
166 public Map<InstanceIdentifier<Node>,OvsdbConnectionInstance> connectionInstancesFromChanges(
167 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
168 Map<InstanceIdentifier<Node>,OvsdbConnectionInstance> result =
169 new HashMap<InstanceIdentifier<Node>,OvsdbConnectionInstance>();
170 result.putAll(connectionInstancesFromMap(changes.getCreatedData()));
171 result.putAll(connectionInstancesFromMap(changes.getUpdatedData()));
172 result.putAll(connectionInstancesFromMap(
173 Maps.filterKeys(changes.getOriginalData(), Predicates.in(changes.getRemovedPaths()))));
177 public Map<InstanceIdentifier<Node>,OvsdbConnectionInstance> connectionInstancesFromMap(Map<InstanceIdentifier<?>,
179 Preconditions.checkNotNull(map);
180 Map<InstanceIdentifier<Node>,OvsdbConnectionInstance> result =
181 new HashMap<InstanceIdentifier<Node>,OvsdbConnectionInstance>();
182 for ( Entry<InstanceIdentifier<?>, DataObject> created : map.entrySet()) {
183 if (created.getValue() instanceof Node) {
184 OvsdbConnectionInstance client = null;
185 LOG.debug("Received request for {}",created.getValue());
186 OvsdbBridgeAugmentation bridge =
187 ((Node)created.getValue()).getAugmentation(OvsdbBridgeAugmentation.class);
188 if (bridge != null) {
189 client = cm.getConnectionInstance(bridge);
191 OvsdbNodeAugmentation ovsNode =
192 ((Node)created.getValue()).getAugmentation(OvsdbNodeAugmentation.class);
193 if (ovsNode != null && ovsNode.getConnectionInfo() != null) {
194 client = cm.getConnectionInstance(ovsNode.getConnectionInfo());
196 List<TerminationPoint> terminationPoint = ((Node)created.getValue()).getTerminationPoint();
197 if (!terminationPoint.isEmpty()) {
198 InstanceIdentifier<Node> nodeIid = SouthboundMapper.
199 createInstanceIdentifier(((Node)created.getValue()).getNodeId());
200 client = cm.getConnectionInstance(nodeIid);
204 if (client != null) {
205 LOG.debug("Found client for {}", created.getValue());
206 result.put((InstanceIdentifier<Node>) created.getKey(), client);
208 LOG.debug("Did not find client for {}",created.getValue());