Moving vpn-interface yang from VPNMgr > NeutronVPN
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / iplearn / LearntVpnVipToPortEventProcessor.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.vpnmanager.iplearn;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.locks.ReentrantLock;
17 import javax.annotation.PostConstruct;
18 import javax.annotation.PreDestroy;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
27 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
28 import org.opendaylight.genius.infra.Datastore;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
32 import org.opendaylight.genius.mdsalutil.NWUtil;
33 import org.opendaylight.genius.utils.JvmGlobalLocks;
34 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
35 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
36 import org.opendaylight.mdsal.eos.binding.api.Entity;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
39 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
40 import org.opendaylight.netvirt.vpnmanager.VpnConstants;
41 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.LearntVpnVipToPortEventAction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.LearntVpnVipToPortEventData;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.event.data.LearntVpnVipToPortEvent;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.Adjacencies;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency.AdjacencyType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.AdjacencyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.AdjacencyKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterface;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterfaceBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.vpn.portip.port.data.VpnPortipToPort;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.Uint32;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 @Singleton
60 public class LearntVpnVipToPortEventProcessor
61         extends AsyncClusteredDataTreeChangeListenerBase<LearntVpnVipToPortEvent, LearntVpnVipToPortEventProcessor> {
62     private static final Logger LOG = LoggerFactory.getLogger(LearntVpnVipToPortEventProcessor.class);
63     private final DataBroker dataBroker;
64     private final ManagedNewTransactionRunner txRunner;
65     private final IInterfaceManager interfaceManager;
66     public static final String MIP_PROCESSING_JOB  = "MIP-JOB";
67     private final JobCoordinator jobCoordinator;
68     private final EntityOwnershipUtils entityOwnershipUtils;
69     private EntityOwnershipCandidateRegistration candidateRegistration;
70     private final VpnUtil vpnUtil;
71
72     @Inject
73     public LearntVpnVipToPortEventProcessor(final DataBroker dataBroker, IInterfaceManager interfaceManager,
74             EntityOwnershipService entityOwnershipService, final JobCoordinator jobCoordinator, VpnUtil vpnUtil) {
75         super(LearntVpnVipToPortEvent.class, LearntVpnVipToPortEventProcessor.class);
76         this.dataBroker = dataBroker;
77         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
78         this.interfaceManager = interfaceManager;
79         this.jobCoordinator = jobCoordinator;
80         this.entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
81         this.vpnUtil = vpnUtil;
82     }
83
84     @PostConstruct
85     public void start() {
86         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
87         try {
88             candidateRegistration = entityOwnershipUtils.getEntityOwnershipService()
89                     .registerCandidate(new Entity(VpnConstants.IP_MONITORING_ENTITY,
90                             VpnConstants.IP_MONITORING_ENTITY));
91         } catch (CandidateAlreadyRegisteredException e) {
92             LOG.error("Failed to register the entity {}", VpnConstants.IP_MONITORING_ENTITY);
93         }
94     }
95
96     @PreDestroy
97     @Override
98     public void close() {
99         super.close();
100         if (candidateRegistration != null) {
101             candidateRegistration.close();
102         }
103     }
104
105     @Override
106     protected InstanceIdentifier<LearntVpnVipToPortEvent> getWildCardPath() {
107         return InstanceIdentifier.create(LearntVpnVipToPortEventData.class).child(LearntVpnVipToPortEvent.class);
108     }
109
110     @Override
111     protected LearntVpnVipToPortEventProcessor getDataTreeChangeListener() {
112         return this;
113     }
114
115     @Override
116     protected void update(InstanceIdentifier<LearntVpnVipToPortEvent> id, LearntVpnVipToPortEvent value,
117             LearntVpnVipToPortEvent dataObjectModificationAfter) {
118         // Updates does not make sense on an event queue .
119         // NOTE: DONOT ADD ANY CODE HERE AND MAKE A CIRCUS
120     }
121
122     @Override
123     protected void add(InstanceIdentifier<LearntVpnVipToPortEvent> identifier, LearntVpnVipToPortEvent value) {
124         // AFTER PROCESSING THE EVENT, REMOVE THE EVENT FROM THE QUEUE
125         entityOwnershipUtils.runOnlyInOwnerNode(VpnConstants.IP_MONITORING_ENTITY, VpnConstants.IP_MONITORING_ENTITY,
126             jobCoordinator, "LearntVpnVipToPortEvent-Handler", () -> {
127                 try {
128                     String vpnName = value.getVpnName();
129                     String ipAddress = value.getSrcFixedip();
130                     if (value.getEventAction() == LearntVpnVipToPortEventAction.Add) {
131                         jobCoordinator.enqueueJob(VpnUtil.buildIpMonitorJobKey(ipAddress, vpnName),
132                                 new AddMipAdjacencyWorker(value));
133                     }
134                     if (value.getEventAction() == LearntVpnVipToPortEventAction.Delete) {
135                         jobCoordinator.enqueueJob(VpnUtil.buildIpMonitorJobKey(ipAddress, vpnName),
136                                 new DeleteMipAdjacencyWorker(value));
137                     }
138                 } finally {
139                     // remove the processed event
140                     vpnUtil.removeLearntVpnVipToPortEvent(value.getLearntVpnVipEventId(), null);
141                 }
142             });
143     }
144
145     @Override
146     protected void remove(InstanceIdentifier<LearntVpnVipToPortEvent> key, LearntVpnVipToPortEvent value) {
147         // Removals are triggered by add handling.
148         // NOTE: DONOT ADD ANY CODE HERE AND MAKE A CIRCUS
149     }
150
151     private class AddMipAdjacencyWorker implements Callable<List<ListenableFuture<Void>>> {
152         String vpnName;
153         String interfaceName;
154         String srcIpAddress;
155         String destIpAddress;
156         String macAddress;
157
158         AddMipAdjacencyWorker(LearntVpnVipToPortEvent event) {
159             this.vpnName = event.getVpnName();
160             this.interfaceName = event.getPortName();
161             this.srcIpAddress = event.getSrcFixedip();
162             this.destIpAddress = event.getDestFixedip();
163             this.macAddress = event.getMacAddress();
164         }
165
166         @Override
167         public List<ListenableFuture<Void>> call() {
168             return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
169                                                                         Datastore.OPERATIONAL, operTx -> {
170                     addMipAdjacency(vpnName, interfaceName, srcIpAddress, macAddress, destIpAddress);
171                     vpnUtil.createVpnPortFixedIpToPort(vpnName, srcIpAddress,
172                             interfaceName, Boolean.TRUE, macAddress, null);
173                     vpnUtil.createLearntVpnVipToPort(vpnName, srcIpAddress, interfaceName, macAddress, operTx);
174                 }));
175         }
176
177         private void addMipAdjacency(String vpnInstName, String vpnInterface, String srcPrefix, String mipMacAddress,
178                                      String dstPrefix) {
179             LOG.trace("Adding {} adjacency to VPN Interface {} ", srcPrefix, vpnInterface);
180             InstanceIdentifier<VpnInterface> vpnIfId = VpnUtil.getVpnInterfaceIdentifier(vpnInterface);
181             // FIXME: separate out to somehow?
182             final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnInterface);
183             lock.lock();
184             try {
185                 Optional<VpnInterface> optVpnInterface = SingleTransactionDataBroker.syncReadOptional(dataBroker,
186                     LogicalDatastoreType.CONFIGURATION, vpnIfId);
187                 if (!optVpnInterface.isPresent()) {
188                     LOG.error("Config VpnInterface not found for interface={}", interfaceName);
189                     return;
190                 }
191                 Adjacencies configAdjacencies = optVpnInterface.get().augmentation(Adjacencies.class);
192                 List<Adjacency> adjacencyList = configAdjacencies == null ? new ArrayList<>()
193                         : new ArrayList<>(configAdjacencies.getAdjacency());
194
195                 String ip = VpnUtil.getIpPrefix(srcPrefix);
196                 AdjacencyBuilder newAdjBuilder;
197                 if (interfaceManager.isExternalInterface(vpnInterface)) {
198                     String subnetId = getSubnetId(vpnInstName, dstPrefix);
199                     if (subnetId == null) {
200                         LOG.trace("Can't find corresponding subnet for src IP {}, src MAC {}, dst IP {},"
201                                 + "  in VPN {}", srcPrefix, mipMacAddress, dstPrefix, vpnInstName);
202                         return;
203                     }
204                     newAdjBuilder = new AdjacencyBuilder().setIpAddress(ip).withKey(new AdjacencyKey(ip))
205                             .setAdjacencyType(AdjacencyType.PrimaryAdjacency).setMacAddress(mipMacAddress)
206                             .setSubnetId(new Uuid(subnetId)).setPhysNetworkFunc(true);
207                 } else {
208                     String nextHopIp = null;
209                     String nextHopMacAddress = null;
210                     for (Adjacency adjacency : adjacencyList) {
211                         if (adjacency.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
212                             if (adjacency.getIpAddress().equals(ip)) {
213                                 LOG.error("The MIP {} is already present as a primary adjacency for interface {}."
214                                         + "Skipping adjacency addition.", ip, interfaceName);
215                                 return;
216                             } else if (NWUtil.getEtherTypeFromIpPrefix(ip) == NWUtil
217                                     .getEtherTypeFromIpPrefix(adjacency.getIpAddress())) {
218                                 nextHopIp = adjacency.getIpAddress().split("/")[0];
219                                 nextHopMacAddress = adjacency.getMacAddress();
220                                 break;
221                             }
222                         }
223                     }
224                     if (nextHopIp == null) {
225                         LOG.error("Next Hop IP not found for MIP={}, interface={}, vpnName {}. Skipping adjacency "
226                                 + "addition.", ip, interfaceName, vpnName);
227                         return;
228                     }
229
230                     String rd = vpnUtil.getVpnRd(vpnInstName);
231                     Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
232                         VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnInstName, ip));
233                     if (label.longValue() == VpnConstants.INVALID_LABEL) {
234                         LOG.error("Unable to fetch label from Id Manager. Bailing out of adding MIP adjacency {}"
235                                 + " to vpn interface {} for vpn {}", ip, vpnInterface, vpnInstName);
236                         return;
237                     }
238                     newAdjBuilder = new AdjacencyBuilder().setIpAddress(ip).withKey(new AdjacencyKey(ip))
239                             .setNextHopIpList(Collections.singletonList(nextHopIp))
240                             .setAdjacencyType(AdjacencyType.LearntIp);
241                     if (mipMacAddress != null && !mipMacAddress.equalsIgnoreCase(nextHopMacAddress)) {
242                         newAdjBuilder.setMacAddress(mipMacAddress);
243                     }
244                 }
245                 adjacencyList.add(newAdjBuilder.build());
246                 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencyList);
247                 VpnInterface newVpnIntf = new VpnInterfaceBuilder(optVpnInterface.get())
248                         .addAugmentation(Adjacencies.class, aug).build();
249                 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnIfId,
250                     newVpnIntf, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
251                 LOG.debug(" Successfully stored subnetroute Adjacency into VpnInterface {}", vpnInterface);
252             } catch (ReadFailedException e) {
253                 LOG.error("addMipAdjacency: Failed to read data store for interface {} vpn {} ip {} mac {}",
254                         vpnInterface, vpnInstName, srcPrefix, mipMacAddress);
255             } catch (TransactionCommitFailedException e) {
256                 LOG.error("addMipAdjacency: Failed to commit to data store for interface {} vpn {} ip {} mac {}",
257                         vpnInterface, vpnInstName, srcPrefix, mipMacAddress);
258             } finally {
259                 lock.unlock();
260             }
261         }
262
263         @Nullable
264         private String getSubnetId(String vpnInstName, String ip) {
265             // Check if this IP belongs to a router_interface
266             VpnPortipToPort vpnPortipToPort =
267                     vpnUtil.getNeutronPortFromVpnPortFixedIp(vpnInstName, ip);
268             if (vpnPortipToPort != null && vpnPortipToPort.isSubnetIp()) {
269                 List<Adjacency> adjacencies =
270                     vpnUtil.getAdjacenciesForVpnInterfaceFromConfig(vpnPortipToPort.getPortName());
271                 if (adjacencies != null) {
272                     for (Adjacency adjacency : adjacencies) {
273                         if (adjacency.getAdjacencyType() == AdjacencyType.PrimaryAdjacency) {
274                             return adjacency.getSubnetId().getValue();
275                         }
276                     }
277                 }
278             }
279
280             // Check if this IP belongs to a router_gateway
281             List<Uuid> routerIds = vpnUtil.getExternalNetworkRouterIds(new Uuid(vpnInstName));
282             for (Uuid routerId : routerIds) {
283                 Uuid subnetId = vpnUtil.getSubnetFromExternalRouterByIp(routerId, ip);
284                 if (subnetId != null) {
285                     return subnetId.getValue();
286                 }
287             }
288
289             // Check if this IP belongs to  external network
290             String extSubnetId = vpnUtil.getAssociatedExternalSubnet(ip);
291             if (extSubnetId != null) {
292                 LOG.info("The IP belongs to extenal subnet {} ", extSubnetId);
293                 return extSubnetId;
294             }
295
296             return null;
297         }
298     }
299
300     private class DeleteMipAdjacencyWorker implements Callable<List<ListenableFuture<Void>>> {
301         String vpnName;
302         String interfaceName;
303         String ipAddress;
304
305         DeleteMipAdjacencyWorker(LearntVpnVipToPortEvent event) {
306             this.vpnName = event.getVpnName();
307             this.interfaceName = event.getPortName();
308             this.ipAddress = event.getSrcFixedip();
309         }
310
311         @Override
312         public List<ListenableFuture<Void>> call() {
313             List<ListenableFuture<Void>> futures = new ArrayList<>();
314             vpnUtil.removeMipAdjAndLearntIp(vpnName, interfaceName,  ipAddress);
315             return futures;
316         }
317
318     }
319
320 }
321