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