12b456cec8dbae6611577c8e1752b09110068f6b
[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.PostConstruct;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
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.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.Uint64;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Singleton
29 public class ElanDpnToTransportZoneListener
30         extends AsyncDataTreeChangeListenerBase<DpnInterfaces, ElanDpnToTransportZoneListener> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnToTransportZoneListener.class);
33     private final TransportZoneNotificationUtil transportZoneNotificationUtil;
34     private final DataBroker dbx;
35     private final Boolean useTransportZone;
36     private final ElanInstanceCache elanInstanceCache;
37
38     @Inject
39     public ElanDpnToTransportZoneListener(final DataBroker dbx,
40             final ElanConfig elanConfig, final TransportZoneNotificationUtil tznu,
41             final ElanInstanceCache elanInstanceCache) {
42         useTransportZone = elanConfig.isAutoConfigTransportZones();
43         transportZoneNotificationUtil = tznu;
44         this.dbx = dbx;
45         this.elanInstanceCache = elanInstanceCache;
46     }
47
48     @PostConstruct
49     public void start() {
50
51         if (useTransportZone) {
52             registerListener(LogicalDatastoreType.OPERATIONAL, dbx);
53             LOG.info("{} registered", getClass().getSimpleName());
54         }
55     }
56
57     @Override
58     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
59         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
60                 .child(DpnInterfaces.class).build();
61     }
62
63     @Override
64     protected void remove(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
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).orNull())) {
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     protected void update(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModificationBefore,
80             DpnInterfaces dataObjectModificationAfter) {
81     }
82
83     @Override
84     protected void add(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
85         LOG.debug("Elan dpn {} add detected, updating transport zones", dataObjectModification.getDpId());
86
87         Uint64 dpId = dataObjectModification.getDpId();
88         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
89
90         if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orNull())) {
91             return;
92         }
93
94         transportZoneNotificationUtil.updateTransportZone(elanInstanceName, dpId);
95     }
96
97     @Override
98     protected ElanDpnToTransportZoneListener getDataTreeChangeListener() {
99         return ElanDpnToTransportZoneListener.this;
100     }
101 }