bb391562e1d507be2bdf3b1a829eaa3c75b1d7c2
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanLearntVpnVipToPortListener.java
1 /*
2  * Copyright (c) 2016 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.elan.internal;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.ListenableFuture;
15
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.Callable;
21
22 import java.util.concurrent.ExecutionException;
23 import javax.annotation.PostConstruct;
24 import javax.inject.Inject;
25 import javax.inject.Singleton;
26
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
30 import org.opendaylight.genius.infra.Datastore.Configuration;
31 import org.opendaylight.genius.infra.Datastore.Operational;
32 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
33 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
34 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
35 import org.opendaylight.genius.infra.TypedWriteTransaction;
36 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
37 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
38 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
39 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
40 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
41 import org.opendaylight.netvirt.elan.utils.ElanConstants;
42 import org.opendaylight.netvirt.elan.utils.ElanUtils;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.LearntVpnVipToPortData;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 @Singleton
56 public class ElanLearntVpnVipToPortListener extends
57         AsyncDataTreeChangeListenerBase<LearntVpnVipToPort, ElanLearntVpnVipToPortListener> {
58     private static final Logger LOG = LoggerFactory.getLogger(ElanLearntVpnVipToPortListener.class);
59     private final DataBroker broker;
60     private final ManagedNewTransactionRunner txRunner;
61     private final IInterfaceManager interfaceManager;
62     private final ElanUtils elanUtils;
63     private final JobCoordinator jobCoordinator;
64     private final ElanInstanceCache elanInstanceCache;
65     private final ElanInterfaceCache elanInterfaceCache;
66
67     @Inject
68     public ElanLearntVpnVipToPortListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils,
69             JobCoordinator jobCoordinator, ElanInstanceCache elanInstanceCache, ElanInterfaceCache elanInterfaceCache) {
70         super(LearntVpnVipToPort.class, ElanLearntVpnVipToPortListener.class);
71         this.broker = broker;
72         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
73         this.interfaceManager = interfaceManager;
74         this.elanUtils = elanUtils;
75         this.jobCoordinator = jobCoordinator;
76         this.elanInstanceCache = elanInstanceCache;
77         this.elanInterfaceCache = elanInterfaceCache;
78     }
79
80     @Override
81     @PostConstruct
82     public void init() {
83         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
84     }
85
86     @Override
87     protected InstanceIdentifier<LearntVpnVipToPort> getWildCardPath() {
88         return InstanceIdentifier.create(LearntVpnVipToPortData.class).child(LearntVpnVipToPort.class);
89     }
90
91     @Override
92     protected void remove(InstanceIdentifier<LearntVpnVipToPort> key, LearntVpnVipToPort dataObjectModification) {
93         String macAddress = dataObjectModification.getMacAddress();
94         String interfaceName = dataObjectModification.getPortName();
95         LOG.trace("Removing mac address {} from interface {} ", macAddress, interfaceName);
96         jobCoordinator.enqueueJob(buildJobKey(macAddress, interfaceName),
97                 new StaticMacRemoveWorker(macAddress, interfaceName));
98     }
99
100     @Override
101     protected void update(InstanceIdentifier<LearntVpnVipToPort> key, LearntVpnVipToPort dataObjectModificationBefore,
102             LearntVpnVipToPort dataObjectModificationAfter) {
103     }
104
105     @Override
106     protected void add(InstanceIdentifier<LearntVpnVipToPort> key, LearntVpnVipToPort dataObjectModification) {
107         String macAddress = dataObjectModification.getMacAddress();
108         String interfaceName = dataObjectModification.getPortName();
109         LOG.trace("Adding mac address {} to interface {} ", macAddress, interfaceName);
110         jobCoordinator.enqueueJob(buildJobKey(macAddress, interfaceName),
111                 new StaticMacAddWorker(macAddress, interfaceName));
112     }
113
114     @Override
115     protected ElanLearntVpnVipToPortListener getDataTreeChangeListener() {
116         return this;
117     }
118
119     private class StaticMacAddWorker implements Callable<List<ListenableFuture<Void>>> {
120         String macAddress;
121         String interfaceName;
122
123         StaticMacAddWorker(String macAddress, String interfaceName) {
124             this.macAddress = macAddress;
125             this.interfaceName = interfaceName;
126         }
127
128         @Override
129         public List<ListenableFuture<Void>> call() {
130             Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
131             if (!elanInterface.isPresent()) {
132                 LOG.debug("ElanInterface Not present for interfaceName {} for add event", interfaceName);
133                 return Collections.emptyList();
134             }
135             List<ListenableFuture<Void>> futures = new ArrayList<>();
136             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, interfaceTx ->
137                 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, flowTx ->
138                     addMacEntryToDsAndSetupFlows(elanInterface.get().getElanInstanceName(), interfaceTx,
139                             flowTx, ElanConstants.STATIC_MAC_TIMEOUT)))));
140             return futures;
141         }
142
143         private void addMacEntryToDsAndSetupFlows(String elanName, TypedWriteTransaction<Operational> interfaceTx,
144                 TypedWriteTransaction<Configuration> flowTx, int macTimeOut) {
145             LOG.trace("Adding mac address {} and interface name {} to ElanInterfaceForwardingEntries and "
146                 + "ElanForwardingTables DS", macAddress, interfaceName);
147             BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
148             PhysAddress physAddress = new PhysAddress(macAddress);
149             MacEntry macEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
150                     .withKey(new MacEntryKey(physAddress)).setControllerLearnedForwardingEntryTimestamp(timeStamp)
151                     .setIsStaticAddress(false).build();
152             InstanceIdentifier<MacEntry> macEntryId = ElanUtils
153                     .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
154             interfaceTx.put(macEntryId, macEntry);
155             InstanceIdentifier<MacEntry> elanMacEntryId =
156                     ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
157             interfaceTx.put(elanMacEntryId, macEntry);
158             ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
159             elanUtils.setupMacFlows(elanInstance, interfaceManager.getInterfaceInfo(interfaceName), macTimeOut,
160                     macAddress, true, flowTx);
161         }
162     }
163
164     private class StaticMacRemoveWorker implements Callable<List<ListenableFuture<Void>>> {
165         String macAddress;
166         String interfaceName;
167
168         StaticMacRemoveWorker(String macAddress, String interfaceName) {
169             this.macAddress = macAddress;
170             this.interfaceName = interfaceName;
171         }
172
173         @Override
174         public List<ListenableFuture<Void>> call() {
175             Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
176             if (!elanInterface.isPresent()) {
177                 LOG.debug("ElanInterface Not present for interfaceName {} for delete event", interfaceName);
178                 return Collections.emptyList();
179             }
180             List<ListenableFuture<Void>> futures = new ArrayList<>();
181             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, interfaceTx ->
182                 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, flowTx ->
183                     deleteMacEntryFromDsAndRemoveFlows(elanInterface.get().getElanInstanceName(),
184                             interfaceTx, flowTx)))));
185             return futures;
186         }
187
188         private void deleteMacEntryFromDsAndRemoveFlows(String elanName,
189                 TypedWriteTransaction<Operational> interfaceTx, TypedReadWriteTransaction<Configuration> flowTx)
190                 throws ExecutionException, InterruptedException {
191             LOG.trace("Deleting mac address {} and interface name {} from ElanInterfaceForwardingEntries "
192                     + "and ElanForwardingTables DS", macAddress, interfaceName);
193             PhysAddress physAddress = new PhysAddress(macAddress);
194             MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
195             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
196             if (macEntry != null && interfaceInfo != null) {
197                 elanUtils.deleteMacFlows(elanInstanceCache.get(elanName).orNull(), interfaceInfo, macEntry, flowTx);
198                 interfaceTx.delete(
199                         ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress));
200                 interfaceTx.delete(
201                         ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress));
202             }
203         }
204     }
205
206     private String buildJobKey(String mac, String interfaceName) {
207         return "ENTERPRISEMACJOB" + mac + interfaceName;
208     }
209
210
211 }