2 * Copyright (c) 2017 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.internal;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import static org.opendaylight.netvirt.natservice.internal.NatUtil.requireNonNullElse;
14 import com.google.common.base.Optional;
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
21 import javax.annotation.Nonnull;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
30 import org.opendaylight.genius.infra.Datastore.Configuration;
31 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
32 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
33 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
36 import org.opendaylight.netvirt.natservice.api.CentralizedSwitchScheduler;
37 import org.opendaylight.serviceutils.tools.mdsal.listener.AbstractClusteredSyncDataTreeChangeListener;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExtRouters;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.upgrade.rev180702.UpgradeConfig;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeListener<UpgradeConfig> {
54 private static final Logger LOG = LoggerFactory.getLogger(UpgradeStateListener.class);
56 private final DataBroker dataBroker;
57 private final CentralizedSwitchScheduler centralizedSwitchScheduler;
58 private final NatserviceConfig.NatMode natMode;
59 private final SNATDefaultRouteProgrammer defaultRouteProgrammer;
60 private IMdsalApiManager mdsalManager;
61 private IdManagerService idManager;
62 private final NaptSwitchHA naptSwitchHA;
63 private final JobCoordinator coordinator;
64 private final ManagedNewTransactionRunner txRunner;
67 public UpgradeStateListener(final DataBroker dataBroker,
68 final CentralizedSwitchScheduler centralizedSwitchScheduler,
69 final SNATDefaultRouteProgrammer defaultRouteProgrammer,
70 final IMdsalApiManager mdsalManager,
71 final IdManagerService idManager,
72 final NaptSwitchHA naptSwitchHA,
73 final NatserviceConfig config, final JobCoordinator coordinator) {
74 super(dataBroker, new DataTreeIdentifier<>(
75 LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(UpgradeConfig.class)));
76 this.dataBroker = dataBroker;
77 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
78 this.centralizedSwitchScheduler = centralizedSwitchScheduler;
79 this.defaultRouteProgrammer = defaultRouteProgrammer;
80 this.coordinator = coordinator;
81 this.naptSwitchHA = naptSwitchHA;
83 this.natMode = config.getNatMode();
85 this.natMode = NatserviceConfig.NatMode.Controller;
87 LOG.trace("UpgradeStateListener (nat) initialized");
91 public void add(@Nonnull UpgradeConfig newDataObject) {
95 public void remove(@Nonnull UpgradeConfig removedDataObject) {
96 if (natMode == NatserviceConfig.NatMode.Conntrack) {
99 LOG.debug("Verify is all Elected Napt Switch and connected back post upgrade");
103 public void update(@Nonnull UpgradeConfig original, UpgradeConfig updated) {
104 if (natMode == NatserviceConfig.NatMode.Controller) {
105 if (original.isUpgradeInProgress() && !updated.isUpgradeInProgress()) {
106 Optional<NaptSwitches> npatSwitches = NatUtil.getAllPrimaryNaptSwitches(dataBroker);
107 if (npatSwitches.isPresent()) {
108 for (RouterToNaptSwitch routerToNaptSwitch : requireNonNullElse(
109 npatSwitches.get().getRouterToNaptSwitch(), Collections.<RouterToNaptSwitch>emptyList())) {
110 BigInteger primaryNaptDpnId = routerToNaptSwitch.getPrimarySwitchId();
111 if (!NatUtil.getSwitchStatus(dataBroker, routerToNaptSwitch.getPrimarySwitchId())) {
112 String routerUuid = routerToNaptSwitch.getRouterName();
113 coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + routerUuid,
114 () -> Collections.singletonList(
115 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
116 confTx -> reElectNewNaptSwitch(routerUuid, primaryNaptDpnId, confTx)
117 )), NatConstants.NAT_DJC_MAX_RETRIES);
125 LOG.info("UpgradeStateListener update from {} to {}", original, updated);
126 if (!(original.isUpgradeInProgress() && !updated.isUpgradeInProgress())) {
130 SingleTransactionDataBroker reader = new SingleTransactionDataBroker(dataBroker);
133 routers = reader.syncRead(LogicalDatastoreType.CONFIGURATION,
134 InstanceIdentifier.create(ExtRouters.class));
135 } catch (ReadFailedException e) {
136 LOG.error("Error reading external routers", e);
140 for (Routers router : requireNonNullElse(routers.getRouters(), Collections.<Routers>emptyList())) {
141 List<ExternalIps> externalIps = router.getExternalIps();
142 if (router.isEnableSnat() && externalIps != null && !externalIps.isEmpty()) {
143 centralizedSwitchScheduler.scheduleCentralizedSwitch(router);
148 private void reElectNewNaptSwitch(String routerName, BigInteger primaryNaptDpnId,
149 TypedReadWriteTransaction<Configuration> confTx) throws ExecutionException, InterruptedException {
150 // Check if this is externalRouter else ignore
151 InstanceIdentifier<Routers> extRoutersId = NatUtil.buildRouterIdentifier(routerName);
152 Optional<Routers> routerData =
153 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
154 LogicalDatastoreType.CONFIGURATION, extRoutersId);
155 if (!routerData.isPresent()) {
156 LOG.debug("reElectNewNaptSwitch : SNAT->Ignoring Re-election for router {} since its not External Router",
160 Uuid networkId = routerData.get().getNetworkId();
161 if (networkId == null) {
162 LOG.error("hndlTepDelForSnatInEachRtr : SNAT -> Ignoring Re-election with Napt {} for router {}"
163 + "as external network configuraton is missing", primaryNaptDpnId, routerName);
166 long routerId = NatUtil.getVpnId(dataBroker, routerName);
167 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Router {} is associated with ext nw {}", routerId, networkId);
168 Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
170 if (bgpVpnUuid == null) {
171 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Internal VPN-ID {} associated to router {}",
172 routerId, routerName);
175 bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
176 if (bgpVpnId == NatConstants.INVALID_ID) {
177 LOG.error("hndlTepDelForSnatInEachRtr :SNAT->Invalid Private BGP VPN ID returned for routerName {}",
182 defaultRouteProgrammer.removeDefNATRouteInDPN(primaryNaptDpnId, bgpVpnId, confTx);
183 if (routerData.get().isEnableSnat()) {
184 LOG.info("hndlTepDelForSnatInEachRtr : SNAT enabled for router {}", routerId);
186 long routerVpnId = routerId;
187 if (bgpVpnId != NatConstants.INVALID_ID) {
188 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT -> Private BGP VPN ID (Internal BGP VPN ID) {} "
189 + "associated to the router {}", bgpVpnId, routerName);
190 routerVpnId = bgpVpnId;
192 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT -> Internal L3 VPN ID (Router ID) {} "
193 + "associated to the router {}", routerVpnId, routerName);
195 //Re-elect the other available switch as the NAPT switch and program the NAT flows.
196 ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,
197 routerName, networkId);
198 if (extNwProvType == null) {
201 NatUtil.removeSNATFromDPN(dataBroker, mdsalManager, idManager, naptSwitchHA, primaryNaptDpnId, routerName,
202 routerId, routerVpnId, extNwProvType, confTx);
205 LOG.info("hndlTepDelForSnatInEachRtr : SNAT is not enabled for router {} to handle addDPN event {}",
206 routerId, primaryNaptDpnId);