498acc848b9c40ec9bd4c61498bacf67010d525f
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanDpnToTransportZoneListener.java
1 /*
2  * Copyright (c) 2017 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 java.math.BigInteger;
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.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
18 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
19 import org.opendaylight.netvirt.elan.utils.ElanUtils;
20 import org.opendaylight.netvirt.elan.utils.TransportZoneNotificationUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Singleton
30 public class ElanDpnToTransportZoneListener
31         extends AsyncDataTreeChangeListenerBase<DpnInterfaces, ElanDpnToTransportZoneListener> {
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, final IInterfaceManager interfaceManager,
41             final ElanConfig elanConfig, final TransportZoneNotificationUtil tznu,
42             final ElanInstanceCache elanInstanceCache) {
43         useTransportZone = elanConfig.isAutoConfigTransportZones();
44         transportZoneNotificationUtil = tznu;
45         this.dbx = dbx;
46         this.elanInstanceCache = elanInstanceCache;
47     }
48
49     @PostConstruct
50     public void start() {
51         LOG.info("{} start", getClass().getSimpleName());
52
53         if (useTransportZone) {
54             registerListener(LogicalDatastoreType.OPERATIONAL, dbx);
55         }
56     }
57
58     @Override
59     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
60         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
61                 .child(DpnInterfaces.class).build();
62     }
63
64     @Override
65     protected void remove(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
66         LOG.debug("Elan dpn {} delete detected, deleting transport zones", dataObjectModification.getDpId());
67         BigInteger dpId = dataObjectModification.getDpId();
68         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
69
70         if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orNull())) {
71             LOG.debug("ElanInstance {} is not vxlan network, nothing to do", elanInstanceName);
72             return;
73         }
74         LOG.debug("Deleting tz for elanInstance {} dpId {}", elanInstanceName, dpId);
75         transportZoneNotificationUtil.deleteTransportZone(elanInstanceName, dpId);
76
77     }
78
79     @Override
80     protected void update(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModificationBefore,
81             DpnInterfaces dataObjectModificationAfter) {
82     }
83
84     @Override
85     protected void add(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
86         LOG.debug("Elan dpn {} add detected, updating transport zones", dataObjectModification.getDpId());
87
88         BigInteger dpId = dataObjectModification.getDpId();
89         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
90
91         if (!ElanUtils.isVxlanNetworkOrVxlanSegment(elanInstanceCache.get(elanInstanceName).orNull())) {
92             return;
93         }
94
95         transportZoneNotificationUtil.updateTransportZone(elanInstanceName, dpId);
96     }
97
98     @Override
99     protected ElanDpnToTransportZoneListener getDataTreeChangeListener() {
100         return ElanDpnToTransportZoneListener.this;
101     }
102 }