2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collections;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
21 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
22 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
23 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener> {
36 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
38 private final DataBroker broker;
39 private final ManagedNewTransactionRunner txRunner;
40 private final ElanClusterUtils elanClusterUtils;
43 public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
44 super(ElanInstance.class, ElanInstanceListener.class);
46 this.txRunner = new ManagedNewTransactionRunnerImpl(db);
47 this.elanClusterUtils = elanClusterUtils;
52 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
56 protected void remove(final InstanceIdentifier<ElanInstance> identifier,
57 final ElanInstance del) {
58 elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
60 LOG.info("Elan instance {} deleted from Configuration tree ", del);
61 List<L2gatewayConnection> connections =
62 L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
63 this.broker, del.getElanInstanceName());
64 if (connections.isEmpty()) {
65 return Collections.emptyList();
67 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
68 for (L2gatewayConnection connection : connections) {
69 InstanceIdentifier<L2gatewayConnection> iid =
70 InstanceIdentifier.create(Neutron.class).child(
71 L2gatewayConnections.class).child(
72 L2gatewayConnection.class, connection.key());
73 tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
76 ListenableFutures.addErrorLogging(future, LOG,
77 "Failed to delete associate L2 gateway connection while deleting network");
78 return Collections.singletonList(future);
83 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
88 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
92 protected ElanInstanceListener getDataTreeChangeListener() {
93 return ElanInstanceListener.this;
97 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
98 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);