2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
26 import org.opendaylight.infrautils.utils.concurrent.Executors;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExternalNetworks;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.networks.Networks;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.Ports;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMap;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.common.Uint64;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 public class ExternalNetworksChangeListener extends AbstractAsyncDataTreeChangeListener<Networks> {
51 private static final Logger LOG = LoggerFactory.getLogger(ExternalNetworksChangeListener.class);
52 private final DataBroker dataBroker;
53 private final ManagedNewTransactionRunner txRunner;
54 private final FloatingIPListener floatingIpListener;
55 private final ExternalRoutersListener externalRouterListener;
56 private final OdlInterfaceRpcService interfaceManager;
57 private final JobCoordinator coordinator;
58 private final NatMode natMode;
61 public ExternalNetworksChangeListener(final DataBroker dataBroker, final FloatingIPListener floatingIpListener,
62 final ExternalRoutersListener externalRouterListener,
63 final OdlInterfaceRpcService interfaceManager,
64 final NatserviceConfig config,
65 final JobCoordinator coordinator) {
66 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ExternalNetworks.class)
67 .child(Networks.class),
68 Executors.newListeningSingleThreadExecutor("ExternalNetworksChangeListener", LOG));
69 this.dataBroker = dataBroker;
70 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
71 this.floatingIpListener = floatingIpListener;
72 this.externalRouterListener = externalRouterListener;
73 this.interfaceManager = interfaceManager;
74 this.coordinator = coordinator;
76 this.natMode = config.getNatMode();
78 this.natMode = NatMode.Controller;
83 LOG.info("{} init", getClass().getSimpleName());
90 Executors.shutdownAndAwaitTermination(getExecutorService());
94 public void add(InstanceIdentifier<Networks> identifier, Networks networks) {
99 public void remove(InstanceIdentifier<Networks> identifier, Networks networks) {
100 if (identifier == null || networks == null || networks.getRouterIds() == null
101 || networks.getRouterIds().isEmpty()) {
102 LOG.warn("remove : returning without processing since networks/identifier is null: "
103 + "identifier: {}, networks: {}", identifier, networks);
107 for (Uuid routerId: networks.getRouterIds()) {
108 String routerName = routerId.toString();
110 InstanceIdentifier<RouterToNaptSwitch> routerToNaptSwitchInstanceIdentifier =
111 NatUtil.buildNaptSwitchIdentifier(routerName);
113 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.OPERATIONAL, routerToNaptSwitchInstanceIdentifier);
115 LOG.debug("remove : successful deletion of data in napt-switches container");
120 public void update(InstanceIdentifier<Networks> identifier, Networks original, Networks update) {
121 //Check for VPN disassociation
122 Uuid originalVpn = original.getVpnid();
123 Uuid updatedVpn = update.getVpnid();
124 if (originalVpn == null && updatedVpn != null) {
125 //external network is dis-associated from L3VPN instance
126 associateExternalNetworkWithVPN(update);
127 } else if (originalVpn != null && updatedVpn == null) {
128 //external network is associated with vpn
129 disassociateExternalNetworkFromVPN(update, originalVpn.getValue());
130 //Remove the SNAT entries
131 removeSnatEntries(original, original.getId());
135 private void removeSnatEntries(Networks original, Uuid networkUuid) {
136 if (original.getRouterIds() != null) {
137 for (Uuid routerUuid : original.getRouterIds()) {
138 Uint32 routerId = NatUtil.getVpnId(dataBroker, routerUuid.getValue());
139 if (routerId == NatConstants.INVALID_ID) {
140 LOG.error("removeSnatEntries : Invalid routerId returned for routerName {}", routerUuid.getValue());
143 Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker,routerId);
144 if (natMode == NatMode.Controller) {
145 coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + routerUuid.getValue(),
146 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
147 tx -> externalRouterListener.handleDisableSnatInternetVpn(routerUuid.getValue(), routerId,
148 networkUuid, externalIps, original.getVpnid().getValue(), tx))),
149 NatConstants.NAT_DJC_MAX_RETRIES);
155 private void associateExternalNetworkWithVPN(Networks network) {
156 if (network.getRouterIds() != null) {
157 List<Uuid> routerIds = network.getRouterIds();
158 for (Uuid routerId : routerIds) {
159 //long router = NatUtil.getVpnId(dataBroker, routerId.getValue());
161 InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerId.getValue());
162 Optional<RouterPorts> optRouterPorts = null;
164 optRouterPorts = SingleTransactionDataBroker.syncReadOptional(dataBroker,
165 LogicalDatastoreType.CONFIGURATION, routerPortsId);
166 } catch (ExecutionException | InterruptedException e) {
167 LOG.error("associateExternalNetworkWithVPN: Exception while reading RouterPorts DS for the "
168 + "router {} network {} ", routerId, network.getId().getValue(), e);
171 if (!optRouterPorts.isPresent()) {
172 LOG.debug("associateExternalNetworkWithVPN : Could not read Router Ports data object with id: {} "
173 + "to handle associate ext nw {}", routerId, network.getId());
176 RouterPorts routerPorts = optRouterPorts.get();
177 for (Ports port : routerPorts.nonnullPorts()) {
178 String portName = port.getPortName();
179 Uint64 dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
180 if (dpnId.equals(Uint64.ZERO)) {
181 LOG.debug("associateExternalNetworkWithVPN : DPN not found for {}, "
182 + "skip handling of ext nw {} association", portName, network.getId());
185 for (InternalToExternalPortMap ipMap : port.nonnullInternalToExternalPortMap()) {
186 // remove all VPN related entries
187 coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + ipMap.key(),
188 () -> Collections.singletonList(
189 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
190 tx -> floatingIpListener.createNATFlowEntries(dpnId, portName, routerId.getValue(),
191 network.getId(), ipMap, tx))), NatConstants.NAT_DJC_MAX_RETRIES);
197 for (Uuid routerId : routerIds) {
198 LOG.debug("associateExternalNetworkWithVPN() : for routerId {}", routerId);
199 Uuid networkId = network.getId();
200 if (networkId == null) {
201 LOG.error("associateExternalNetworkWithVPN : networkId is null for the router ID {}", routerId);
204 final String vpnName = network.getVpnid().getValue();
205 if (vpnName == null) {
206 LOG.error("associateExternalNetworkWithVPN : No VPN associated with ext nw {} for router {}",
207 networkId, routerId);
211 Uint64 dpnId = Uint64.valueOf("0");
212 InstanceIdentifier<RouterToNaptSwitch> routerToNaptSwitch =
213 NatUtil.buildNaptSwitchRouterIdentifier(routerId.getValue());
214 Optional<RouterToNaptSwitch> rtrToNapt = Optional.empty();
216 rtrToNapt = SingleTransactionDataBroker.syncReadOptional(dataBroker,
217 LogicalDatastoreType.CONFIGURATION, routerToNaptSwitch);
218 } catch (ExecutionException | InterruptedException e) {
219 LOG.error("associateExternalNetworkWithVPN: Exception while reading routerToNaptSwitch DS for the "
220 + "router {}", routerId, e);
222 if (rtrToNapt.isPresent()) {
223 dpnId = rtrToNapt.get().getPrimarySwitchId();
225 LOG.debug("associateExternalNetworkWithVPN : got primarySwitch as dpnId{} ", dpnId);
226 if (dpnId == null || dpnId.equals(Uint64.ZERO)) {
227 LOG.warn("associateExternalNetworkWithVPN : primary napt Switch not found for router {} on dpn: {}",
231 final Uint64 finalDpnId = dpnId;
232 coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + routerId.getValue(),
233 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
235 Uint32 routerIdentifier = NatUtil.getVpnId(dataBroker, routerId.getValue());
236 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice
237 .rev160111.intext.ip.map.IpMapping> idBuilder =
238 InstanceIdentifier.builder(IntextIpMap.class)
239 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111
240 .intext.ip.map.IpMapping.class,
241 new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111
242 .intext.ip.map.IpMappingKey(routerIdentifier));
243 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice
244 .rev160111.intext.ip.map.IpMapping> id = idBuilder.build();
245 Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111
246 .intext.ip.map.IpMapping> ipMapping = MDSALUtil.read(dataBroker,
247 LogicalDatastoreType.OPERATIONAL, id);
248 if (ipMapping.isPresent()) {
249 for (IpMap ipMap : ipMapping.get().nonnullIpMap()) {
250 String externalIp = ipMap.getExternalIp();
252 "associateExternalNetworkWithVPN : Calling advToBgpAndInstallFibAndTsFlows "
253 + "for dpnId {},vpnName {} and externalIp {}", finalDpnId, vpnName,
255 if (natMode == NatMode.Controller) {
256 externalRouterListener.advToBgpAndInstallFibAndTsFlows(finalDpnId,
257 NwConstants.INBOUND_NAPT_TABLE, vpnName, routerIdentifier,
258 routerId.getValue(), externalIp, network.getId(),
259 null /* external-router */, confTx);
263 LOG.warn("associateExternalNetworkWithVPN: No ipMapping present fot the routerId {}",
267 Uint32 vpnId = NatUtil.getVpnId(dataBroker, vpnName);
268 // Install 47 entry to point to 21
269 if (natMode == NatMode.Controller) {
270 externalRouterListener.installNaptPfibEntriesForExternalSubnets(routerId.getValue(),
272 if (vpnId.longValue() != -1) {
273 LOG.debug("associateExternalNetworkWithVPN : Calling externalRouterListener "
274 + "installNaptPfibEntry for dpnId {} and vpnId {}", finalDpnId, vpnId);
275 externalRouterListener.installNaptPfibEntry(finalDpnId, vpnId, confTx);
278 })), NatConstants.NAT_DJC_MAX_RETRIES);
283 private void disassociateExternalNetworkFromVPN(Networks network, String vpnName) {
284 if (network.getRouterIds() != null) {
285 for (Uuid routerId : network.getRouterIds()) {
286 InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerId.getValue());
287 Optional<RouterPorts> optRouterPorts = Optional.empty();
289 optRouterPorts = SingleTransactionDataBroker.syncReadOptional(dataBroker,
290 LogicalDatastoreType.CONFIGURATION, routerPortsId);
291 } catch (ExecutionException | InterruptedException e) {
292 LOG.error("disassociateExternalNetworkFromVPN: Exception while reading RouterPorts DS for the "
293 + "router {} network {} vpn {}", routerId, network.getId().getValue(), vpnName, e);
295 if (!optRouterPorts.isPresent()) {
297 "disassociateExternalNetworkFromVPN : Could not read Router Ports data object with id: {} "
298 + "to handle disassociate ext nw {}", routerId, network.getId());
301 RouterPorts routerPorts = optRouterPorts.get();
302 for (Ports port : routerPorts.nonnullPorts()) {
303 String portName = port.getPortName();
304 Uint64 dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
305 if (dpnId.equals(Uint64.ZERO)) {
306 LOG.debug("disassociateExternalNetworkFromVPN : DPN not found for {},"
307 + "skip handling of ext nw {} disassociation", portName, network.getId());
310 for (InternalToExternalPortMap intExtPortMap : port.nonnullInternalToExternalPortMap()) {
311 coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + intExtPortMap.key(),
312 () -> Collections.singletonList(
313 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
314 tx -> floatingIpListener.removeNATFlowEntries(dpnId, portName, vpnName,
316 intExtPortMap, tx))), NatConstants.NAT_DJC_MAX_RETRIES);