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