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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
24 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
25 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener> {
38 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
40 private final DataBroker broker;
41 private final ManagedNewTransactionRunner txRunner;
42 private final ElanClusterUtils elanClusterUtils;
45 public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
46 super(ElanInstance.class, ElanInstanceListener.class);
48 this.txRunner = new ManagedNewTransactionRunnerImpl(db);
49 this.elanClusterUtils = elanClusterUtils;
54 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
58 protected void remove(final InstanceIdentifier<ElanInstance> identifier,
59 final ElanInstance del) {
60 elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
62 LOG.info("Elan instance {} deleted from Configuration tree ", del);
63 List<L2gatewayConnection> connections =
64 L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
65 this.broker, del.getElanInstanceName());
66 if (connections.isEmpty()) {
67 return Collections.emptyList();
69 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
71 for (L2gatewayConnection connection : connections) {
72 InstanceIdentifier<L2gatewayConnection> iid =
73 InstanceIdentifier.create(Neutron.class).child(
74 L2gatewayConnections.class).child(
75 L2gatewayConnection.class, connection.key());
79 LoggingFutures.addErrorLogging(future, LOG,
80 "Failed to delete associate L2 gateway connection while deleting network");
81 return Collections.singletonList(future);
86 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
91 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
95 protected ElanInstanceListener getDataTreeChangeListener() {
96 return ElanInstanceListener.this;
100 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
101 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);