80da10c4b88102cfa72da5ec7fd9f73183302d06
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / CentralizedSwitchChangeListener.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.vpnmanager;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.concurrent.ExecutionException;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.genius.infra.Datastore.Configuration;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.infrautils.utils.concurrent.Executors;
25 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
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.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.opendaylight.yangtools.yang.common.Uint64;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * CentralizedSwitchChangeListener detect changes in switch:router mapping and
42  * update flows accordingly.<br>
43  * The centralized switch a.k.a NAPT switch is currently defined using models
44  * residing in natservice bundle. As the roles of centralized switch will grow
45  * beyond NAT use cases, the associated models and logic need to be renamed
46  * and moved to either vpnmanager or new bundle as part of Carbon model changes
47  *
48  */
49 @Singleton
50 public class CentralizedSwitchChangeListener
51         extends AbstractAsyncDataTreeChangeListener<RouterToNaptSwitch> {
52
53     private static final Logger LOG = LoggerFactory.getLogger(CentralizedSwitchChangeListener.class);
54
55     private final DataBroker dataBroker;
56     private final ManagedNewTransactionRunner txRunner;
57     private final IVpnManager vpnManager;
58     private final ExternalRouterDataUtil externalRouterDataUtil;
59     private final VpnUtil vpnUtil;
60
61     @Inject
62     public CentralizedSwitchChangeListener(final DataBroker dataBroker, final IVpnManager vpnManager,
63             final ExternalRouterDataUtil externalRouterDataUtil, final VpnUtil vpnUtil) {
64         super(dataBroker, LogicalDatastoreType.CONFIGURATION,
65                 InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class),
66                 Executors.newListeningSingleThreadExecutor("CentralizedSwitchChangeListener", LOG));
67         this.dataBroker = dataBroker;
68         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
69         this.vpnManager = vpnManager;
70         this.externalRouterDataUtil = externalRouterDataUtil;
71         this.vpnUtil = vpnUtil;
72     }
73
74     public void start() {
75         LOG.info("{} start", getClass().getSimpleName());
76     }
77
78     @Override
79     @PreDestroy
80     public void close() {
81         super.close();
82         Executors.shutdownAndAwaitTermination(getExecutorService());
83     }
84
85     @Override
86     public void remove(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
87         LOG.debug("Removing {}", routerToNaptSwitch);
88         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx ->
89                                             setupRouterGwFlows(routerToNaptSwitch, tx, NwConstants.DEL_FLOW)), LOG,
90                                                 "Error processing switch removal for {}", routerToNaptSwitch);
91     }
92
93     @Override
94     public void update(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch origRouterToNaptSwitch,
95             RouterToNaptSwitch updatedRouterToNaptSwitch) {
96         LOG.debug("Updating old {} new {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
97         if (!Objects.equals(updatedRouterToNaptSwitch.getPrimarySwitchId(),
98                 origRouterToNaptSwitch.getPrimarySwitchId())) {
99             ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
100                 setupRouterGwFlows(origRouterToNaptSwitch, tx, NwConstants.DEL_FLOW);
101                 setupRouterGwFlows(updatedRouterToNaptSwitch, tx, NwConstants.ADD_FLOW);
102             }), LOG, "Error updating switch {} to {}", origRouterToNaptSwitch, updatedRouterToNaptSwitch);
103         }
104     }
105
106     @Override
107     public void add(InstanceIdentifier<RouterToNaptSwitch> key, RouterToNaptSwitch routerToNaptSwitch) {
108         LOG.debug("Adding {}", routerToNaptSwitch);
109         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx ->
110                         setupRouterGwFlows(routerToNaptSwitch, tx, NwConstants.ADD_FLOW)), LOG,
111                 "Error processing switch addition for {}", routerToNaptSwitch);
112     }
113
114     private void setupRouterGwFlows(RouterToNaptSwitch routerToNaptSwitch,
115             TypedReadWriteTransaction<Configuration> confTx, int addOrRemove)
116                                     throws ExecutionException, InterruptedException {
117         Routers router = null;
118         if (addOrRemove == NwConstants.ADD_FLOW) {
119             router = vpnUtil.getExternalRouter(routerToNaptSwitch.getRouterName());
120         }
121         else {
122             router = externalRouterDataUtil.getRouter(routerToNaptSwitch.getRouterName());
123         }
124         if (router == null) {
125             LOG.warn("No router data found for router id {}", routerToNaptSwitch.getRouterName());
126             return;
127         }
128
129         Uint64 primarySwitchId = routerToNaptSwitch.getPrimarySwitchId();
130         Uuid extNetworkId = router.getNetworkId();
131         String extGwMacAddress = router.getExtGwMacAddress();
132         String routerName = router.getRouterName();
133         List<ExternalIps> externalIps = router.getExternalIps();
134         if (externalIps.isEmpty()) {
135             LOG.error("CentralizedSwitchChangeListener: setupRouterGwFlows no externalIP present");
136             return;
137         }
138
139         for (ExternalIps extIp : router.nonnullExternalIps()) {
140             Uuid subnetVpnName = extIp.getSubnetId();
141             if (addOrRemove == NwConstants.ADD_FLOW) {
142                 vpnManager.addRouterGwMacFlow(routerName, extGwMacAddress, primarySwitchId, extNetworkId,
143                         subnetVpnName.getValue(), confTx);
144                 externalRouterDataUtil.addtoRouterMap(router);
145             } else {
146                 vpnManager.removeRouterGwMacFlow(routerName, extGwMacAddress, primarySwitchId, extNetworkId,
147                         subnetVpnName.getValue(), confTx);
148                 externalRouterDataUtil.removeFromRouterMap(router);
149             }
150         }
151
152         if (addOrRemove == NwConstants.ADD_FLOW) {
153             vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName,
154                     VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
155                     extGwMacAddress, primarySwitchId, extNetworkId);
156         } else {
157             vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName,
158                     VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
159                     extGwMacAddress, primarySwitchId, extNetworkId);
160         }
161     }
162 }