Clean up MDSALManager exception handling
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / ha / SnatCentralizedSwitchChangeListener.java
1 /*
2  * Copyright (c) 2017, 2018 Red Hat, Inc. 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.natservice.ha;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import java.math.BigInteger;
14
15 import java.time.Duration;
16 import java.util.concurrent.ExecutionException;
17 import javax.annotation.PostConstruct;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
23 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
24 import org.opendaylight.genius.infra.Datastore;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
28 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
29 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
30 import org.opendaylight.netvirt.natservice.internal.NatConstants;
31 import org.opendaylight.netvirt.natservice.internal.NatUtil;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * CentralizedSwitchChangeListener detect changes in switch:router mapping and
41  * update flows accordingly.
42  */
43 @Singleton
44 public class SnatCentralizedSwitchChangeListener
45         extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(SnatCentralizedSwitchChangeListener.class);
48     private final DataBroker dataBroker;
49     private final ManagedNewTransactionRunner txRunner;
50     private final SnatServiceManager snatServiceManger;
51     private final NatDataUtil natDataUtil;
52     private final DataTreeEventCallbackRegistrar eventCallbacks;
53
54     @Inject
55     public SnatCentralizedSwitchChangeListener(final DataBroker dataBroker,
56             final SnatServiceManager snatServiceManger, NatDataUtil natDataUtil,
57             final DataTreeEventCallbackRegistrar dataTreeEventCallbackRegistrar) {
58         super(RouterToNaptSwitch.class, SnatCentralizedSwitchChangeListener.class);
59         this.dataBroker = dataBroker;
60         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
61         this.snatServiceManger = snatServiceManger;
62         this.natDataUtil = natDataUtil;
63         this.eventCallbacks = dataTreeEventCallbackRegistrar;
64     }
65
66     @Override
67     @PostConstruct
68     public void init() {
69         LOG.info("{} init", getClass().getSimpleName());
70         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
71     }
72
73     @Override
74     protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
75         return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
76     }
77
78     @Override
79     protected void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
80         LOG.debug("Deleting {}", routerToNaptSwitch);
81         BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
82         Routers router = natDataUtil.getRouter(routerToNaptSwitch.getRouterName());
83         if (router != null) {
84             ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
85                 confTx -> snatServiceManger.notify(confTx, router, primarySwitchId, null,
86                     SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL)), LOG,
87                 "error handling SNAT centralized switch removal");
88             natDataUtil.removeFromRouterMap(router);
89         }
90     }
91
92     @Override
93     protected void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
94             RouterToNaptSwitch updatedRouterToNaptSwitch) {
95         LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
96         BigInteger origPrimarySwitchId = origRouterToNaptSwitch.getPrimarySwitchId();
97         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
98             Routers origRouter = NatUtil.getRoutersFromConfigDS(confTx, origRouterToNaptSwitch.getRouterName());
99             if (origRouter != null) {
100                 snatServiceManger.notify(confTx, origRouter, origPrimarySwitchId, null,
101                     SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
102                 natDataUtil.removeFromRouterMap(origRouter);
103             }
104             BigInteger updatedPrimarySwitchId = updatedRouterToNaptSwitch.getPrimarySwitchId();
105             Routers updatedRouter = NatUtil.getRoutersFromConfigDS(confTx, updatedRouterToNaptSwitch.getRouterName());
106             if (updatedRouter != null) {
107                 natDataUtil.updateRouterMap(updatedRouter);
108                 snatServiceManger.notify(confTx, updatedRouter, updatedPrimarySwitchId, null,
109                     SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
110             }
111         }), LOG, "Error handling SNAT centralized switch update");
112     }
113
114     @Override
115     protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
116         LOG.debug("Adding {}", routerToNaptSwitch);
117         BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
118         String routerName = routerToNaptSwitch.getRouterName();
119         Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
120         long vpnId = NatUtil.getVpnId(dataBroker, routerName);
121         if (vpnId == NatConstants.INVALID_ID) {
122             LOG.warn("VpnId not unavailable for router {} yet", routerName);
123             eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
124                 NatUtil.getVpnInstanceToVpnIdIdentifier(routerName), (unused, newVpnId) -> {
125                     ListenableFutures.addErrorLogging(
126                         txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
127                             innerConfTx -> handleAdd(innerConfTx, routerName, router, primarySwitchId)), LOG,
128                         "Error handling router addition");
129                     return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
130                 }, Duration.ofSeconds(5), iid -> LOG.error("VpnId not found for router {}", routerName));
131             return;
132         }
133         ListenableFutures.addErrorLogging(
134             txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
135                 confTx -> handleAdd(confTx, routerName, router, primarySwitchId)), LOG,
136             "Error handling router addition");
137     }
138
139     private void handleAdd(TypedReadWriteTransaction<Datastore.Configuration> confTx,
140             String routerName, Routers router, BigInteger primarySwitchId)
141             throws ExecutionException, InterruptedException {
142         if (router != null) {
143             natDataUtil.addtoRouterMap(router);
144             snatServiceManger.notify(confTx, router, primarySwitchId, null,
145                 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
146         } else {
147             LOG.error("Router {} not found for primarySwitch {}", routerName, primarySwitchId);
148         }
149     }
150
151     @Override
152     protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {
153         return this;
154     }
155 }