HONEYCOMB-62: Add Ip readers
[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 io.fd.honeycomb.vbd.api.VxlanTunnelIdAllocator;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.Map;
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;
35
36 /**
37  * Class responsible for monitoring /network-topology/topology and activating a {@link BridgeDomain} when a particular
38  * topology is marked as a bridge domain.
39  */
40 final class TopologyMonitor implements DataTreeChangeListener<VbridgeTopology>, AutoCloseable {
41     private static final Logger LOG = LoggerFactory.getLogger(TopologyMonitor.class);
42
43     @GuardedBy("this")
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();
48
49     public TopologyMonitor(DataBroker dataBroker, MountPointService mountService) {
50         this.dataBroker = Preconditions.checkNotNull(dataBroker);
51         this.mountService = Preconditions.checkNotNull(mountService);
52
53     }
54
55     @Override
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);
62
63             Preconditions.checkArgument(!topology.isWildcarded(), "Wildcard topology %s is not supported", topology);
64
65             final DataObjectModification<VbridgeTopology> mod = c.getRootNode();
66             switch (mod.getModificationType()) {
67                 case DELETE:
68                     LOG.debug("Topology {} removed", topology);
69                     stopDomain(topology);
70                     break;
71                 case WRITE:
72                     LOG.debug("Topology {} added", topology);
73                     startDomain(topology);
74                     break;
75                 default:
76                     LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
77                     break;
78             }
79         }
80     }
81
82     private synchronized void completeDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
83         LOG.debug("Bridge domain for {} completed operation", topology);
84         domains.remove(topology);
85
86         synchronized (domains) {
87             domains.notify();
88         }
89     }
90
91     private synchronized void restartDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
92         final BridgeDomain prev = domains.remove(topology);
93         if (prev == null) {
94             LOG.warn("No domain for {}, not restarting", topology);
95             return;
96         }
97
98         prev.forceStop();
99         startDomain(topology);
100     }
101
102     @GuardedBy("this")
103     private void startDomain(final KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
104         final BridgeDomain prev = domains.get(topology.getKey());
105         if (prev != null) {
106             LOG.warn("Bridge domain {} for {} already started", prev, topology);
107             return;
108         }
109
110         LOG.debug("Starting bridge domain for {}", topology);
111
112         final BindingTransactionChain chain = dataBroker.createTransactionChain(new TransactionChainListener() {
113             @Override
114             public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
115                 completeDomain(topology);
116             }
117
118             @Override
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);
123             }
124         });
125
126         final BridgeDomain domain = BridgeDomain.create(dataBroker, mountService, topology, chain, tunnelIdAllocator);
127         domains.put(topology.getKey(), domain);
128
129         LOG.debug("Bridge domain {} for {} started", domain, topology);
130     }
131
132     @GuardedBy("this")
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);
137             return;
138         }
139
140         domain.stop();
141     }
142
143     @Override
144     public synchronized void close() {
145         LOG.debug("Topology monitor {} shut down started", this);
146
147         for (Entry<TopologyKey, BridgeDomain> e : domains.entrySet()) {
148             LOG.debug("Shutting down bridge domain {} (key {})", e.getValue(), e.getKey());
149             e.getValue().stop();
150         }
151
152         while (!domains.isEmpty()) {
153             LOG.debug("Waiting for domains for {} to complete", domains.keySet());
154             synchronized (domains) {
155                 try {
156                     domains.wait();
157                 } catch (InterruptedException e) {
158                     LOG.warn("Interrupted while waiting for domain shutdown, {} have not completed yet",
159                         domains.keySet(), e);
160                     break;
161                 }
162             }
163         }
164
165         LOG.debug("Topology monitor {} shut down completed", this);
166     }
167
168     public static class VxlanTunnelIdAllocatorImpl implements VxlanTunnelIdAllocator {
169
170         private final Map<KeyedInstanceIdentifier<Node, NodeKey>, Integer> vppIIToNextTunnelId;
171
172         VxlanTunnelIdAllocatorImpl() {
173             vppIIToNextTunnelId = new HashMap<>();
174         }
175
176         @Override
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);
181                 return value + 1;
182             } else {
183                 vppIIToNextTunnelId.put(iiToVPP, 0);
184                 return 0;
185             }
186         }
187
188     }
189 }