NETVIRT-1200: Upstreaming fixes 2
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / InterfaceStateChangeListener.java
1 /*
2  * Copyright (c) 2015, 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 package org.opendaylight.netvirt.vpnmanager;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.HashBasedTable;
12 import com.google.common.collect.Table;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
28 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class InterfaceStateChangeListener
42     extends AsyncDataTreeChangeListenerBase<Interface, InterfaceStateChangeListener> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
45     private static final short DJC_MAX_RETRIES = 3;
46     private final DataBroker dataBroker;
47     private final ManagedNewTransactionRunner txRunner;
48     private final VpnInterfaceManager vpnInterfaceManager;
49     private final JobCoordinator jobCoordinator;
50
51     Table<OperStatus, OperStatus, IntfTransitionState> stateTable = HashBasedTable.create();
52
53     enum IntfTransitionState {
54         STATE_UP,
55         STATE_DOWN,
56         STATE_IGNORE
57     }
58
59     private void initialize() {
60         //  Interface State Transition Table
61         //               Up                Down            Unknown
62         // ---------------------------------------------------------------
63         /* Up       { STATE_IGNORE,   STATE_DOWN,     STATE_IGNORE }, */
64         /* Down     { STATE_UP,       STATE_IGNORE,   STATE_IGNORE }, */
65         /* Unknown  { STATE_UP,       STATE_DOWN,     STATE_IGNORE }, */
66
67         stateTable.put(Interface.OperStatus.Up, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
68         stateTable.put(Interface.OperStatus.Down, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
69         stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
70         stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
71     }
72
73     @Inject
74     public InterfaceStateChangeListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
75             final JobCoordinator jobCoordinator) {
76         super(Interface.class, InterfaceStateChangeListener.class);
77         this.dataBroker = dataBroker;
78         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
79         this.vpnInterfaceManager = vpnInterfaceManager;
80         this.jobCoordinator = jobCoordinator;
81         initialize();
82     }
83
84     @PostConstruct
85     public void start() {
86         LOG.info("{} start", getClass().getSimpleName());
87         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
88     }
89
90
91     @Override
92     protected InstanceIdentifier<Interface> getWildCardPath() {
93         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
94     }
95
96     @Override
97     protected InterfaceStateChangeListener getDataTreeChangeListener() {
98         return InterfaceStateChangeListener.this;
99     }
100
101
102     @Override
103     // TODO Clean up the exception handling
104     @SuppressWarnings("checkstyle:IllegalCatch")
105     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
106         try {
107             if (L2vlan.class.equals(intrf.getType())) {
108                 LOG.info("VPN Interface add event - intfName {} from InterfaceStateChangeListener",
109                                 intrf.getName());
110                 jobCoordinator.enqueueJob("VPNINTERFACE-" + intrf.getName(), () -> {
111                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
112                     futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
113                         ListenableFuture<Void> configFuture
114                             = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
115                                 ListenableFuture<Void> operFuture
116                                     = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> {
117                                         final String interfaceName = intrf.getName();
118                                         LOG.info("Detected interface add event for interface {}", interfaceName);
119                                         final VpnInterface vpnIf =
120                                                 VpnUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
121                                         if (vpnIf != null) {
122                                             for (VpnInstanceNames vpnInterfaceVpnInstance :
123                                                     vpnIf.getVpnInstanceNames()) {
124                                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
125                                                 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
126                                                 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
127                                                     LOG.info("VPN Interface add event - intfName {} onto vpnName {} "
128                                                             + "running oper-driven, VpnInstance not ready, holding"
129                                                             + " on", vpnIf.getName(), vpnName);
130                                                 } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
131                                                     LOG.error("add: Ignoring addition of vpnInterface {}, as"
132                                                             + " vpnInstance {} with primaryRd {} is already marked for"
133                                                             + " deletion", interfaceName, vpnName, primaryRd);
134                                                 } else {
135                                                     BigInteger intfDpnId = BigInteger.ZERO;
136                                                     try {
137                                                         intfDpnId = InterfaceUtils.getDpIdFromInterface(intrf);
138                                                     } catch (Exception e) {
139                                                         LOG.error("Unable to retrieve dpnId for interface {}. "
140                                                                 + "Process vpn interface add failed",intrf.getName(),
141                                                                 e);
142                                                         return;
143                                                     }
144                                                     final BigInteger dpnId = intfDpnId;
145                                                     final int ifIndex = intrf.getIfIndex();
146                                                     LOG.info("VPN Interface add event - intfName {} onto vpnName {}"
147                                                             + " running oper-driven", vpnIf.getName(), vpnName);
148                                                     vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
149                                                             ifIndex, false, writeConfigTxn, writeOperTxn, writeInvTxn,
150                                                             intrf, vpnName);
151
152                                                 }
153                                             }
154
155                                         }
156                                     });
157                                 futures.add(operFuture);
158                                 operFuture.get(); //Synchronous submit of operTxn
159                             });
160                         futures.add(configFuture);
161                         //TODO: Allow immediateFailedFuture from writeCfgTxn to cancel writeInvTxn as well.
162                         Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), true,
163                                 "Operational"));
164                     }));
165                     return futures;
166                 });
167             }
168         } catch (Exception e) {
169             LOG.error("Exception caught in Interface {} Operational State Up event", intrf.getName(), e);
170         }
171     }
172
173     @Override
174     // TODO Clean up the exception handling
175     @SuppressWarnings("checkstyle:IllegalCatch")
176     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
177         final String ifName = intrf.getName();
178         BigInteger dpId = BigInteger.ZERO;
179         try {
180             if (L2vlan.class.equals(intrf.getType())) {
181                 LOG.info("VPN Interface remove event - intfName {} from InterfaceStateChangeListener",
182                                 intrf.getName());
183                 try {
184                     dpId = InterfaceUtils.getDpIdFromInterface(intrf);
185                 } catch (Exception e) {
186                     LOG.error("Unable to retrieve dpnId from interface operational data store for interface"
187                             + " {}. Fetching from vpn interface op data store. ", ifName, e);
188                 }
189                 final BigInteger inputDpId = dpId;
190                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
191                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
192                     ListenableFuture<Void> configFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(
193                         writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
194                             writeOperTxn -> futures.add(
195                                     txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
196                                         VpnInterface cfgVpnInterface =
197                                                 VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
198                                         if (cfgVpnInterface == null) {
199                                             LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
200                                             return;
201                                         }
202                                         for (VpnInstanceNames vpnInterfaceVpnInstance :
203                                                 cfgVpnInterface.getVpnInstanceNames()) {
204                                             String vpnName = vpnInterfaceVpnInstance.getVpnName();
205                                             Optional<VpnInterfaceOpDataEntry> optVpnInterface =
206                                                     VpnUtil.getVpnInterfaceOpDataEntry(dataBroker, ifName, vpnName);
207                                             if (!optVpnInterface.isPresent()) {
208                                                 LOG.debug("Interface {} vpn {} is not a vpninterface, or deletion"
209                                                         + " triggered by northbound agent. ignoring.", ifName, vpnName);
210                                                 continue;
211                                             }
212                                             final VpnInterfaceOpDataEntry vpnInterface = optVpnInterface.get();
213                                             String gwMac = intrf.getPhysAddress() != null ? intrf.getPhysAddress()
214                                                     .getValue() : vpnInterface.getGatewayMacAddress();
215                                             BigInteger dpnId = inputDpId;
216                                             if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
217                                                 dpnId = vpnInterface.getDpnId();
218                                             }
219                                             final int ifIndex = intrf.getIfIndex();
220                                             LOG.info("VPN Interface remove event - intfName {} onto vpnName {}"
221                                                     + " running oper-driver", vpnInterface.getName(), vpnName);
222                                             vpnInterfaceManager.processVpnInterfaceDown(dpnId, ifName, ifIndex, gwMac,
223                                                     vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
224                                         }
225                                     })))));
226                     futures.add(configFuture);
227                     Futures.addCallback(configFuture, new PostVpnInterfaceThreadWorker(intrf.getName(), false,
228                             "Operational"));
229                     return futures;
230                 }, DJC_MAX_RETRIES);
231             }
232         } catch (Exception e) {
233             LOG.error("Exception observed in handling deletion of VPN Interface {}. ", ifName, e);
234         }
235     }
236
237     // TODO Clean up the exception handling
238     @SuppressWarnings("checkstyle:IllegalCatch")
239     @Override
240     protected void update(InstanceIdentifier<Interface> identifier,
241                     Interface original, Interface update) {
242         final String ifName = update.getName();
243         try {
244             if (update.getIfIndex() == null) {
245                 return;
246             }
247             if (L2vlan.class.equals(update.getType())) {
248                 LOG.info("VPN Interface update event - intfName {} from InterfaceStateChangeListener",
249                         update.getName());
250                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
251                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
252                     futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> futures.add(
253                             txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> futures.add(
254                                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
255                                     final VpnInterface vpnIf =
256                                             VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
257                                     if (vpnIf != null) {
258                                         final int ifIndex = update.getIfIndex();
259                                         BigInteger dpnId = BigInteger.ZERO;
260                                         try {
261                                             dpnId = InterfaceUtils.getDpIdFromInterface(update);
262                                         } catch (Exception e) {
263                                             LOG.error("remove: Unable to retrieve dpnId for interface {}", ifName, e);
264                                             return;
265                                         }
266                                         IntfTransitionState state = getTransitionState(original.getOperStatus(),
267                                                 update.getOperStatus());
268                                         if (state.equals(IntfTransitionState.STATE_IGNORE)) {
269                                             LOG.info("InterfaceStateChangeListener: Interface {} state original {}"
270                                                     + "updated {} not handled", ifName, original.getOperStatus(),
271                                                     update.getOperStatus());
272                                             return;
273                                         }
274                                         if (state.equals(IntfTransitionState.STATE_UP)) {
275                                             for (VpnInstanceNames vpnInterfaceVpnInstance :
276                                                     vpnIf.getVpnInstanceNames()) {
277                                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
278                                                 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
279                                                 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
280                                                     LOG.error(
281                                                             "VPN Interface update event - intfName {} onto vpnName {} "
282                                                                     + "running oper-driven UP, VpnInstance not ready,"
283                                                             + " holding on", vpnIf.getName(), vpnName);
284                                                 } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
285                                                     LOG.error("update: Ignoring UP event for vpnInterface {}, as "
286                                                             + "vpnInstance {} with primaryRd {} is already marked for"
287                                                             + " deletion", vpnIf.getName(), vpnName, primaryRd);
288                                                 } else {
289                                                     vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
290                                                             ifIndex, true, writeConfigTxn, writeOperTxn, writeInvTxn,
291                                                             update, vpnName);
292                                                 }
293                                             }
294                                         } else if (state.equals(IntfTransitionState.STATE_DOWN)) {
295                                             for (VpnInstanceNames vpnInterfaceVpnInstance :
296                                                     vpnIf.getVpnInstanceNames()) {
297                                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
298                                                 LOG.info("VPN Interface update event - intfName {} onto vpnName {}"
299                                                        + " running oper-driven DOWN", vpnIf.getName(), vpnName);
300                                                 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
301                                                      VpnUtil.getVpnInterfaceOpDataEntry(dataBroker,
302                                                                          vpnIf.getName(), vpnName);
303                                                 if (optVpnInterface.isPresent()) {
304                                                     VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
305                                                     vpnInterfaceManager.processVpnInterfaceDown(dpnId, vpnIf.getName(),
306                                                             ifIndex, update.getPhysAddress().getValue(), vpnOpInterface,
307                                                             true, writeConfigTxn, writeOperTxn, writeInvTxn);
308                                                 } else {
309                                                     LOG.error(
310                                                             "InterfaceStateChangeListener Update DOWN - vpnInterface {}"
311                                                             + " not available, ignoring event", vpnIf.getName());
312                                                     continue;
313                                                 }
314                                             }
315                                         }
316                                     } else {
317                                         LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
318                                     }
319                                 }))))));
320                     return futures;
321                 });
322             }
323         } catch (Exception e) {
324             LOG.error("Exception observed in handling updation of VPN Interface {}. ", update.getName(), e);
325         }
326     }
327
328     private class PostVpnInterfaceThreadWorker implements FutureCallback<Void> {
329         private final String interfaceName;
330         private final boolean add;
331         private final String txnDestination;
332
333         PostVpnInterfaceThreadWorker(String interfaceName, boolean add, String transactionDest) {
334             this.interfaceName = interfaceName;
335             this.add = add;
336             this.txnDestination = transactionDest;
337         }
338
339         @Override
340         public void onSuccess(Void voidObj) {
341             if (add) {
342                 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} stored into destination {} successfully",
343                         interfaceName, txnDestination);
344             } else {
345                 LOG.debug("InterfaceStateChangeListener:  VrfEntries for {} removed successfully", interfaceName);
346             }
347         }
348
349         @Override
350         public void onFailure(Throwable throwable) {
351             if (add) {
352                 LOG.error("InterfaceStateChangeListener: VrfEntries for {} failed to store into destination {}",
353                         interfaceName, txnDestination, throwable);
354             } else {
355                 LOG.error("InterfaceStateChangeListener: VrfEntries for {} removal failed", interfaceName, throwable);
356                 VpnUtil.unsetScheduledToRemoveForVpnInterface(txRunner, interfaceName);
357             }
358         }
359     }
360
361     private IntfTransitionState getTransitionState(Interface.OperStatus original , Interface.OperStatus updated) {
362         IntfTransitionState transitionState = stateTable.get(original, updated);
363
364         if (transitionState == null) {
365             return IntfTransitionState.STATE_IGNORE;
366         }
367         return transitionState;
368     }
369 }