<commit_before>/*
 * Copyright (c) 2016 <NAME>, Co. and others. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.netvirt.neutronvpn;

import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.List;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortAddedToSubnetBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
    private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
    private final DataBroker dataBroker;
    private NotificationPublishService notificationPublishService;

    public SubnetmapChangeListener(final DataBroker dataBroker, final NotificationPublishService notiPublishService) {
        super(Subnetmap.class, SubnetmapChangeListener.class);
        this.dataBroker = dataBroker;
        this.notificationPublishService = notiPublishService;
    }

    public void start() {
        LOG.info("{} start", getClass().getSimpleName());
        registerListener(dataBroker);
    }

    @Override
    protected InstanceIdentifier<Subnetmap> getWildCardPath() {
        return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
    }

    // TODO Clean up the exception handling
    @SuppressWarnings("checkstyle:IllegalCatch")
    private void registerListener(final DataBroker db) {
        try {
            registerListener(LogicalDatastoreType.CONFIGURATION, db);
        } catch (final Exception e) {
            LOG.error("NeutronVpn subnetMap config DataChange listener registration fail!", e);
            throw new IllegalStateException("NeutronVpn subnetMap config DataChange listener registration failed.", e);
        }
    }

    @Override
    protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
        LOG.trace("SubnetmapChangeListener add subnetmap method - key: " + identifier + ", value=" + subnetmap);
    }

    @Override
    protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
        LOG.trace("SubnetmapChangeListener remove subnetmap method - key: " + identifier + ", value" + subnetmap);
    }

    @Override
    // TODO Clean up the exception handling
    @SuppressWarnings("checkstyle:IllegalCatch")
    protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
            subnetmapUpdate) {
        LOG.trace("SubnetmapListener update subnetmap method - key: {}, original: {}, update: {}",
                    identifier, subnetmapOriginal, subnetmapUpdate);
        Uuid vpnIdNew = subnetmapUpdate.getVpnId();
        Uuid vpnIdOld = subnetmapOriginal.getVpnId();
        Uuid subnetId = subnetmapUpdate.getId();
        String elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
        String subnetIp = subnetmapUpdate.getSubnetIp();
        Long elanTag = getElanTag(elanInstanceName);
        if (elanTag.equals(0L)) {
            LOG.debug("Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
                + "subnetmapListener update for subnet {}", elanInstanceName, subnetId);
            return;
        }
        // subnet added to VPN case
        if (vpnIdNew != null && vpnIdOld == null) {
            boolean isExternalVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
            try {
                checkAndPublishSubnetAddedToVpnNotification(subnetId, subnetIp, vpnIdNew.getValue(),
                        isExternalVpn, elanTag);
                LOG.debug("Subnet added to VPN notification sent for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdNew.getValue());
            } catch (Exception e) {
                LOG.error("Subnet added to VPN notification failed for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdNew.getValue(), e);
            }
            return;
        }
        // subnet removed from VPN case
        if (vpnIdOld != null && vpnIdNew == null) {
            Boolean isExternalVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
            try {
                checkAndPublishSubnetDeletedFromVpnNotification(subnetId, subnetIp,
                        vpnIdOld.getValue(), isExternalVpn, elanTag);
                LOG.debug("Subnet removed from VPN notification sent for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdOld.getValue());
            } catch (Exception e) {
                LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdOld.getValue(), e);
            }
            return;
        }
        // subnet updated in VPN case
        if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {<commit_msg>BUG7748: Subnet-op-data empty after cluster reboot<commit_after>/*
 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.netvirt.neutronvpn;

import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.List;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortAddedToSubnetBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
    private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
    private final DataBroker dataBroker;
    private NotificationPublishService notificationPublishService;

    public SubnetmapChangeListener(final DataBroker dataBroker, final NotificationPublishService notiPublishService) {
        super(Subnetmap.class, SubnetmapChangeListener.class);
        this.dataBroker = dataBroker;
        this.notificationPublishService = notiPublishService;
    }

    public void start() {
        LOG.info("{} start", getClass().getSimpleName());
        registerListener(dataBroker);
    }

    @Override
    protected InstanceIdentifier<Subnetmap> getWildCardPath() {
        return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
    }

    // TODO Clean up the exception handling
    @SuppressWarnings("checkstyle:IllegalCatch")
    private void registerListener(final DataBroker db) {
        try {
            registerListener(LogicalDatastoreType.CONFIGURATION, db);
        } catch (final Exception e) {
            LOG.error("NeutronVpn subnetMap config DataChange listener registration fail!", e);
            throw new IllegalStateException("NeutronVpn subnetMap config DataChange listener registration failed.", e);
        }
    }

    @Override
    protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
        LOG.trace("add:SubnetmapChangeListener add subnetmap method - key: " + identifier + ", value=" + subnetmap);
        Uuid subnetId = subnetmap.getId();
        Uuid vpnId = subnetmap.getVpnId();
        if (subnetmap.getVpnId() != null) {
            //The isExternalVpn here represents if its a BGPVPN.
            boolean isExternalVpn = !vpnId.equals(subnetmap.getRouterId());
            String elanInstanceName = subnetmap.getNetworkId().getValue();
            Long elanTag = getElanTag(elanInstanceName);
            if (elanTag.equals(0L)) {
                LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
                        + "subnetmapListener add for subnet {}", elanInstanceName, subnetId.getValue());
                return;
            }
            try {
                // subnet added to VPN case upon config DS replay after reboot
                // ports added to subnet upon config DS replay after reboot are handled implicitly by the above
                // notification in SubnetRouteHandler
                checkAndPublishSubnetAddedToVpnNotification(subnetId, subnetmap.getSubnetIp(),
                        vpnId.getValue(), isExternalVpn, elanTag);
                LOG.debug("add:Subnet added to VPN notification sent for subnet {} on VPN {}", subnetId
                                .getValue(), vpnId.getValue());
            } catch (InterruptedException e) {
                LOG.error("add:Subnet added to VPN notification failed for subnet {} on VPN {}", subnetId
                                .getValue(), vpnId.getValue(), e);
            }
            return;
        }
    }

    @Override
    protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
        LOG.trace("remove:SubnetmapChangeListener remove subnetmap method - key: " + identifier + ", value"
                + subnetmap);
    }

    @Override
    // TODO Clean up the exception handling
    @SuppressWarnings("checkstyle:IllegalCatch")
    protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
            subnetmapUpdate) {
        LOG.trace("update:SubnetmapListener update subnetmap method - key: {}, original: {}, update: {}",
                    identifier, subnetmapOriginal, subnetmapUpdate);
        Uuid vpnIdNew = subnetmapUpdate.getVpnId();
        Uuid vpnIdOld = subnetmapOriginal.getVpnId();
        Uuid subnetId = subnetmapUpdate.getId();
        String elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
        String subnetIp = subnetmapUpdate.getSubnetIp();
        Long elanTag = getElanTag(elanInstanceName);
        if (elanTag.equals(0L)) {
            LOG.error("update:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
                + "subnetmapListener update for subnet {}", elanInstanceName, subnetId.getValue());
            return;
        }
        // subnet added to VPN case
        if (vpnIdNew != null && vpnIdOld == null) {
            boolean isExternalVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
            try {
                checkAndPublishSubnetAddedToVpnNotification(subnetId, subnetIp, vpnIdNew.getValue(),
                        isExternalVpn, elanTag);
                LOG.debug("update:Subnet added to VPN notification sent for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdNew.getValue());
            } catch (Exception e) {
                LOG.error("update:Subnet added to VPN notification failed for subnet {} on VPN {}", subnetId.getValue(),
                        vpnIdNew.getValue(), e);
            }
            return;
        }
        // subnet removed from VPN case
        if (vpnIdOld != null && vpnIdNew == null) {
            Boolean isExternalVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
            try {
                checkAndPublishSubnetDeletedFromVpnNotification(subnetId, subnetIp,
                        vpnIdOld.getValue(), isExternalVpn, elanTag);
                LOG.debug("update:Subnet removed from VPN notification sent for subnet {} on VPN {}",
                            subnetId.getValue(), vpnIdOld.getValue());
            } catch (Exception e) {
                LOG.error("update:Subnet removed from VPN notification failed for subnet {} on VPN {}",
                            subnetId.getValue(), vpnIdOld.getValue(), e);
            }
            return;
        }
        // subnet updated in VPN case
        if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
{'fraction_non_alphanumeric': 0.09508041627246926, 'fraction_numerical': 0.01250168941748885, 'mean_word_length': 3.1074104912572857, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7327599', 'n_tokens_mistral': 4472, 'n_tokens_neox': 4272, 'n_words': 857}