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.binding.api.MountPointService;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.network.topology.topology.topology.types.VbridgeTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
29 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Class responsible for monitoring /network-topology/topology and activating a {@link BridgeDomain} when a particular
35 * topology is marked as a bridge domain.
37 final class TopologyMonitor implements DataTreeChangeListener<VbridgeTopology>, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(TopologyMonitor.class);
41 private final Map<TopologyKey, BridgeDomain> domains = new HashMap<>();
42 private final DataBroker dataBroker;
43 private final MountPointService mountService;
45 public TopologyMonitor(DataBroker dataBroker, MountPointService mountService) {
46 this.dataBroker = Preconditions.checkNotNull(dataBroker);
47 this.mountService = Preconditions.checkNotNull(mountService);
51 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<VbridgeTopology>> changes) {
52 for (DataTreeModification<VbridgeTopology> c : changes) {
53 @SuppressWarnings("unchecked")
54 final KeyedInstanceIdentifier<Topology, TopologyKey> topology =
55 (KeyedInstanceIdentifier<Topology, TopologyKey>) c.getRootPath().getRootIdentifier()
56 .firstIdentifierOf(Topology.class);
58 Preconditions.checkArgument(!topology.isWildcarded(), "Wildcard topology %s is not supported", topology);
60 final DataObjectModification<VbridgeTopology> mod = c.getRootNode();
61 switch (mod.getModificationType()) {
63 LOG.debug("Topology {} removed", topology);
67 LOG.debug("Topology {} added", topology);
68 startDomain(topology);
71 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
77 private synchronized void completeDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
78 LOG.debug("Bridge domain for {} completed operation", topology);
79 domains.remove(topology);
81 synchronized (domains) {
86 private synchronized void restartDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
87 final BridgeDomain prev = domains.remove(topology);
89 LOG.warn("No domain for {}, not restarting", topology);
94 startDomain(topology);
98 private void startDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
99 final BridgeDomain prev = domains.get(topology.getKey());
101 LOG.warn("Bridge domain {} for {} already started", prev, topology);
105 LOG.debug("Starting bridge domain for {}", topology);
107 final BindingTransactionChain chain = dataBroker.createTransactionChain(new TransactionChainListener() {
109 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
110 completeDomain(topology);
114 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
115 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
116 LOG.warn("Bridge domain for {} failed, restarting it", cause);
117 restartDomain(topology);
121 final BridgeDomain domain = BridgeDomain.create(dataBroker, mountService, topology, chain);
122 domains.put(topology.getKey(), domain);
124 LOG.debug("Bridge domain {} for {} started", domain, topology);
128 private void stopDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
129 final BridgeDomain domain = domains.remove(topology.getKey());
130 if (domain == null) {
131 LOG.warn("Bridge domain for {} not present", topology);
139 public synchronized void close() {
140 LOG.debug("Topology monitor {} shut down started", this);
142 for (Entry<TopologyKey, BridgeDomain> e : domains.entrySet()) {
143 LOG.debug("Shutting down bridge domain {} (key {})", e.getValue(), e.getKey());
147 while (!domains.isEmpty()) {
148 LOG.debug("Waiting for domains for {} to complete", domains.keySet());
149 synchronized (domains) {
152 } catch (InterruptedException e) {
153 LOG.warn("Interrupted while waiting for domain shutdown, {} have not completed yet",
154 domains.keySet(), e);
160 LOG.debug("Topology monitor {} shut down completed", this);