2 * Copyright (c) 2017, 2018 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.natservice.ha;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import java.math.BigInteger;
15 import java.time.Duration;
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.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
22 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
23 import org.opendaylight.genius.infra.Datastore;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
26 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
27 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
28 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
29 import org.opendaylight.netvirt.natservice.internal.NatConstants;
30 import org.opendaylight.netvirt.natservice.internal.NatUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * CentralizedSwitchChangeListener detect changes in switch:router mapping and
40 * update flows accordingly.
43 public class SnatCentralizedSwitchChangeListener
44 extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
46 private static final Logger LOG = LoggerFactory.getLogger(SnatCentralizedSwitchChangeListener.class);
47 private final DataBroker dataBroker;
48 private final ManagedNewTransactionRunner txRunner;
49 private final SnatServiceManager snatServiceManger;
50 private final NatDataUtil natDataUtil;
51 private final DataTreeEventCallbackRegistrar eventCallbacks;
54 public SnatCentralizedSwitchChangeListener(final DataBroker dataBroker,
55 final SnatServiceManager snatServiceManger, NatDataUtil natDataUtil,
56 final DataTreeEventCallbackRegistrar dataTreeEventCallbackRegistrar) {
57 super(RouterToNaptSwitch.class, SnatCentralizedSwitchChangeListener.class);
58 this.dataBroker = dataBroker;
59 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
60 this.snatServiceManger = snatServiceManger;
61 this.natDataUtil = natDataUtil;
62 this.eventCallbacks = dataTreeEventCallbackRegistrar;
68 LOG.info("{} init", getClass().getSimpleName());
69 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
73 protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
74 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
78 protected void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
79 LOG.debug("Deleting {}", routerToNaptSwitch);
80 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
81 Routers router = natDataUtil.getRouter(routerToNaptSwitch.getRouterName());
83 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
84 confTx -> snatServiceManger.notify(confTx, router, primarySwitchId, null,
85 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL)), LOG,
86 "error handling SNAT centralized switch removal");
87 natDataUtil.removeFromRouterMap(router);
92 protected void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
93 RouterToNaptSwitch updatedRouterToNaptSwitch) {
94 LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
95 BigInteger origPrimarySwitchId = origRouterToNaptSwitch.getPrimarySwitchId();
96 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
97 Routers origRouter = NatUtil.getRoutersFromConfigDS(confTx, origRouterToNaptSwitch.getRouterName());
98 if (origRouter != null) {
99 snatServiceManger.notify(confTx, origRouter, origPrimarySwitchId, null,
100 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
101 natDataUtil.removeFromRouterMap(origRouter);
103 BigInteger updatedPrimarySwitchId = updatedRouterToNaptSwitch.getPrimarySwitchId();
104 Routers updatedRouter = NatUtil.getRoutersFromConfigDS(confTx, updatedRouterToNaptSwitch.getRouterName());
105 if (updatedRouter != null) {
106 natDataUtil.updateRouterMap(updatedRouter);
107 snatServiceManger.notify(confTx, updatedRouter, updatedPrimarySwitchId, null,
108 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
110 }), LOG, "Error handling SNAT centralized switch update");
114 protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
115 LOG.debug("Adding {}", routerToNaptSwitch);
116 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
117 String routerName = routerToNaptSwitch.getRouterName();
118 Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
119 long vpnId = NatUtil.getVpnId(dataBroker, routerName);
120 if (vpnId == NatConstants.INVALID_ID) {
121 LOG.warn("VpnId not unavailable for router {} yet", routerName);
122 eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
123 NatUtil.getVpnInstanceToVpnIdIdentifier(routerName), (unused, newVpnId) -> {
124 ListenableFutures.addErrorLogging(
125 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
126 innerConfTx -> handleAdd(innerConfTx, routerName, router, primarySwitchId)), LOG,
127 "Error handling router addition");
128 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
129 }, Duration.ofSeconds(5), iid -> LOG.error("VpnId not found for router {}", routerName));
132 ListenableFutures.addErrorLogging(
133 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
134 confTx -> handleAdd(confTx, routerName, router, primarySwitchId)), LOG,
135 "Error handling router addition");
138 private void handleAdd(TypedReadWriteTransaction<Datastore.Configuration> confTx,
139 String routerName, Routers router, BigInteger primarySwitchId) {
140 if (router != null) {
141 natDataUtil.addtoRouterMap(router);
142 snatServiceManger.notify(confTx, router, primarySwitchId, null,
143 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
145 LOG.error("Router {} not found for primarySwitch {}", routerName, primarySwitchId);
150 protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {