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