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