Remove redundant names in paths
[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.utils.ElanUtils;
19 import org.opendaylight.netvirt.elan.utils.TransportZoneNotificationUtil;
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.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
37     @Inject
38     public ElanDpnToTransportZoneListener(final DataBroker dbx, final IInterfaceManager interfaceManager,
39             final ElanConfig elanConfig, final TransportZoneNotificationUtil tznu) {
40         useTransportZone = elanConfig.isAutoConfigTransportZones();
41         transportZoneNotificationUtil = tznu;
42         this.dbx = dbx;
43     }
44
45     @PostConstruct
46     public void start() {
47         LOG.info("{} start", getClass().getSimpleName());
48
49         if (useTransportZone) {
50             registerListener(LogicalDatastoreType.OPERATIONAL, dbx);
51         }
52     }
53
54     @Override
55     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
56         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
57                 .child(DpnInterfaces.class).build();
58     }
59
60     @Override
61     protected void remove(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
62         LOG.debug("Elan dpn {} delete detected, deleting transport zones", dataObjectModification.getDpId());
63         BigInteger dpId = dataObjectModification.getDpId();
64         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
65
66         if (!ElanUtils.isVxlanNetwork(dbx, elanInstanceName)) {
67             LOG.debug("ElanInstance {} is not vxlan network, nothing to do", elanInstanceName);
68             return;
69         }
70         LOG.debug("Deleting tz for elanInstance {} dpId {}", elanInstanceName, dpId);
71         transportZoneNotificationUtil.deleteTransportZone(elanInstanceName, dpId);
72
73     }
74
75     @Override
76     protected void update(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModificationBefore,
77             DpnInterfaces dataObjectModificationAfter) {
78     }
79
80     @Override
81     protected void add(InstanceIdentifier<DpnInterfaces> key, DpnInterfaces dataObjectModification) {
82         LOG.debug("Elan dpn {} add detected, updating transport zones", dataObjectModification.getDpId());
83
84         BigInteger dpId = dataObjectModification.getDpId();
85         String elanInstanceName = key.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
86
87         if (!ElanUtils.isVxlanNetwork(dbx, elanInstanceName)) {
88             return;
89         }
90
91         transportZoneNotificationUtil.updateTransportZone(elanInstanceName, dpId);
92     }
93
94     @Override
95     protected ElanDpnToTransportZoneListener getDataTreeChangeListener() {
96         return ElanDpnToTransportZoneListener.this;
97     }
98 }