Remove redundant names in paths
[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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
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,
35         ElanInstanceListener> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
38
39     private final DataBroker broker;
40     private final ElanClusterUtils elanClusterUtils;
41     private static final Map<String, List<Runnable>> WAITING_JOB_LIST = new ConcurrentHashMap<>();
42
43     @Inject
44     public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils) {
45         super(ElanInstance.class, ElanInstanceListener.class);
46         broker = 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 == null || connections.isEmpty()) {
65                     return null;
66                 }
67                 try {
68                     ReadWriteTransaction tx = this.broker.newReadWriteTransaction();
69                     for (L2gatewayConnection connection : connections) {
70                         InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
71                             .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
72                         tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
73                     }
74                     tx.submit().checkedGet();
75                 } catch (TransactionCommitFailedException e) {
76                     LOG.error("Failed to delete associated l2gwconnection while deleting network", e);
77                 }
78                 return null;
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         List<Runnable> runnables = WAITING_JOB_LIST.get(add.getElanInstanceName());
90         if (runnables != null) {
91             runnables.forEach(Runnable::run);
92         }
93     }
94
95     public static void runJobAfterElanIsAvailable(String elanName, Runnable runnable) {
96         WAITING_JOB_LIST.computeIfAbsent(elanName, (name) -> new ArrayList<>());
97         WAITING_JOB_LIST.get(elanName).add(runnable);
98     }
99
100     @Override
101     protected ElanInstanceListener getDataTreeChangeListener() {
102         return ElanInstanceListener.this;
103     }
104
105     @Override
106     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
107         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
108     }
109
110 }