NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / L2GatewayConnectionListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.elan.l2gw.listeners;
9
10 import static java.util.stream.Collectors.groupingBy;
11 import static java.util.stream.Collectors.toList;
12 import static java.util.stream.Collectors.toMap;
13 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
14
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import java.util.function.BiPredicate;
26 import java.util.function.Function;
27 import java.util.function.Predicate;
28 import javax.annotation.PreDestroy;
29 import javax.inject.Inject;
30 import javax.inject.Singleton;
31 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
33 import org.opendaylight.infrautils.metrics.Counter;
34 import org.opendaylight.infrautils.metrics.Labeled;
35 import org.opendaylight.infrautils.metrics.MetricDescriptor;
36 import org.opendaylight.infrautils.metrics.MetricProvider;
37 import org.opendaylight.infrautils.utils.concurrent.Executors;
38 import org.opendaylight.mdsal.binding.api.DataBroker;
39 import org.opendaylight.mdsal.binding.api.DataObjectModification;
40 import org.opendaylight.mdsal.binding.api.ReadTransaction;
41 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
42 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
43 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayConnectionInstanceRecoveryHandler;
44 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
45 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
46 import org.opendaylight.netvirt.elan.utils.Scheduler;
47 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
48 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
49 import org.opendaylight.serviceutils.srm.RecoverableListener;
50 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
51 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 @Singleton
66 public class L2GatewayConnectionListener extends AbstractClusteredAsyncDataTreeChangeListener<L2gatewayConnection>
67         implements RecoverableListener {
68     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionListener.class);
69     private static final int MAX_READ_TRIALS = 120;
70
71     private static final Function<Node, InstanceIdentifier<Node>> TO_GLOBAL_PATH =
72             HwvtepHAUtil::getGlobalNodePathFromPSNode;
73
74     private static final Function<Node, InstanceIdentifier<Node>> TO_NODE_PATH =
75         (node) -> HwvtepSouthboundUtils.createInstanceIdentifier(node.getNodeId());
76
77     private static final Function<InstanceIdentifier<Node>, String> GET_DEVICE_NAME = HwvtepHAUtil::getPsName;
78
79     private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE = (psIid) ->
80             HwvtepHAUtil.getPsName(psIid) != null;
81
82     private static final Predicate<Node> IS_HA_PARENT_NODE = (node) -> {
83         HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
84         if (augmentation != null && augmentation.getManagers() != null) {
85             return augmentation.getManagers().stream().anyMatch(
86                 manager -> manager.key().getTarget().getValue().equals(HwvtepHAUtil.MANAGER_KEY));
87         }
88         return false;
89     };
90
91     private static final BiPredicate<InstanceIdentifier<Node>, Node> PS_NODE_OF_PARENT_NODE =
92         (psIid, node) -> psIid.firstKeyOf(Node.class).getNodeId().getValue().contains(node.getNodeId().getValue());
93
94     private final DataBroker broker;
95     private final L2GatewayConnectionUtils l2GatewayConnectionUtils;
96     private final Scheduler scheduler;
97     private final L2GatewayCache l2GatewayCache;
98     private final Labeled<Labeled<Counter>> elanConnectionsCounter;
99
100     @Inject
101     public L2GatewayConnectionListener(final DataBroker db, L2GatewayConnectionUtils l2GatewayConnectionUtils,
102                                        Scheduler scheduler, L2GatewayCache l2GatewayCache,
103                                        MetricProvider metricProvider,
104                                        final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
105                                        final L2GatewayConnectionInstanceRecoveryHandler l2InstanceRecoveryHandler,
106                                        final ServiceRecoveryRegistry serviceRecoveryRegistry) {
107         super(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
108                 .child(L2gatewayConnections.class).child(L2gatewayConnection.class),
109                 Executors.newListeningSingleThreadExecutor("L2GatewayConnectionListener", LOG));
110         this.broker = db;
111         this.l2GatewayConnectionUtils = l2GatewayConnectionUtils;
112         this.scheduler = scheduler;
113         this.l2GatewayCache = l2GatewayCache;
114         this.elanConnectionsCounter = metricProvider.newCounter(MetricDescriptor.builder()
115                 .anchor(this).project("netvirt").module("l2gw").id("connections").build(), "modification", "elan");
116         serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(),
117                 this);
118         serviceRecoveryRegistry.addRecoverableListener(l2InstanceRecoveryHandler.buildServiceRegistryKey(),
119                 this);
120         init();
121     }
122
123     public void init() {
124         loadL2GwDeviceCache(1);
125         LOG.trace("Loading l2gw connection cache");
126         loadL2GwConnectionCache();
127     }
128
129     @Override
130     @PreDestroy
131     public void close() {
132         super.close();
133         Executors.shutdownAndAwaitTermination(getExecutorService());
134     }
135
136     @Override
137     public void registerListener() {
138         super.register();
139         LOG.info("Registering L2GatewayConnectionListener");
140     }
141
142     public void deregisterListener() {
143         super.close();
144         LOG.info("Deregistering L2GatewayConnectionListener");
145     }
146
147     @Override
148     public void add(final InstanceIdentifier<L2gatewayConnection> identifier, final L2gatewayConnection input) {
149         LOG.trace("Adding L2gatewayConnection: {}", input);
150         elanConnectionsCounter
151                 .label(DataObjectModification.ModificationType.WRITE.name())
152                 .label(input.getNetworkId().getValue()).increment();
153         // Get associated L2GwId from 'input'
154         // Create logical switch in each of the L2GwDevices part of L2Gw
155         // Logical switch name is network UUID
156         // Add L2GwDevices to ELAN
157         l2GatewayConnectionUtils.addL2GatewayConnection(input);
158     }
159
160     @Override
161     public void remove(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection input) {
162         LOG.trace("Removing L2gatewayConnection: {}", input);
163         elanConnectionsCounter
164                 .label(DataObjectModification.ModificationType.DELETE.name())
165                 .label(input.getNetworkId().getValue()).increment();
166         l2GatewayConnectionUtils.deleteL2GatewayConnection(input);
167     }
168
169     @Override
170     public void update(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection original,
171             L2gatewayConnection update) {
172         LOG.trace("Updating L2gatewayConnection : original value={}, updated value={}", original, update);
173     }
174
175     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
176             justification = "https://github.com/spotbugs/spotbugs/issues/811")
177     private void loadL2GwDeviceCache(final int trialNo) {
178         scheduler.getScheduledExecutorService().schedule(() -> {
179             if (trialNo == MAX_READ_TRIALS) {
180                 LOG.error("Failed to read config topology");
181                 return;
182             }
183             ReadTransaction tx = broker.newReadOnlyTransaction();
184             InstanceIdentifier<Topology> topoIid = HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier();
185             Futures.addCallback(tx.read(CONFIGURATION, topoIid), new FutureCallback<Optional<Topology>>() {
186                 @Override
187                 public void onSuccess(Optional<Topology> topologyOptional) {
188                     if (topologyOptional != null && topologyOptional.isPresent()) {
189                         loadL2GwDeviceCache(topologyOptional.get().getNode());
190                     }
191                     registerListener();
192                 }
193
194                 @Override
195                 public void onFailure(Throwable throwable) {
196                     loadL2GwDeviceCache(trialNo + 1);
197                 }
198             }, MoreExecutors.directExecutor());
199             tx.close();
200         }, 1, TimeUnit.SECONDS);
201     }
202
203     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
204             justification = "https://github.com/spotbugs/spotbugs/issues/811")
205     private void loadL2GwDeviceCache(List<Node> nodes) {
206         if (nodes == null) {
207             LOG.debug("No config topology nodes are present");
208             return;
209         }
210         Map<InstanceIdentifier<Node>, Node> allNodes = nodes
211                 .stream()
212                 .collect(toMap(TO_NODE_PATH, Function.identity()));
213
214         LOG.trace("Loading all config nodes");
215
216         Set<InstanceIdentifier<Node>> allIids = allNodes.keySet();
217
218         Map<String, List<InstanceIdentifier<Node>>> psNodesByDeviceName = allIids
219                 .stream()
220                 .filter(IS_PS_NODE)
221                 .collect(groupingBy(GET_DEVICE_NAME, toList()));
222
223         //Process HA nodes
224         allNodes.values().stream()
225                 .filter(IS_HA_PARENT_NODE)
226                 .forEach(parentNode -> allIids.stream()
227                         .filter(IS_PS_NODE)
228                         .filter(psIid -> PS_NODE_OF_PARENT_NODE.test(psIid, parentNode))
229                         .forEach(psIid -> addL2DeviceToCache(psIid, parentNode, allNodes.get(psIid))));
230
231         //Process non HA nodes there will be only one ps node iid for each device for non ha nodes
232         psNodesByDeviceName.values().stream()
233                 .filter(psIids -> psIids.size() == 1)
234                 .map(psIids -> psIids.get(0))
235                 .forEach(psIid -> {
236                     Node psNode = allNodes.get(psIid);
237                     Node globalNode = allNodes.get(TO_GLOBAL_PATH.apply(psNode));
238                     if (globalNode != null) {
239                         addL2DeviceToCache(psIid, globalNode, psNode);
240                     }
241                 });
242     }
243
244     public void loadL2GwConnectionCache() {
245         InstanceIdentifier<L2gatewayConnections> parentIid = InstanceIdentifier
246                 .create(Neutron.class)
247                 .child(L2gatewayConnections.class);
248
249         Optional<L2gatewayConnections> optional = Optional.empty();
250         try {
251             optional = SingleTransactionDataBroker.syncReadOptional(broker, CONFIGURATION,
252                     parentIid);
253         } catch (ExecutionException | InterruptedException e) {
254             LOG.error("loadL2GwConnectionCache: Exception while reading L2gatewayConnections DS", e);
255         }
256         if (optional.isPresent() && optional.get().getL2gatewayConnection() != null) {
257             LOG.trace("Found some connections to fill in l2gw connection cache");
258             optional.get().getL2gatewayConnection()
259                     .forEach(connection -> {
260                         add(parentIid.child(L2gatewayConnection.class, connection.key()), connection);
261                     });
262         }
263     }
264
265     void addL2DeviceToCache(InstanceIdentifier<Node> psIid, Node globalNode, Node psNode) {
266         LOG.trace("Adding device to cache {}", psNode.getNodeId().getValue());
267         String deviceName = HwvtepHAUtil.getPsName(psIid);
268         L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(deviceName);
269         l2GwDevice.setConnected(true);
270         l2GwDevice.setHwvtepNodeId(globalNode.getNodeId().getValue());
271
272         List<TunnelIps> tunnelIps = psNode.augmentation(PhysicalSwitchAugmentation.class) != null
273                 ? psNode.augmentation(PhysicalSwitchAugmentation.class).getTunnelIps() : null;
274         if (tunnelIps != null) {
275             for (TunnelIps tunnelIp : tunnelIps) {
276                 IpAddress tunnelIpAddr = tunnelIp.getTunnelIpsKey();
277                 l2GwDevice.addTunnelIp(tunnelIpAddr);
278             }
279         }
280     }
281 }