auto-config-transport-zones has incorrect default
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / VpnDpnToTransportZoneListener.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 java.util.stream.Collectors;
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.netvirt.elan.utils.TransportZoneNotificationUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceOpData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Singleton
27 public class VpnDpnToTransportZoneListener
28         extends AsyncDataTreeChangeListenerBase<VpnToDpnList, VpnDpnToTransportZoneListener> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(VpnDpnToTransportZoneListener.class);
31     private final TransportZoneNotificationUtil transportZoneNotificationUtil;
32     private final DataBroker dbx;
33     private final Boolean useTransportZone;
34
35     @Inject
36     public VpnDpnToTransportZoneListener(final DataBroker dbx,
37             final ElanConfig elanConfig, final TransportZoneNotificationUtil tznu) {
38         useTransportZone = elanConfig.isAutoConfigTransportZones();
39         transportZoneNotificationUtil = tznu;
40         this.dbx = dbx;
41     }
42
43     @PostConstruct
44     public void start() {
45
46         if (useTransportZone) {
47             registerListener(LogicalDatastoreType.OPERATIONAL, dbx);
48             LOG.info("{} registered", getClass().getSimpleName());
49         }
50     }
51
52     @Override
53     protected InstanceIdentifier<VpnToDpnList> getWildCardPath() {
54         return InstanceIdentifier.builder(VpnInstanceOpData.class).child(VpnInstanceOpDataEntry.class)
55                 .child(VpnToDpnList.class).build();
56     }
57
58     @Override
59     protected void remove(InstanceIdentifier<VpnToDpnList> identifier, VpnToDpnList del) {
60         LOG.debug("Vpn dpn {} remove detected, SHOULD BE deleting transport zones", del.getDpnId());
61     }
62
63     @Override
64     protected void update(InstanceIdentifier<VpnToDpnList> identifier, VpnToDpnList original, VpnToDpnList update) {
65         LOG.debug("Vpn dpn {} update detected, updating transport zones", update.getDpnId());
66
67         if (update.getVpnInterfaces() == null || update.getVpnInterfaces().isEmpty()) {
68             LOG.debug("Vpn dpn {} doesn't contain any vpn interfaces", update.getDpnId());
69             return;
70         }
71
72         boolean shouldCreateVtep;
73         if (original.getVpnInterfaces() != null && !original.getVpnInterfaces().isEmpty()) {
74             shouldCreateVtep = transportZoneNotificationUtil.shouldCreateVtep(update.getVpnInterfaces().stream()
75                     .filter(vi -> !original.getVpnInterfaces().contains(vi)).collect(Collectors.toList()));
76         } else {
77             shouldCreateVtep = transportZoneNotificationUtil.shouldCreateVtep(update.getVpnInterfaces());
78         }
79
80         if (shouldCreateVtep) {
81             String vrfId = identifier.firstKeyOf(VpnInstanceOpDataEntry.class).getVrfId();
82             transportZoneNotificationUtil.updateTransportZone(vrfId, update.getDpnId());
83         }
84     }
85
86     @Override
87     protected void add(InstanceIdentifier<VpnToDpnList> identifier, VpnToDpnList add) {
88         LOG.debug("Vpn dpn {} add detected, updating transport zones", add.getDpnId());
89
90         boolean shouldCreateVtep = transportZoneNotificationUtil.shouldCreateVtep(add.getVpnInterfaces());
91         if (shouldCreateVtep) {
92             String vrfId = identifier.firstKeyOf(VpnInstanceOpDataEntry.class).getVrfId();
93             transportZoneNotificationUtil.updateTransportZone(vrfId, add.getDpnId());
94         }
95     }
96
97     @Override
98     protected VpnDpnToTransportZoneListener getDataTreeChangeListener() {
99         return VpnDpnToTransportZoneListener.this;
100     }
101 }