Migrate elanmanager to use LoggingFutures
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / evpn / listeners / EvpnElanInstanceListener.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.netvirt.elan.evpn.listeners;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import java.util.concurrent.ExecutionException;
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.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
24 import org.opendaylight.netvirt.elan.evpn.utils.EvpnMacVrfUtils;
25 import org.opendaylight.netvirt.elan.evpn.utils.EvpnUtils;
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.EvpnAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35 @Singleton
36 public class EvpnElanInstanceListener extends AsyncDataTreeChangeListenerBase<ElanInstance, EvpnElanInstanceListener> {
37     private static final Logger LOG = LoggerFactory.getLogger(EvpnElanInstanceListener.class);
38     private final DataBroker broker;
39     private final ManagedNewTransactionRunner txRunner;
40     private final EvpnUtils evpnUtils;
41     private final EvpnMacVrfUtils evpnMacVrfUtils;
42     private final IMdsalApiManager mdsalManager;
43
44
45     @Inject
46     public EvpnElanInstanceListener(final DataBroker dataBroker, final EvpnUtils evpnUtils,
47                                     EvpnMacVrfUtils evpnMacVrfUtils, IMdsalApiManager mdsalApiManager) {
48         super(ElanInstance.class, EvpnElanInstanceListener.class);
49         this.broker = dataBroker;
50         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
51         this.evpnUtils = evpnUtils;
52         this.evpnMacVrfUtils = evpnMacVrfUtils;
53         this.mdsalManager = mdsalApiManager;
54     }
55
56     @Override
57     @PostConstruct
58     public void init() {
59         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
60     }
61
62     @Override
63     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
64         return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class).build();
65     }
66
67     @Override
68     protected void add(InstanceIdentifier<ElanInstance> instanceIdentifier, ElanInstance evpnAugmentation) {
69     }
70
71     @Override
72     protected void remove(InstanceIdentifier<ElanInstance> instanceIdentifier, ElanInstance evpnAugmentation) {
73     }
74
75     @Override
76     protected void update(InstanceIdentifier<ElanInstance> instanceIdentifier, ElanInstance original,
77                           ElanInstance update) {
78         String elanName = update.getElanInstanceName();
79         LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
80             if (evpnUtils.isWithdrawEvpnRT2Routes(original, update)) {
81                 evpnUtils.withdrawEvpnRT2Routes(original.augmentation(EvpnAugmentation.class), elanName);
82                 evpnMacVrfUtils.updateEvpnDmacFlows(original, false);
83                 evpnUtils.programEvpnL2vniDemuxTable(elanName,
84                     (elan, interfaceName) -> evpnUtils.bindElanServiceToExternalTunnel(elanName, interfaceName),
85                     (dpnId, flowEntity) -> mdsalManager.addFlow(confTx, flowEntity));
86             } else if (evpnUtils.isAdvertiseEvpnRT2Routes(original, update)) {
87                 evpnUtils.advertiseEvpnRT2Routes(update.augmentation(EvpnAugmentation.class), elanName);
88                 evpnMacVrfUtils.updateEvpnDmacFlows(update, true);
89                 evpnUtils.programEvpnL2vniDemuxTable(elanName,
90                     (elan, interfaceName) -> evpnUtils.unbindElanServiceFromExternalTunnel(elanName, interfaceName),
91                     (dpnId, flowEntity) -> {
92                         try {
93                             mdsalManager.removeFlow(confTx, dpnId, flowEntity.getFlowId(), flowEntity.getTableId());
94                         } catch (ExecutionException | InterruptedException e) {
95                             LOG.error("Error removing flow", e);
96                         }
97                     });
98             }
99         }), LOG, "Error handling EVPN ELAN instance update");
100     }
101
102     @Override
103     protected EvpnElanInstanceListener getDataTreeChangeListener() {
104         return this;
105     }
106
107 }