Handle nullable lists in elanmanager
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanDpnInterfaceClusteredListener.java
1 /*
2  * Copyright (c) 2016, 2017 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.internal;
9
10 import static java.util.Collections.emptyList;
11 import static org.opendaylight.netvirt.elan.utils.ElanUtils.requireNonNullElse;
12
13 import java.util.List;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
20 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
22 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
23 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
24 import org.opendaylight.netvirt.elan.cache.ElanInstanceDpnsCache;
25 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
26 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
27 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
28 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
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.elan.dpn.interfaces.list.DpnInterfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class ElanDpnInterfaceClusteredListener
39         extends AsyncClusteredDataTreeChangeListenerBase<DpnInterfaces, ElanDpnInterfaceClusteredListener> {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
42
43     private final DataBroker broker;
44     private final EntityOwnershipUtils entityOwnershipUtils;
45     private final ElanL2GatewayUtils elanL2GatewayUtils;
46     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
47     private final ElanClusterUtils elanClusterUtils;
48     private final JobCoordinator jobCoordinator;
49     private final ElanInstanceCache elanInstanceCache;
50     private final ElanInstanceDpnsCache elanInstanceDpnsCache;
51
52     @Inject
53     public ElanDpnInterfaceClusteredListener(DataBroker broker, EntityOwnershipUtils entityOwnershipUtils,
54                                              ElanL2GatewayUtils elanL2GatewayUtils,
55                                              ElanClusterUtils elanClusterUtils, JobCoordinator jobCoordinator,
56                                              ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
57                                              ElanInstanceCache elanInstanceCache,
58                                              ElanInstanceDpnsCache elanInstanceDpnsCache) {
59         this.broker = broker;
60         this.entityOwnershipUtils = entityOwnershipUtils;
61         this.elanL2GatewayUtils = elanL2GatewayUtils;
62         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
63         this.elanClusterUtils = elanClusterUtils;
64         this.jobCoordinator = jobCoordinator;
65         this.elanInstanceCache = elanInstanceCache;
66         this.elanInstanceDpnsCache = elanInstanceDpnsCache;
67     }
68
69     @PostConstruct
70     public void init() {
71         registerListener(LogicalDatastoreType.OPERATIONAL, this.broker);
72     }
73
74     @Override
75     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
76         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
77                 .child(DpnInterfaces.class).build();
78     }
79
80     void handleUpdate(InstanceIdentifier<DpnInterfaces> id, DpnInterfaces dpnInterfaces) {
81         final String elanName = getElanName(id);
82         if (ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).isEmpty()) {
83             LOG.debug("dpnInterface updation, no external l2 devices to update for elan {} with Dp Id {}", elanName,
84                     dpnInterfaces.getDpId());
85             return;
86         }
87         elanClusterUtils.runOnlyInOwnerNode(elanName, "updating mcast mac upon tunnel event",
88             () -> {
89                 elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName);
90                 return emptyList();
91             });
92     }
93
94     @Override
95     protected void remove(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
96         // this is the last dpn interface on this elan
97         final String elanName = getElanName(identifier);
98         //Cache need to be updated in all cluster nodes and not only by leader node .
99         //Hence moved out from DJC job
100
101         jobCoordinator.enqueueJob(elanName + ":l2gw", () -> {
102             try {
103                 if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
104                         HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
105                     // deleting Elan L2Gw Devices UcastLocalMacs From Dpn
106                     elanL2GatewayUtils.deleteElanL2GwDevicesUcastLocalMacsFromDpn(elanName,
107                             dpnInterfaces.getDpId());
108
109                     //Removing this dpn from cache to avoid race between this and local ucast mac listener
110                     elanInstanceDpnsCache.remove(getElanName(identifier), dpnInterfaces);
111
112                     // updating remote mcast mac on l2gw devices
113                     elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName);
114                 }
115             } finally {
116                 elanInstanceDpnsCache.remove(getElanName(identifier), dpnInterfaces);
117             }
118
119             return null;
120         });
121     }
122
123     @Override
124     protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
125                           final DpnInterfaces dpnInterfaces) {
126         List<String> interfaces = requireNonNullElse(dpnInterfaces.getInterfaces(), emptyList());
127         LOG.debug("dpninterfaces update fired new size {}", interfaces.size());
128         if (interfaces.isEmpty()) {
129             elanInstanceDpnsCache.remove(getElanName(identifier), dpnInterfaces);
130             LOG.debug("dpninterfaces last dpn interface on this elan {} ", dpnInterfaces.key());
131             // this is the last dpn interface on this elan
132             handleUpdate(identifier, dpnInterfaces);
133         }
134     }
135
136     @Override
137     protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
138         final String elanName = getElanName(identifier);
139
140         jobCoordinator.enqueueJob(elanName + ":l2gw", () -> {
141             elanInstanceDpnsCache.add(getElanName(identifier), dpnInterfaces);
142             if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
143                     HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
144                 ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
145                 if (elanInstance != null) {
146                     elanL2GatewayUtils.installElanL2gwDevicesLocalMacsInDpn(
147                             dpnInterfaces.getDpId(), elanInstance, dpnInterfaces.getInterfaces().get(0));
148
149                     // updating remote mcast mac on l2gw devices
150                     elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName);
151                 }
152             }
153             return emptyList();
154         });
155     }
156
157     private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
158         return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
159     }
160
161     @Override
162     protected ElanDpnInterfaceClusteredListener getDataTreeChangeListener() {
163         return this;
164     }
165
166 }