b71598bf1b0e685c492becfeafca90950a5764f3
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanInstanceEntityOwnershipListener.java
1 /*
2  * Copyright (c) 2019 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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
11 import static org.opendaylight.netvirt.elan.utils.ElanConstants.ELAN_EOS_DELAY;
12
13 import com.google.common.base.Optional;
14
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;
20
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;
36
37 @Singleton
38 public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListener {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceEntityOwnershipListener.class);
41
42     private final L2GatewayConnectionListener l2GatewayConnectionListener;
43     private final ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
44     private final Scheduler scheduler;
45     private final DataBroker dataBroker;
46     volatile ScheduledFuture<?> ft;
47
48     @Inject
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);
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     @Override
62     public void ownershipChanged(EntityOwnershipChange ownershipChange) {
63         LOG.info("Entity Ownership changed for the entity: {}" , ownershipChange);
64         if (!ownershipChange.getState().isOwner()) {
65             if (ft != null) {
66                 ft.cancel(false);
67                 ft = null;
68             }
69             return;
70         }
71
72         if (!ownershipChange.getState().wasOwner() && ownershipChange.getState().isOwner()) {
73             if (ft != null) {
74                 ft.cancel(false);
75                 ft = null;
76             }
77             ft = scheduler.getScheduledExecutorService().schedule(() -> {
78                 try {
79                     //check if i'm the owner
80                     if (ownershipChange.getState().isOwner()) {
81                         LOG.info("Elan Entity owner is: {}", ownershipChange);
82                         l2GatewayConnectionListener.loadL2GwConnectionCache();
83
84                         InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfacesInstanceIdentifier = InstanceIdentifier
85                                 .builder(ElanDpnInterfaces.class).build();
86
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()),
101                                             dpnInterface);
102                                 }
103                             });
104                         }
105                     } else {
106                         LOG.info("Not the owner for Elan entity {}", ownershipChange);
107                     }
108                     ft = null;
109                 } catch (Exception e) {
110                     LOG.error("Failed to read mdsal ", e);
111                 }
112             }, ELAN_EOS_DELAY, TimeUnit.MINUTES);
113         }
114     }
115 }