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;
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.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListener {
40 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceEntityOwnershipListener.class);
42 private final L2GatewayConnectionListener l2GatewayConnectionListener;
43 private final ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
44 private final Scheduler scheduler;
45 private final DataBroker dataBroker;
46 volatile ScheduledFuture<?> ft;
49 public ElanInstanceEntityOwnershipListener(L2GatewayConnectionListener l2GatewayConnectionListener,
50 ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener,
51 Scheduler scheduler, DataBroker dataBroker,
52 EntityOwnershipService entityOwnershipService) {
53 this.l2GatewayConnectionListener = l2GatewayConnectionListener;
54 this.elanDpnInterfaceClusteredListener = elanDpnInterfaceClusteredListener;
55 this.scheduler = scheduler;
56 this.dataBroker = dataBroker;
57 entityOwnershipService.registerListener(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, this);
60 @SuppressWarnings("checkstyle:IllegalCatch")
62 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
63 LOG.info("Entity Ownership changed for the entity: {}" , ownershipChange);
64 if (!ownershipChange.getState().isOwner()) {
72 if (!ownershipChange.getState().wasOwner() && ownershipChange.getState().isOwner()) {
77 ft = scheduler.getScheduledExecutorService().schedule(() -> {
79 //check if i'm the owner
80 if (ownershipChange.getState().isOwner()) {
81 LOG.info("Elan Entity owner is: {}", ownershipChange);
82 l2GatewayConnectionListener.loadL2GwConnectionCache();
84 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfacesInstanceIdentifier = InstanceIdentifier
85 .builder(ElanDpnInterfaces.class).build();
87 Optional<ElanDpnInterfaces> optional = MDSALUtil.read(dataBroker, OPERATIONAL,
88 elanDpnInterfacesInstanceIdentifier);
89 if (optional.isPresent() && optional.get().getElanDpnInterfacesList() != null) {
90 LOG.debug("Found elan dpn interfaces list");
91 optional.get().getElanDpnInterfacesList().values().forEach(elanDpnInterfacesList -> {
92 Map<DpnInterfacesKey, DpnInterfaces> dpnInterfaces
93 = elanDpnInterfacesList.getDpnInterfaces();
94 InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
95 .builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class,
96 new ElanDpnInterfacesListKey(elanDpnInterfacesList
97 .getElanInstanceName())).build();
98 for (DpnInterfaces dpnInterface : dpnInterfaces.values()) {
99 LOG.debug("Found elan dpn interfaces");
100 elanDpnInterfaceClusteredListener.add(parentIid
101 .child(DpnInterfaces.class, dpnInterface.key()),
107 LOG.info("Not the owner for Elan entity {}", ownershipChange);
110 } catch (ExecutionException | InterruptedException e) {
111 LOG.error("Failed to read mdsal ", e);
113 }, ELAN_EOS_DELAY, TimeUnit.MINUTES);