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