MRI version bumpup for Aluminium
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnInterfaceOpListener.java
1 /*
2  * Copyright (c) 2015 - 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.vpnmanager;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import javax.annotation.PreDestroy;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.genius.infra.Datastore.Configuration;
26 import org.opendaylight.genius.infra.Datastore.Operational;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
29 import org.opendaylight.genius.infra.TypedReadTransaction;
30 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
31 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
32 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
33 import org.opendaylight.mdsal.binding.api.DataBroker;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesOp;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInterfaceOpData;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntryKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.AdjacencyKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 @Singleton
50 public class VpnInterfaceOpListener extends AbstractAsyncDataTreeChangeListener<VpnInterfaceOpDataEntry> {
51     private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceOpListener.class);
52     private final DataBroker dataBroker;
53     private final ManagedNewTransactionRunner txRunner;
54     private final VpnInterfaceManager vpnInterfaceManager;
55     private final VpnFootprintService vpnFootprintService;
56     private final JobCoordinator jobCoordinator;
57     private final ExecutorService executorService = Executors.newSingleThreadExecutor();
58     private final VpnUtil vpnUtil;
59
60     /*public VpnInterfaceOpListener(final DataBroker dataBroker) {
61         super(VpnInterface.class);
62         this.dataBroker = dataBroker;
63     }*/
64
65     @Inject
66     public VpnInterfaceOpListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
67         final VpnFootprintService vpnFootprintService, final JobCoordinator jobCoordinator,
68                                   final VpnUtil vpnUtil) {
69         super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(VpnInterfaceOpData.class)
70                         .child(VpnInterfaceOpDataEntry.class),
71                 org.opendaylight.infrautils.utils.concurrent.Executors
72                         .newListeningSingleThreadExecutor("VpnInterfaceOpListener", LOG));
73         this.dataBroker = dataBroker;
74         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
75         this.vpnInterfaceManager = vpnInterfaceManager;
76         this.vpnFootprintService = vpnFootprintService;
77         this.jobCoordinator = jobCoordinator;
78         this.vpnUtil = vpnUtil;
79         start();
80     }
81
82     public void start() {
83         LOG.info("{} start", getClass().getSimpleName());
84     }
85
86     @Override
87     @PreDestroy
88     public void close() {
89         super.close();
90         org.opendaylight.infrautils.utils.concurrent.Executors.shutdownAndAwaitTermination(getExecutorService());
91     }
92
93     @Override
94     public void remove(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
95             final VpnInterfaceOpDataEntry del) {
96         final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class);
97         final String interfaceName = key.getName();
98         jobCoordinator.enqueueJob("VPNINTERFACE-" + interfaceName,
99             () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
100                 postProcessVpnInterfaceRemoval(identifier, del, tx, null);
101                 LOG.info("remove: Removed vpn operational data for interface {} on dpn {} vpn {}", del.getName(),
102                         del.getDpnId(), del.getVpnInstanceName());
103             })));
104     }
105
106     private void postProcessVpnInterfaceRemoval(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
107             VpnInterfaceOpDataEntry del, @Nullable TypedReadWriteTransaction<Operational> operTx,
108             @Nullable TypedReadTransaction<Configuration> confTx) throws InterruptedException {
109         if (confTx == null) {
110             txRunner.callWithNewReadOnlyTransactionAndClose(CONFIGURATION,
111                 tx -> postProcessVpnInterfaceRemoval(identifier, del, operTx, tx));
112             return;
113         }
114         if (operTx == null) {
115             LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
116                 tx -> postProcessVpnInterfaceRemoval(identifier, del, tx, confTx)), LOG,
117                 "Error post-processing VPN interface removal");
118             return;
119         }
120         final VpnInterfaceOpDataEntryKey key = identifier.firstKeyOf(VpnInterfaceOpDataEntry.class);
121         String interfaceName = key.getName();
122         String vpnName = del.getVpnInstanceName();
123         try {
124             LOG.info("postProcessVpnInterfaceRemoval: interface name {} vpnName {} dpn {}", interfaceName, vpnName,
125                     del.getDpnId());
126             //decrement the vpn interface count in Vpn Instance Op Data
127             Optional<VpnInstance> vpnInstance =
128                 confTx.read(VpnOperDsUtils.getVpnInstanceToVpnIdIdentifier(vpnName)).get();
129
130             if (vpnInstance.isPresent()) {
131                 String rd = vpnInstance.get().getVrfId();
132
133                 VpnInstanceOpDataEntry vpnInstOp = vpnUtil.getVpnInstanceOpData(rd);
134
135                 AdjacenciesOp adjs = del.augmentation(AdjacenciesOp.class);
136                 Map<AdjacencyKey, Adjacency> adjMap = adjs != null ? adjs.getAdjacency() : null;
137
138                 if (vpnInstOp != null && adjMap != null && adjMap.size() > 0) {
139                 /*
140                  * When a VPN Interface is removed by FibManager (aka VrfEntryListener and its cohorts),
141                  * one adjacency or two adjacency (in case of dual-stack)
142                  * for that VPN Interface will be hanging around along with that
143                  * VPN Interface.   That adjacency could be primary (or) non-primary.
144                  * If its a primary adjacency, then a prefix-to-interface entry will be available for the
145                  * same.  If its a non-primary adjacency, then a prefix-to-interface entry will not be
146                  * available for the same, instead we will have vpn-to-extraroutes filled in for them.
147                  *
148                  * Here we try to remove prefix-to-interface entry for pending adjacency in the deleted
149                  * vpnInterface.   More importantly, we also update the vpnInstanceOpData by removing this
150                  * vpnInterface from it.
151                  */
152                     List<Prefixes> prefixToInterface = new ArrayList<>();
153                     for (Adjacency adjacency : adjs.getAdjacency().values()) {
154                         List<Prefixes> prefixToInterfaceLocal = new ArrayList<>();
155                         Optional<Prefixes> prefix = operTx.read(
156                             VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
157                                 VpnUtil.getIpPrefix(adjacency.getIpAddress()))).get();
158                         if (prefix.isPresent()) {
159                             prefixToInterfaceLocal.add(prefix.get());
160                         }
161                         if (prefixToInterfaceLocal.isEmpty() && adjacency.getNextHopIpList() != null) {
162                             for (String nh : adjacency.getNextHopIpList()) {
163                                 prefix = operTx.read(VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
164                                     VpnUtil.getIpPrefix(nh))).get();
165                                 if (prefix.isPresent()) {
166                                     prefixToInterfaceLocal.add(prefix.get());
167                                 }
168                             }
169                         }
170                         if (!prefixToInterfaceLocal.isEmpty()) {
171                             prefixToInterface.addAll(prefixToInterfaceLocal);
172                         }
173                     }
174                 /*
175                  * In VPN Migration scenarios, there is a race condition where we use the new DPNID
176                  * for the migrated VM instead of old DPNID because when we read prefix-to-interface to cleanup
177                  * old DPNID, we actually get the new DPNID.
178                  *
179                  * More dangerously, we tend to alter the new prefix-to-interface which should be retained intac
180                  * for the migration to succeed in L3VPN.  As a workaround, here we are going to use the dpnId in
181                  * the deleted vpnInterface itself instead of tinkering with the prefix-to-interface.  Further we
182                  * will tinker prefix-to-interface only when are damn sure if its value matches our
183                  * deleted vpnInterface.
184                  *
185                  */
186                     for (Prefixes pref : prefixToInterface) {
187                         if (VpnUtil.isMatchedPrefixToInterface(pref, del)) {
188                             operTx.delete(
189                                 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(), pref.getIpAddress()));
190                         }
191                     }
192                 }
193                 if (del.getDpnId() != null) {
194                     vpnFootprintService.updateVpnToDpnMapping(del.getDpnId(), del.getVpnInstanceName(), rd,
195                             interfaceName, null /*ipAddressSourceValuePair*/,
196                             false /* do delete */);
197                 }
198                 LOG.info("postProcessVpnInterfaceRemoval: Removed vpn operational data and updated vpn footprint"
199                         + " for interface {} on dpn {} vpn {}", interfaceName, del.getDpnId(), vpnName);
200             } else {
201                 LOG.error("postProcessVpnInterfaceRemoval: rd not retrievable as vpninstancetovpnid for vpn {}"
202                         + " is absent, trying rd as {}. interface {} dpn {}", vpnName, vpnName, interfaceName,
203                         del.getDpnId());
204             }
205             notifyTaskIfRequired(interfaceName);
206         } catch (InterruptedException | ExecutionException e) {
207             LOG.error("postProcessVpnInterfaceRemoval: Failed to read data store for interface {} vpn {}",
208                     interfaceName, vpnName);
209         }
210     }
211
212     private void notifyTaskIfRequired(String intfName) {
213         Runnable notifyTask = vpnInterfaceManager.isNotifyTaskQueued(intfName);
214         if (notifyTask == null) {
215             LOG.debug("notifyTaskIfRequired: No tasks queued to wait for deletion of vpnInterface {}", intfName);
216             return;
217         }
218         executorService.execute(notifyTask);
219     }
220
221     @Override
222     public void update(final InstanceIdentifier<VpnInterfaceOpDataEntry> identifier,
223             final VpnInterfaceOpDataEntry original, final VpnInterfaceOpDataEntry update) {
224         LOG.info("update: interface {} vpn {}", original.getName(), original.getVpnInstanceName());
225     }
226
227     @Override
228     public void add(InstanceIdentifier<VpnInterfaceOpDataEntry> identifier, VpnInterfaceOpDataEntry add) {
229         LOG.info("add: interface {} vpn {}. Ignoring", add.getName(), add.getVpnInstanceName());
230     }
231 }