2 * Copyright (c) 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
8 package org.opendaylight.netvirt.natservice.internal;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import java.util.Objects;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
23 import org.opendaylight.netvirt.natservice.api.CentralizedSwitchScheduler;
24 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
25 import org.opendaylight.serviceutils.upgrade.UpgradeState;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExtRouters;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class SnatExternalRoutersListener extends AsyncDataTreeChangeListenerBase<Routers, SnatExternalRoutersListener> {
38 private static final Logger LOG = LoggerFactory.getLogger(SnatExternalRoutersListener.class);
40 private final DataBroker dataBroker;
41 private final ManagedNewTransactionRunner txRunner;
42 private final IdManagerService idManager;
43 private final CentralizedSwitchScheduler centralizedSwitchScheduler;
44 private final NatMode natMode;
45 private final UpgradeState upgradeState;
46 private final SnatServiceManager natServiceManager;
49 public SnatExternalRoutersListener(final DataBroker dataBroker,
50 final IdManagerService idManager,
51 final CentralizedSwitchScheduler centralizedSwitchScheduler,
52 final NatserviceConfig config,
53 final SnatServiceManager natServiceManager,
54 final UpgradeState upgradeState) {
55 super(Routers.class, SnatExternalRoutersListener.class);
56 this.dataBroker = dataBroker;
57 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
58 this.idManager = idManager;
59 this.centralizedSwitchScheduler = centralizedSwitchScheduler;
60 this.upgradeState = upgradeState;
61 this.natServiceManager = natServiceManager;
63 this.natMode = config.getNatMode();
65 this.natMode = NatMode.Conntrack;
72 LOG.info("{} init", getClass().getSimpleName());
73 // This class handles ExternalRouters for Conntrack SNAT mode.
74 // For Controller SNAT mode, its handled in ExternalRoutersListeners.java
75 if (natMode == NatMode.Conntrack) {
76 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
77 NatUtil.createGroupIdPool(idManager);
82 protected InstanceIdentifier<Routers> getWildCardPath() {
83 return InstanceIdentifier.create(ExtRouters.class).child(Routers.class);
87 @SuppressWarnings("checkstyle:IllegalCatch")
88 protected void add(InstanceIdentifier<Routers> identifier, Routers routers) {
89 String routerName = routers.getRouterName();
90 if (upgradeState.isUpgradeInProgress()) {
91 LOG.warn("add event for ext-router {}, but upgrade is in progress.", routerName);
95 LOG.info("add : external router event for {}", routerName);
96 long routerId = NatUtil.getVpnId(dataBroker, routerName);
97 NatUtil.createRouterIdsConfigDS(dataBroker, routerId, routerName);
98 Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
99 if (bgpVpnUuid != null) {
100 // Router associated to BGPVPN, ignoring it.
103 // Allocate Primary NAPTSwitch for this router
104 centralizedSwitchScheduler.scheduleCentralizedSwitch(routers);
108 protected void update(InstanceIdentifier<Routers> identifier, Routers original, Routers update) {
109 String routerName = original.getRouterName();
110 Long routerId = NatUtil.getVpnId(dataBroker, routerName);
111 if (routerId == NatConstants.INVALID_ID) {
112 LOG.error("update : external router event - Invalid routerId for routerName {}", routerName);
115 LOG.info("update :called for router {} with originalSNATStatus {} and updatedSNATStatus {}",
116 routerName, original.isEnableSnat(), update.isEnableSnat());
117 if (!upgradeState.isUpgradeInProgress()) {
118 centralizedSwitchScheduler.updateCentralizedSwitch(original, update);
120 if (!Objects.equals(original.getSubnetIds(), update.getSubnetIds())
121 || !Objects.equals(original.getExternalIps(), update.getExternalIps())) {
122 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
123 confTx -> natServiceManager.notify(confTx, update, original, null, null,
124 SnatServiceManager.Action.SNAT_ROUTER_UPDATE)), LOG,
125 "error handling external router update");
130 protected void remove(InstanceIdentifier<Routers> identifier, Routers router) {
131 if (identifier == null || router == null) {
132 LOG.error("remove : returning without processing since ext-router is null");
136 LOG.info("remove : external router event for {}", router.getRouterName());
137 centralizedSwitchScheduler.releaseCentralizedSwitch(router);
141 protected SnatExternalRoutersListener getDataTreeChangeListener() {
142 return SnatExternalRoutersListener.this;