2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.vpnmanager;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import java.math.BigInteger;
14 import java.util.List;
15 import java.util.Objects;
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.infra.Datastore.Configuration;
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.genius.mdsalutil.NwConstants;
28 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
29 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
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.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.ext.routers.routers.ExternalIps;
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.<br>
42 * The centralized switch a.k.a NAPT switch is currently defined using models
43 * residing in natservice bundle. As the roles of centralized switch will grow
44 * beyond NAT use cases, the associated models and logic need to be renamed
45 * and moved to either vpnmanager or new bundle as part of Carbon model changes
49 public class CentralizedSwitchChangeListener
50 extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, CentralizedSwitchChangeListener> {
52 private static final Logger LOG = LoggerFactory.getLogger(CentralizedSwitchChangeListener.class);
54 private final DataBroker dataBroker;
55 private final ManagedNewTransactionRunner txRunner;
56 private final IVpnManager vpnManager;
57 private final ExternalRouterDataUtil externalRouterDataUtil;
58 private final VpnUtil vpnUtil;
61 public CentralizedSwitchChangeListener(final DataBroker dataBroker, final IVpnManager vpnManager,
62 final ExternalRouterDataUtil externalRouterDataUtil, final VpnUtil vpnUtil) {
63 super(RouterToNaptSwitch.class, CentralizedSwitchChangeListener.class);
64 this.dataBroker = dataBroker;
65 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
66 this.vpnManager = vpnManager;
67 this.externalRouterDataUtil = externalRouterDataUtil;
68 this.vpnUtil = vpnUtil;
74 LOG.info("{} init", getClass().getSimpleName());
75 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
79 protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
80 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
84 protected void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
85 LOG.debug("Removing {}", routerToNaptSwitch);
86 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx ->
87 setupRouterGwFlows(routerToNaptSwitch, tx, NwConstants.DEL_FLOW)), LOG,
88 "Error processing switch removal for {}", routerToNaptSwitch);
92 protected void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
93 RouterToNaptSwitch updatedRouterToNaptSwitch) {
94 LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
95 if (!Objects.equals(updatedRouterToNaptSwitch.getPrimarySwitchId(),
96 origRouterToNaptSwitch.getPrimarySwitchId())) {
97 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
98 setupRouterGwFlows(origRouterToNaptSwitch, tx, NwConstants.DEL_FLOW);
99 setupRouterGwFlows(updatedRouterToNaptSwitch, tx, NwConstants.ADD_FLOW);
100 }), LOG, "Error updating switch {} to {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
105 protected void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
106 LOG.debug("Adding {}", routerToNaptSwitch);
107 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx ->
108 setupRouterGwFlows(routerToNaptSwitch, tx, NwConstants.ADD_FLOW)), LOG,
109 "Error processing switch addition for {}", routerToNaptSwitch);
113 protected CentralizedSwitchChangeListener getDataTreeChangeListener() {
117 private void setupRouterGwFlows(RouterToNaptSwitch routerToNaptSwitch,
118 TypedReadWriteTransaction<Configuration> confTx, int addOrRemove)
119 throws ExecutionException, InterruptedException {
120 Routers router = null;
121 if (addOrRemove == NwConstants.ADD_FLOW) {
122 router = vpnUtil.getExternalRouter(routerToNaptSwitch.getRouterName());
125 router = externalRouterDataUtil.getRouter(routerToNaptSwitch.getRouterName());
127 if (router == null) {
128 LOG.warn("No router data found for router id {}", routerToNaptSwitch.getRouterName());
132 BigInteger primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
133 Uuid extNetworkId = router.getNetworkId();
134 String extGwMacAddress = router.getExtGwMacAddress();
135 String routerName = router.getRouterName();
136 List<ExternalIps> externalIps = router.getExternalIps();
137 if (externalIps.isEmpty()) {
138 LOG.error("CentralizedSwitchChangeListener: setupRouterGwFlows no externalIP present");
142 for (ExternalIps extIp : router.nonnullExternalIps()) {
143 Uuid subnetVpnName = extIp.getSubnetId();
144 if (addOrRemove == NwConstants.ADD_FLOW) {
145 vpnManager.addRouterGwMacFlow(routerName, extGwMacAddress, primarySwitchId, extNetworkId,
146 subnetVpnName.getValue(), confTx);
147 externalRouterDataUtil.addtoRouterMap(router);
149 vpnManager.removeRouterGwMacFlow(routerName, extGwMacAddress, primarySwitchId, extNetworkId,
150 subnetVpnName.getValue(), confTx);
151 externalRouterDataUtil.removeFromRouterMap(router);
155 if (addOrRemove == NwConstants.ADD_FLOW) {
156 vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName,
157 VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
158 extGwMacAddress, primarySwitchId, extNetworkId);
160 vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName,
161 VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
162 extGwMacAddress, primarySwitchId, extNetworkId);