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.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
11 import static org.opendaylight.netvirt.elan.utils.ElanConstants.ELAN_EOS_DELAY;
13 import com.google.common.base.Optional;
15 import java.util.List;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
24 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
25 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
26 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
27 import org.opendaylight.netvirt.elan.internal.ElanDpnInterfaceClusteredListener;
28 import org.opendaylight.netvirt.elan.utils.Scheduler;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
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().forEach(elanDpnInterfacesList -> {
92 List<DpnInterfaces> dpnInterfaces = elanDpnInterfacesList.getDpnInterfaces();
93 InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
94 .builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class,
95 new ElanDpnInterfacesListKey(elanDpnInterfacesList
96 .getElanInstanceName())).build();
97 for (DpnInterfaces dpnInterface : dpnInterfaces) {
98 LOG.debug("Found elan dpn interfaces");
99 elanDpnInterfaceClusteredListener.add(parentIid
100 .child(DpnInterfaces.class, dpnInterface.key()),
106 LOG.info("Not the owner for Elan entity {}", ownershipChange);
109 } catch (Exception e) {
110 LOG.error("Failed to read mdsal ", e);
112 }, ELAN_EOS_DELAY, TimeUnit.MINUTES);