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 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;
40 * CentralizedSwitchChangeListener detect changes in switch:router mapping and
41 * update flows accordingly.
44 public class SnatCentralizedSwitchChangeListener
45 extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, SnatCentralizedSwitchChangeListener> {
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;
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;
69 LOG.info("{} init", getClass().getSimpleName());
70 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
74 protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
75 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
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());
84 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
85 confTx -> snatServiceManger.notify(confTx, router, null, primarySwitchId, null,
86 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL)), LOG,
87 "error handling SNAT centralized switch removal");
88 natDataUtil.removeFromRouterMap(router);
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 BigInteger updatedPrimarySwitchId = updatedRouterToNaptSwitch.getPrimarySwitchId();
98 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
99 Routers origRouter = NatUtil.getRoutersFromConfigDS(confTx, origRouterToNaptSwitch.getRouterName());
100 Routers updatedRouter = NatUtil.getRoutersFromConfigDS(confTx, updatedRouterToNaptSwitch.getRouterName());
101 if (origPrimarySwitchId != updatedPrimarySwitchId) {
102 if (origRouter != null) {
103 snatServiceManger.notify(confTx, origRouter, null, origPrimarySwitchId, null,
104 SnatServiceManager.Action.CNT_ROUTER_ALL_SWITCH_DISBL);
105 if (origRouterToNaptSwitch.isEnableSnat()) {
106 snatServiceManger.notify(confTx, origRouter, null, origPrimarySwitchId, null,
107 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
109 natDataUtil.removeFromRouterMap(origRouter);
111 if (updatedRouter != null) {
112 natDataUtil.updateRouterMap(updatedRouter);
113 snatServiceManger.notify(confTx, updatedRouter, null, updatedPrimarySwitchId, null,
114 SnatServiceManager.Action.CNT_ROUTER_ALL_SWITCH_ENBL);
115 if (updatedRouterToNaptSwitch.isEnableSnat()) {
116 snatServiceManger.notify(confTx, updatedRouter, null, updatedPrimarySwitchId, null,
117 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
121 boolean origIsSnatEnabled = false;
122 boolean updatedIsSnatEnabled = false;
123 if (origRouterToNaptSwitch.isEnableSnat() != null) {
124 origIsSnatEnabled = origRouterToNaptSwitch.isEnableSnat();
126 if (updatedRouterToNaptSwitch.isEnableSnat() != null) {
127 updatedIsSnatEnabled = updatedRouterToNaptSwitch.isEnableSnat();
129 if (origIsSnatEnabled != updatedIsSnatEnabled) {
130 if (updatedRouterToNaptSwitch.isEnableSnat()) {
131 snatServiceManger.notify(confTx, updatedRouter, null, updatedPrimarySwitchId, null,
132 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
134 snatServiceManger.notify(confTx, origRouter, null, origPrimarySwitchId, null,
135 SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
139 }), LOG, "Error handling SNAT centralized switch update");
143 protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
144 LOG.debug("Adding {}", routerToNaptSwitch);
145 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
146 String routerName = routerToNaptSwitch.getRouterName();
147 Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
148 final boolean isEnableSnat;
149 if (routerToNaptSwitch.isEnableSnat() != null) {
150 isEnableSnat = routerToNaptSwitch.isEnableSnat();
152 isEnableSnat = false;
154 long vpnId = NatUtil.getVpnId(dataBroker, routerName);
155 if (vpnId == NatConstants.INVALID_ID) {
156 LOG.warn("VpnId not unavailable for router {} yet", routerName);
157 eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
158 NatUtil.getVpnInstanceToVpnIdIdentifier(routerName), (unused, newVpnId) -> {
159 ListenableFutures.addErrorLogging(
160 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
161 innerConfTx -> handleAdd(innerConfTx, routerName, router, primarySwitchId,
163 "Error handling router addition");
164 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
165 }, Duration.ofSeconds(5), iid -> LOG.error("VpnId not found for router {}", routerName));
168 ListenableFutures.addErrorLogging(
169 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
170 confTx -> handleAdd(confTx, routerName, router, primarySwitchId,
171 isEnableSnat)), LOG, "Error handling router addition");
174 private void handleAdd(TypedReadWriteTransaction<Datastore.Configuration> confTx,
175 String routerName, Routers router, BigInteger primarySwitchId, boolean isSnatEnabled)
176 throws ExecutionException, InterruptedException {
177 if (router != null) {
178 natDataUtil.addtoRouterMap(router);
179 snatServiceManger.notify(confTx, router, null, primarySwitchId, null,
180 SnatServiceManager.Action.CNT_ROUTER_ALL_SWITCH_ENBL);
182 snatServiceManger.notify(confTx, router, null, primarySwitchId, null,
183 SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
186 LOG.error("Router {} not found for primarySwitch {}", routerName, primarySwitchId);
191 protected SnatCentralizedSwitchChangeListener getDataTreeChangeListener() {