b2e52c37e95c284046ad028805aa6e671069c94e
[honeycomb.git] / vbd / impl / src / main / java / io / fd / honeycomb / vbd / impl / TopologyMonitor.java
1 /*
2  * Copyright (c) 2016 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
9 package io.fd.honeycomb.vbd.impl;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
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;
31
32 /**
33  * Class responsible for monitoring /network-topology/topology and activating a {@link BridgeDomain} when a particular
34  * topology is marked as a bridge domain.
35  */
36 final class TopologyMonitor implements DataTreeChangeListener<VbridgeTopology>, AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(TopologyMonitor.class);
38
39     @GuardedBy("this")
40     private final Map<TopologyKey, BridgeDomain> domains = new HashMap<>();
41     private final DataBroker dataBroker;
42
43     TopologyMonitor(final DataBroker dataBroker) {
44         this.dataBroker = Preconditions.checkNotNull(dataBroker);
45     }
46
47     @Override
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);
54
55             Preconditions.checkArgument(!topology.isWildcarded(), "Wildcard topology %s is not supported", topology);
56
57             final DataObjectModification<VbridgeTopology> mod = c.getRootNode();
58             switch (mod.getModificationType()) {
59                 case DELETE:
60                     LOG.debug("Topology {} removed", topology);
61                     stopDomain(topology);
62                     break;
63                 case WRITE:
64                     LOG.debug("Topology {} added", topology);
65                     startDomain(topology);
66                     break;
67                 default:
68                     LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
69                     break;
70             }
71         }
72     }
73
74     private synchronized void completeDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
75         LOG.debug("Bridge domain for {} completed operation", topology);
76         domains.remove(topology);
77
78         synchronized (domains) {
79             domains.notify();
80         }
81     }
82
83     private synchronized void restartDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
84         final BridgeDomain prev = domains.remove(topology);
85         if (prev == null) {
86             LOG.warn("No domain for {}, not restarting", topology);
87             return;
88         }
89
90         prev.forceStop();
91         startDomain(topology);
92     }
93
94     @GuardedBy("this")
95     private void startDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
96         final BridgeDomain prev = domains.get(topology.getKey());
97         if (prev != null) {
98             LOG.warn("Bridge domain {} for {} already started", prev, topology);
99             return;
100         }
101
102         LOG.debug("Starting bridge domain for {}", topology);
103
104         final BindingTransactionChain chain = dataBroker.createTransactionChain(new TransactionChainListener() {
105             @Override
106             public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
107                 completeDomain(topology);
108             }
109
110             @Override
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);
115             }
116         });
117
118         final BridgeDomain domain = BridgeDomain.create(dataBroker, topology, chain);
119         domains.put(topology.getKey(), domain);
120
121         LOG.debug("Bridge domain {} for {} started", domain, topology);
122     }
123
124     @GuardedBy("this")
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);
129             return;
130         }
131
132         domain.stop();
133     }
134
135     @Override
136     public synchronized void close() {
137         LOG.debug("Topology monitor {} shut down started", this);
138
139         for (Entry<TopologyKey, BridgeDomain> e : domains.entrySet()) {
140             LOG.debug("Shutting down bridge domain {} (key {})", e.getValue(), e.getKey());
141             e.getValue().stop();
142         }
143
144         while (!domains.isEmpty()) {
145             LOG.debug("Waiting for domains for {} to complete", domains.keySet());
146             synchronized (domains) {
147                 try {
148                     domains.wait();
149                 } catch (InterruptedException e) {
150                     LOG.warn("Interrupted while waiting for domain shutdown, {} have not completed yet",
151                         domains.keySet(), e);
152                     break;
153                 }
154             }
155         }
156
157         LOG.debug("Topology monitor {} shut down completed", this);
158     }
159 }