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 io.fd.honeycomb.vbd.api.VxlanTunnelIdAllocator;
13 import java.util.Collection;
14 import java.util.HashMap;
16 import java.util.Map.Entry;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.network.topology.topology.topology.types.VbridgeTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class responsible for monitoring /network-topology/topology and activating a {@link BridgeDomain} when a particular
38 * topology is marked as a bridge domain.
40 final class TopologyMonitor implements DataTreeChangeListener<VbridgeTopology>, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(TopologyMonitor.class);
44 private final Map<TopologyKey, BridgeDomain> domains = new HashMap<>();
45 private final DataBroker dataBroker;
46 private final MountPointService mountService;
47 private static final VxlanTunnelIdAllocator tunnelIdAllocator = new VxlanTunnelIdAllocatorImpl();
49 public TopologyMonitor(DataBroker dataBroker, MountPointService mountService) {
50 this.dataBroker = Preconditions.checkNotNull(dataBroker);
51 this.mountService = Preconditions.checkNotNull(mountService);
56 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<VbridgeTopology>> changes) {
57 for (DataTreeModification<VbridgeTopology> c : changes) {
58 @SuppressWarnings("unchecked")
59 final KeyedInstanceIdentifier<Topology, TopologyKey> topology =
60 (KeyedInstanceIdentifier<Topology, TopologyKey>) c.getRootPath().getRootIdentifier()
61 .firstIdentifierOf(Topology.class);
63 Preconditions.checkArgument(!topology.isWildcarded(), "Wildcard topology %s is not supported", topology);
65 final DataObjectModification<VbridgeTopology> mod = c.getRootNode();
66 switch (mod.getModificationType()) {
68 LOG.debug("Topology {} removed", topology);
72 LOG.debug("Topology {} added", topology);
73 startDomain(topology);
76 LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
82 private synchronized void completeDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
83 LOG.debug("Bridge domain for {} completed operation", topology);
84 domains.remove(topology);
86 synchronized (domains) {
91 private synchronized void restartDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
92 final BridgeDomain prev = domains.remove(topology);
94 LOG.warn("No domain for {}, not restarting", topology);
99 startDomain(topology);
103 private void startDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
104 final BridgeDomain prev = domains.get(topology.getKey());
106 LOG.warn("Bridge domain {} for {} already started", prev, topology);
110 LOG.debug("Starting bridge domain for {}", topology);
112 final BindingTransactionChain chain = dataBroker.createTransactionChain(new TransactionChainListener() {
114 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
115 completeDomain(topology);
119 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
120 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
121 LOG.warn("Bridge domain for {} failed, restarting it", cause);
122 restartDomain(topology);
126 final BridgeDomain domain = BridgeDomain.create(dataBroker, mountService, topology, chain, tunnelIdAllocator);
127 domains.put(topology.getKey(), domain);
129 LOG.debug("Bridge domain {} for {} started", domain, topology);
133 private void stopDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
134 final BridgeDomain domain = domains.remove(topology.getKey());
135 if (domain == null) {
136 LOG.warn("Bridge domain for {} not present", topology);
144 public synchronized void close() {
145 LOG.debug("Topology monitor {} shut down started", this);
147 for (Entry<TopologyKey, BridgeDomain> e : domains.entrySet()) {
148 LOG.debug("Shutting down bridge domain {} (key {})", e.getValue(), e.getKey());
152 while (!domains.isEmpty()) {
153 LOG.debug("Waiting for domains for {} to complete", domains.keySet());
154 synchronized (domains) {
157 } catch (InterruptedException e) {
158 LOG.warn("Interrupted while waiting for domain shutdown, {} have not completed yet",
159 domains.keySet(), e);
165 LOG.debug("Topology monitor {} shut down completed", this);
168 public static class VxlanTunnelIdAllocatorImpl implements VxlanTunnelIdAllocator {
170 private final Map<KeyedInstanceIdentifier<Node, NodeKey>, Integer> vppIIToNextTunnelId;
172 VxlanTunnelIdAllocatorImpl() {
173 vppIIToNextTunnelId = new HashMap<>();
177 public synchronized Integer nextIdFor(final KeyedInstanceIdentifier<Node, NodeKey> iiToVPP) {
178 if(vppIIToNextTunnelId.containsKey(iiToVPP)) {
179 final int value = vppIIToNextTunnelId.get(iiToVPP);
180 vppIIToNextTunnelId.put(iiToVPP, value + 1);
183 vppIIToNextTunnelId.put(iiToVPP, 0);