b3236a56e2e4c99650ca419b7f055f7d0cbb9886
[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.PostConstruct;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
22 import org.opendaylight.genius.infra.Datastore.Configuration;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
25 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
28 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.Uint64;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
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
46  *
47  */
48 @Singleton
49 public class CentralizedSwitchChangeListener
50         extends AsyncDataTreeChangeListenerBase<RouterToNaptSwitch, CentralizedSwitchChangeListener> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(CentralizedSwitchChangeListener.class);
53
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;
59
60     @Inject
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;
69     }
70
71     @Override
72     @PostConstruct
73     public void init() {
74         LOG.info("{} init", getClass().getSimpleName());
75         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
76     }
77
78     @Override
79     protected InstanceIdentifier<RouterToNaptSwitch> getWildCardPath() {
80         return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class);
81     }
82
83     @Override
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);
89     }
90
91     @Override
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);
101         }
102     }
103
104     @Override
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);
110     }
111
112     @Override
113     protected CentralizedSwitchChangeListener getDataTreeChangeListener() {
114         return this;
115     }
116
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());
123         }
124         else {
125             router = externalRouterDataUtil.getRouter(routerToNaptSwitch.getRouterName());
126         }
127         if (router == null) {
128             LOG.warn("No router data found for router id {}", routerToNaptSwitch.getRouterName());
129             return;
130         }
131
132         Uint64 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");
139             return;
140         }
141
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);
148             } else {
149                 vpnManager.removeRouterGwMacFlow(routerName, extGwMacAddress, primarySwitchId, extNetworkId,
150                         subnetVpnName.getValue(), confTx);
151                 externalRouterDataUtil.removeFromRouterMap(router);
152             }
153         }
154
155         if (addOrRemove == NwConstants.ADD_FLOW) {
156             vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName,
157                     VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
158                     extGwMacAddress, primarySwitchId, extNetworkId);
159         } else {
160             vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName,
161                     VpnUtil.getIpsListFromExternalIps(router.getExternalIps()),
162                     extGwMacAddress, primarySwitchId, extNetworkId);
163         }
164     }
165 }