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