Freeze upstream versions
[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.mdsal.binding.util.Datastore.CONFIGURATION;
11 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
12 import static org.opendaylight.netvirt.elan.utils.ElanConstants.ELAN_EOS_DELAY;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
26 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
28 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
29 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
30 import org.opendaylight.netvirt.elan.internal.ElanDpnInterfaceClusteredListener;
31 import org.opendaylight.netvirt.elan.utils.Scheduler;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListener {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceEntityOwnershipListener.class);
44
45     private final L2GatewayConnectionListener l2GatewayConnectionListener;
46     private final ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
47     private final Scheduler scheduler;
48     private final DataBroker dataBroker;
49     volatile ScheduledFuture<?> ft;
50     private final ManagedNewTransactionRunner txRunner;
51
52     @Inject
53     public ElanInstanceEntityOwnershipListener(L2GatewayConnectionListener l2GatewayConnectionListener,
54                                         ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener,
55                                         Scheduler scheduler, DataBroker dataBroker,
56                                         EntityOwnershipService entityOwnershipService) {
57         this.l2GatewayConnectionListener = l2GatewayConnectionListener;
58         this.elanDpnInterfaceClusteredListener = elanDpnInterfaceClusteredListener;
59         this.scheduler = scheduler;
60         this.dataBroker = dataBroker;
61         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
62         entityOwnershipService.registerListener(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, this);
63         ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
64     }
65
66     @SuppressWarnings("checkstyle:IllegalCatch")
67     @Override
68     public void ownershipChanged(EntityOwnershipChange ownershipChange) {
69         LOG.info("Entity Ownership changed for the entity: {}" , ownershipChange);
70         if (!ownershipChange.getState().isOwner()) {
71             if (ft != null) {
72                 ft.cancel(false);
73                 ft = null;
74             }
75             return;
76         }
77
78         if (!ownershipChange.getState().wasOwner() && ownershipChange.getState().isOwner()) {
79             if (ft != null) {
80                 ft.cancel(false);
81                 ft = null;
82             }
83             ft = scheduler.getScheduledExecutorService().schedule(() -> {
84                 try {
85                     //check if i'm the owner
86                     if (ownershipChange.getState().isOwner()) {
87                         LOG.info("Elan Entity owner is: {}", ownershipChange);
88
89                         txRunner.callWithNewReadOnlyTransactionAndClose(CONFIGURATION, tx -> {
90                             l2GatewayConnectionListener.loadL2GwConnectionCache(tx);
91                         });
92
93                         InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfacesInstanceIdentifier = InstanceIdentifier
94                                 .builder(ElanDpnInterfaces.class).build();
95
96                         txRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> {
97                             Optional<ElanDpnInterfaces> optional = Optional.empty();
98                             try {
99                                 optional = tx.read(elanDpnInterfacesInstanceIdentifier).get();
100                             } catch (ExecutionException | InterruptedException e) {
101                                 LOG.error("Exception While reading ElanDpnInterfaces", e);
102                             }
103                             if (optional.isPresent()
104                                 && optional.get().getElanDpnInterfacesList() != null) {
105                                 LOG.debug("Found elan dpn interfaces list");
106                                 optional.get().nonnullElanDpnInterfacesList().values()
107                                     .forEach(elanDpnInterfacesList -> {
108                                         List<DpnInterfaces> dpnInterfaces = new ArrayList<>(
109                                             elanDpnInterfacesList.nonnullDpnInterfaces().values());
110                                         InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
111                                             .builder(ElanDpnInterfaces.class)
112                                             .child(ElanDpnInterfacesList.class,
113                                                 new ElanDpnInterfacesListKey(
114                                                     elanDpnInterfacesList
115                                                         .getElanInstanceName())).build();
116                                         for (DpnInterfaces dpnInterface : dpnInterfaces) {
117                                             LOG.debug("Found elan dpn interfaces");
118                                             elanDpnInterfaceClusteredListener.add(parentIid
119                                                     .child(DpnInterfaces.class, dpnInterface.key()),
120                                                 dpnInterface);
121                                         }
122                                     });
123                             }
124                         });
125
126                     } else {
127                         LOG.info("Not the owner for Elan entity {}", ownershipChange);
128                     }
129                     ft = null;
130                 } catch (Exception e) {
131                     LOG.error("Failed to read mdsal ", e);
132                 }
133             }, ELAN_EOS_DELAY, TimeUnit.MINUTES);
134         }
135     }
136 }