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