2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.internal;
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;
34 public class ElanDpnInterfaceClusteredListener
35 extends AsyncClusteredDataTreeChangeListenerBase<DpnInterfaces, ElanDpnInterfaceClusteredListener> {
37 private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
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;
47 public ElanDpnInterfaceClusteredListener(DataBroker broker, EntityOwnershipUtils entityOwnershipUtils,
48 ElanL2GatewayUtils elanL2GatewayUtils,
49 ElanClusterUtils elanClusterUtils, JobCoordinator jobCoordinator,
50 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils) {
52 this.entityOwnershipUtils = entityOwnershipUtils;
53 this.elanL2GatewayUtils = elanL2GatewayUtils;
54 this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
55 this.elanClusterUtils = elanClusterUtils;
56 this.jobCoordinator = jobCoordinator;
61 registerListener(LogicalDatastoreType.OPERATIONAL, this.broker);
65 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
66 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
67 .child(DpnInterfaces.class).build();
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());
77 elanClusterUtils.runOnlyInOwnerNode(elanName, "updating mcast mac upon tunnel event",
78 () -> Collections.singletonList(
79 elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName)));
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
89 jobCoordinator.enqueueJob(elanName + ":l2gw", () -> {
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());
97 //Removing this dpn from cache to avoid race between this and local ucast mac listener
98 ElanUtils.removeDPNInterfaceFromElanInCache(getElanName(identifier), dpnInterfaces);
100 // updating remote mcast mac on l2gw devices
101 elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName);
104 ElanUtils.removeDPNInterfaceFromElanInCache(getElanName(identifier), dpnInterfaces);
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);
124 protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
125 final String elanName = getElanName(identifier);
127 jobCoordinator.enqueueJob(elanName + ":l2gw", () -> {
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));
136 // updating remote mcast mac on l2gw devices
137 elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName);
140 ElanUtils.addDPNInterfaceToElanInCache(getElanName(identifier), dpnInterfaces);
146 private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
147 return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
151 protected ElanDpnInterfaceClusteredListener getDataTreeChangeListener() {