service restart for l2gw
[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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
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.recovery.impl.L2GatewayServiceRecoveryHandler;
25 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
26 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
27 import org.opendaylight.serviceutils.srm.RecoverableListener;
28 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @Singleton
39 public class ElanInstanceListener extends AsyncClusteredDataTreeChangeListenerBase<ElanInstance, ElanInstanceListener>
40         implements RecoverableListener {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceListener.class);
43
44     private final DataBroker broker;
45     private final ManagedNewTransactionRunner txRunner;
46     private final ElanClusterUtils elanClusterUtils;
47
48     @Inject
49     public ElanInstanceListener(final DataBroker db, final ElanClusterUtils elanClusterUtils,
50                                 final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
51                                 final ServiceRecoveryRegistry serviceRecoveryRegistry) {
52         super(ElanInstance.class, ElanInstanceListener.class);
53         broker = db;
54         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
55         this.elanClusterUtils = elanClusterUtils;
56         serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this);
57     }
58
59     @PostConstruct
60     public void init() {
61         registerListener();
62     }
63
64     @Override
65     public void registerListener() {
66         LOG.info("Registering ElanInstanceListener");
67         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
68     }
69
70     public void deregisterListener() {
71         LOG.info("Deregistering ElanInstanceListener");
72         super.deregisterListener();
73     }
74
75     @Override
76     protected void remove(final InstanceIdentifier<ElanInstance> identifier,
77                           final ElanInstance del) {
78         elanClusterUtils.runOnlyInOwnerNode(del.getElanInstanceName(), "delete Elan instance",
79             () -> {
80                 LOG.info("Elan instance {} deleted from Configuration tree ", del);
81                 List<L2gatewayConnection> connections =
82                         L2GatewayConnectionUtils.getL2GwConnectionsByElanName(
83                                 this.broker, del.getElanInstanceName());
84                 if (connections.isEmpty()) {
85                     return Collections.emptyList();
86                 }
87                 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
88                     tx -> {
89                         for (L2gatewayConnection connection : connections) {
90                             InstanceIdentifier<L2gatewayConnection> iid =
91                                     InstanceIdentifier.create(Neutron.class).child(
92                                             L2gatewayConnections.class).child(
93                                             L2gatewayConnection.class, connection.key());
94                             tx.delete(iid);
95                         }
96                     });
97                 LoggingFutures.addErrorLogging(future, LOG,
98                         "Failed to delete associate L2 gateway connection while deleting network");
99                 return Collections.singletonList(future);
100             });
101     }
102
103     @Override
104     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
105
106     }
107
108     @Override
109     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance add) {
110     }
111
112     @Override
113     protected ElanInstanceListener getDataTreeChangeListener() {
114         return ElanInstanceListener.this;
115     }
116
117     @Override
118     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
119         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
120     }
121
122 }