2 * Copyright (c) 2016 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 io.fd.honeycomb.vbd.impl;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.network.topology.topology.topology.types.VbridgeTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Class responsible for monitoring /network-topology/topology and activating a {@link BridgeDomain} when a particular
34 * topology is marked as a bridge domain.
36 final class TopologyMonitor implements DataTreeChangeListener<VbridgeTopology>, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(TopologyMonitor.class);
40 private final Map<TopologyKey, BridgeDomain> domains = new HashMap<>();
41 private final DataBroker dataBroker;
43 TopologyMonitor(final DataBroker dataBroker) {
44 this.dataBroker = Preconditions.checkNotNull(dataBroker);
48 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<VbridgeTopology>> changes) {
49 for (DataTreeModification<VbridgeTopology> c : changes) {
50 @SuppressWarnings("unchecked")
51 final KeyedInstanceIdentifier<Topology, TopologyKey> topology =
52 (KeyedInstanceIdentifier<Topology, TopologyKey>) c.getRootPath().getRootIdentifier()
53 .firstIdentifierOf(Topology.class);
55 Preconditions.checkArgument(!topology.isWildcarded(), "Wildcard topology %s is not supported", topology);
57 final DataObjectModification<VbridgeTopology> mod = c.getRootNode();
58 switch (mod.getModificationType()) {
60 LOG.debug("Topology {} removed", topology);
64 LOG.debug("Topology {} added", topology);
65 startDomain(topology);
68 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
74 private synchronized void completeDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
75 LOG.debug("Bridge domain for {} completed operation", topology);
76 domains.remove(topology);
78 synchronized (domains) {
83 private synchronized void restartDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
84 final BridgeDomain prev = domains.remove(topology);
86 LOG.warn("No domain for {}, not restarting", topology);
91 startDomain(topology);
95 private void startDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
96 final BridgeDomain prev = domains.get(topology.getKey());
98 LOG.warn("Bridge domain {} for {} already started", prev, topology);
102 LOG.debug("Starting bridge domain for {}", topology);
104 final BindingTransactionChain chain = dataBroker.createTransactionChain(new TransactionChainListener() {
106 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
107 completeDomain(topology);
111 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
112 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
113 LOG.warn("Bridge domain for {} failed, restarting it", cause);
114 restartDomain(topology);
118 final BridgeDomain domain = BridgeDomain.create(dataBroker, topology, chain);
119 domains.put(topology.getKey(), domain);
121 LOG.debug("Bridge domain {} for {} started", domain, topology);
125 private void stopDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
126 final BridgeDomain domain = domains.remove(topology.getKey());
127 if (domain == null) {
128 LOG.warn("Bridge domain for {} not present", topology);
136 public synchronized void close() {
137 LOG.debug("Topology monitor {} shut down started", this);
139 for (Entry<TopologyKey, BridgeDomain> e : domains.entrySet()) {
140 LOG.debug("Shutting down bridge domain {} (key {})", e.getValue(), e.getKey());
144 while (!domains.isEmpty()) {
145 LOG.debug("Waiting for domains for {} to complete", domains.keySet());
146 synchronized (domains) {
149 } catch (InterruptedException e) {
150 LOG.warn("Interrupted while waiting for domain shutdown, {} have not completed yet",
151 domains.keySet(), e);
157 LOG.debug("Topology monitor {} shut down completed", this);