Fixup Augmentable and Identifiable methods changing
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanInstanceListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.l2gw.listeners;
9
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;
32
33 @Singleton
34 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
37
38     private final DataBroker broker;
39     private final ManagedNewTransactionRunner txRunner;
40     private final ElanClusterUtils elanClusterUtils;
41
42     @Inject
43     public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
44         super(ElanInstance.class, ElanInstanceListener.class);
45         broker = db;
46         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
47         this.elanClusterUtils = elanClusterUtils;
48     }
49
50     @PostConstruct
51     public void init() {
52         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
53     }
54
55     @Override
56     protected void remove(final InstanceIdentifier<ElanInstance> identifier,
57                           final ElanInstance del) {
58         elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
59             () -> {
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();
66                 }
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);
74                     }
75                 });
76                 ListenableFutures.addErrorLogging(future, LOG,
77                         "Failed to delete associate L2 gateway connection while deleting network");
78                 return Collections.singletonList(future);
79             });
80     }
81
82     @Override
83     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
84
85     }
86
87     @Override
88     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
89     }
90
91     @Override
92     protected ElanInstanceListener getDataTreeChangeListener() {
93         return ElanInstanceListener.this;
94     }
95
96     @Override
97     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
98         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
99     }
100
101 }