NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanDpnToTransportZoneListener.java
1 /*
2  * Copyright (c) 2017, 2019 HPE 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 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;
28
29 @Singleton
30 public class ElanDpnToTransportZoneListener
31         extends AbstractAsyncDataTreeChangeListener<DpnInterfaces> {
32
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;
38
39     @Inject
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;
48         this.dbx = dbx;
49         this.elanInstanceCache = elanInstanceCache;
50         start();
51     }
52
53     public void start() {
54         if (useTransportZone) {
55             LOG.info("{} registered", getClass().getSimpleName());
56         }
57     }
58
59     @Override
60     public void remove(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
61         //Proceed only if "auto-config-transport-zones = TRUE"
62         if (!useTransportZone) {
63             return;
64         }
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();
68
69         if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orElse(null))) {
70             LOG.debug("ElanInstance {} is not vxlan network, nothing to do", elanInstanceName);
71             return;
72         }
73         LOG.debug("Deleting tz for elanInstance {} dpId {}", elanInstanceName, dpId);
74         transportZoneNotificationUtil.deleteTransportZone(elanInstanceName, dpId);
75
76     }
77
78     @Override
79     public void update(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModificationBefore,
80             DpnInterfaces dataObjectModificationAfter) {
81     }
82
83     @Override
84     public void add(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
85         //Proceed only if "auto-config-transport-zones = TRUE"
86         if (!useTransportZone) {
87             return;
88         }
89         LOG.debug("Elan dpn {} add detected, updating transport zones", dataObjectModification.getDpId());
90
91         Uint64 dpId = dataObjectModification.getDpId();
92         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
93
94         if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orElse(null))) {
95             return;
96         }
97
98         transportZoneNotificationUtil.updateTransportZone(elanInstanceName, dpId);
99     }
100
101     @Override
102     @PreDestroy
103     public void close() {
104         super.close();
105         Executors.shutdownAndAwaitTermination(getExecutorService());
106     }
107 }