2 * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
11 import static org.opendaylight.netvirt.elan.utils.ElanConstants.ELAN_EOS_DELAY;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
24 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
25 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
26 import org.opendaylight.netvirt.elan.internal.ElanDpnInterfaceClusteredListener;
27 import org.opendaylight.netvirt.elan.utils.Scheduler;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListener {
39 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceEntityOwnershipListener.class);
41 private final L2GatewayConnectionListener l2GatewayConnectionListener;
42 private final ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
43 private final Scheduler scheduler;
44 private final DataBroker dataBroker;
45 volatile ScheduledFuture<?> ft;
48 public ElanInstanceEntityOwnershipListener(L2GatewayConnectionListener l2GatewayConnectionListener,
49 ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener,
50 Scheduler scheduler, DataBroker dataBroker,
51 EntityOwnershipService entityOwnershipService) {
52 this.l2GatewayConnectionListener = l2GatewayConnectionListener;
53 this.elanDpnInterfaceClusteredListener = elanDpnInterfaceClusteredListener;
54 this.scheduler = scheduler;
55 this.dataBroker = dataBroker;
56 entityOwnershipService.registerListener(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, this);
59 @SuppressWarnings("checkstyle:IllegalCatch")
61 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
62 LOG.info("Entity Ownership changed for the entity: {}" , ownershipChange);
63 if (!ownershipChange.getState().isOwner()) {
71 if (!ownershipChange.getState().wasOwner() && ownershipChange.getState().isOwner()) {
76 ft = scheduler.getScheduledExecutorService().schedule(() -> {
78 //check if i'm the owner
79 if (ownershipChange.getState().isOwner()) {
80 LOG.info("Elan Entity owner is: {}", ownershipChange);
81 l2GatewayConnectionListener.loadL2GwConnectionCache();
83 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfacesInstanceIdentifier = InstanceIdentifier
84 .builder(ElanDpnInterfaces.class).build();
86 Optional<ElanDpnInterfaces> optional = MDSALUtil.read(dataBroker, OPERATIONAL,
87 elanDpnInterfacesInstanceIdentifier);
88 if (optional.isPresent() && optional.get().getElanDpnInterfacesList() != null) {
89 LOG.debug("Found elan dpn interfaces list");
90 optional.get().getElanDpnInterfacesList().forEach(elanDpnInterfacesList -> {
91 List<DpnInterfaces> dpnInterfaces = elanDpnInterfacesList.getDpnInterfaces();
92 InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
93 .builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class,
94 new ElanDpnInterfacesListKey(elanDpnInterfacesList
95 .getElanInstanceName())).build();
96 for (DpnInterfaces dpnInterface : dpnInterfaces) {
97 LOG.debug("Found elan dpn interfaces");
98 elanDpnInterfaceClusteredListener.add(parentIid
99 .child(DpnInterfaces.class, dpnInterface.key()),
105 LOG.info("Not the owner for Elan entity {}", ownershipChange);
108 } catch (ExecutionException | InterruptedException e) {
109 LOG.error("Failed to read mdsal ", e);
111 }, ELAN_EOS_DELAY, TimeUnit.MINUTES);