2 * Copyright (c) 2017, 2019 HPE 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 javax.annotation.PreDestroy;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.infrautils.utils.concurrent.Executors;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
17 import org.opendaylight.netvirt.elan.utils.ElanUtils;
18 import org.opendaylight.netvirt.elan.utils.TransportZoneNotificationUtil;
19 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.common.Uint64;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class ElanDpnToTransportZoneListener
31 extends AbstractAsyncDataTreeChangeListener<DpnInterfaces> {
33 private static final Logger LOG = LoggerFactory.getLogger(ElanDpnToTransportZoneListener.class);
34 private final TransportZoneNotificationUtil transportZoneNotificationUtil;
35 private final DataBroker dbx;
36 private final Boolean useTransportZone;
37 private final ElanInstanceCache elanInstanceCache;
40 public ElanDpnToTransportZoneListener(final DataBroker dbx,
41 final ElanConfig elanConfig, final TransportZoneNotificationUtil tznu,
42 final ElanInstanceCache elanInstanceCache) {
43 super(dbx, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ElanDpnInterfaces.class)
44 .child(ElanDpnInterfacesList.class).child(DpnInterfaces.class),
45 Executors.newListeningSingleThreadExecutor("ElanDpnToTransportZoneListener", LOG));
46 useTransportZone = elanConfig.isAutoConfigTransportZones();
47 transportZoneNotificationUtil = tznu;
49 this.elanInstanceCache = elanInstanceCache;
54 if (useTransportZone) {
55 LOG.info("{} registered", getClass().getSimpleName());
60 public void remove(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
61 //Proceed only if "auto-config-transport-zones = TRUE"
62 if (!useTransportZone) {
65 LOG.debug("Elan dpn {} delete detected, deleting transport zones", dataObjectModification.getDpId());
66 Uint64 dpId = dataObjectModification.getDpId();
67 String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
69 if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orElse(null))) {
70 LOG.debug("ElanInstance {} is not vxlan network, nothing to do", elanInstanceName);
73 LOG.debug("Deleting tz for elanInstance {} dpId {}", elanInstanceName, dpId);
74 transportZoneNotificationUtil.deleteTransportZone(elanInstanceName, dpId);
79 public void update(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModificationBefore,
80 DpnInterfaces dataObjectModificationAfter) {
84 public void add(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
85 //Proceed only if "auto-config-transport-zones = TRUE"
86 if (!useTransportZone) {
89 LOG.debug("Elan dpn {} add detected, updating transport zones", dataObjectModification.getDpId());
91 Uint64 dpId = dataObjectModification.getDpId();
92 String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
94 if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orElse(null))) {
98 transportZoneNotificationUtil.updateTransportZone(elanInstanceName, dpId);
103 public void close() {
105 Executors.shutdownAndAwaitTermination(getExecutorService());