NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanServiceProvider.java
1 /*
2  * Copyright (c) 2016, 2017 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
9 package org.opendaylight.netvirt.elan.internal;
10
11 import static java.util.Collections.emptyList;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.concurrent.Future;
24 import java.util.function.BiFunction;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.genius.infra.Datastore;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
31 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
32 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
33 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
34 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
35 import org.opendaylight.genius.mdsalutil.MDSALUtil;
36 import org.opendaylight.genius.mdsalutil.NwConstants;
37 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
38 import org.opendaylight.genius.utils.ServiceIndex;
39 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
40 import org.opendaylight.infrautils.inject.AbstractLifecycle;
41 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
42 import org.opendaylight.mdsal.binding.api.DataBroker;
43 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
44 import org.opendaylight.mdsal.eos.binding.api.Entity;
45 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
46 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
47 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
48 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderInput;
49 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
50 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
51 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
52 import org.opendaylight.netvirt.elan.utils.ElanConstants;
53 import org.opendaylight.netvirt.elan.utils.ElanUtils;
54 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
55 import org.opendaylight.netvirt.elanmanager.api.IElanService;
56 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
87 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
88 import org.opendaylight.yangtools.yang.common.RpcResult;
89 import org.opendaylight.yangtools.yang.common.Uint32;
90 import org.opendaylight.yangtools.yang.common.Uint64;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
93
94 @Singleton
95 public class ElanServiceProvider extends AbstractLifecycle implements IElanService {
96
97     private static final Logger LOG = LoggerFactory.getLogger(ElanServiceProvider.class);
98
99     private final IdManagerService idManager;
100     private final IInterfaceManager interfaceManager;
101     private final ElanBridgeManager bridgeMgr;
102     private final DataBroker broker;
103     private final ManagedNewTransactionRunner txRunner;
104     private final ElanUtils elanUtils;
105     private final SouthboundUtils southboundUtils;
106     private final IMdsalApiManager mdsalManager;
107     private final ElanInstanceCache elanInstanceCache;
108     private final ElanInterfaceCache elanInterfaceCache;
109     private boolean isL2BeforeL3;
110
111     private final EntityOwnershipCandidateRegistration candidateRegistration;
112
113     @Inject
114     public ElanServiceProvider(IdManagerService idManager, IInterfaceManager interfaceManager,
115                                ElanBridgeManager bridgeMgr,
116                                DataBroker dataBroker,
117                                ElanUtils elanUtils,
118                                EntityOwnershipService entityOwnershipService,
119                                SouthboundUtils southboundUtils, ElanInstanceCache elanInstanceCache,
120                                ElanInterfaceCache elanInterfaceCache, IMdsalApiManager mdsalManager) {
121         this.idManager = idManager;
122         this.interfaceManager = interfaceManager;
123         this.bridgeMgr = bridgeMgr;
124         this.broker = dataBroker;
125         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
126         this.elanUtils = elanUtils;
127         this.southboundUtils = southboundUtils;
128         this.elanInstanceCache = elanInstanceCache;
129         this.elanInterfaceCache = elanInterfaceCache;
130         this.mdsalManager = mdsalManager;
131
132         candidateRegistration = registerCandidate(entityOwnershipService);
133     }
134
135     @Nullable
136     private static EntityOwnershipCandidateRegistration registerCandidate(
137             EntityOwnershipService entityOwnershipService) {
138         try {
139             return entityOwnershipService.registerCandidate(
140                     new Entity(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE));
141         } catch (CandidateAlreadyRegisteredException e) {
142             LOG.error("failed to register the entity");
143             return null;
144         }
145     }
146
147     @Override
148     @SuppressWarnings("checkstyle:IllegalCatch")
149     protected void start() throws Exception {
150         LOG.info("Starting ElanServiceProvider");
151         setIsL2BeforeL3();
152         createIdPool();
153     }
154
155     @Override
156     protected void stop() {
157         if (candidateRegistration != null) {
158             candidateRegistration.close();
159         }
160
161         LOG.info("ElanServiceProvider stopped");
162     }
163
164     @Override
165     // Confusing with isOpenstackVniSemanticsEnforced but this is an interface method so can't change it.
166     @SuppressFBWarnings("NM_CONFUSING")
167     public Boolean isOpenStackVniSemanticsEnforced() {
168         return elanUtils.isOpenstackVniSemanticsEnforced();
169     }
170
171     private void createIdPool() throws Exception {
172         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
173                 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
174         Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
175         if (result != null && result.get().isSuccessful()) {
176             LOG.debug("ELAN Id Pool is created successfully");
177         }
178     }
179
180     @Override
181     public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
182         Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
183         boolean isSuccess = true;
184         if (existingElanInstance.isPresent()) {
185             if (compareWithExistingElanInstance(existingElanInstance.get(), macTimeout, description)) {
186                 LOG.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
187                 return true;
188             } else {
189                 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
190                         .setDescription(description).setMacTimeout(macTimeout)
191                         .withKey(new ElanInstanceKey(elanInstanceName)).build();
192                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
193                         ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
194                 LOG.debug("Updating the Elan Instance {} with MAC TIME-OUT {} and Description {}",
195                         updateElanInstance, macTimeout, description);
196             }
197         } else {
198             ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
199                     .setMacTimeout(macTimeout).setDescription(description)
200                     .withKey(new ElanInstanceKey(elanInstanceName)).build();
201             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
202                     ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
203             LOG.debug("Creating the new Elan Instance {}", elanInstance);
204         }
205         return isSuccess;
206     }
207
208     @Override
209     public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
210         Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
211         boolean isSuccess = true;
212         if (existingElanInstance.isPresent()) {
213             if (compareWithExistingElanInstance(existingElanInstance.get(), macTimeout, description)) {
214                 LOG.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
215                 return true;
216             } else {
217                 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
218                 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
219                         .setDescription(description).setMacTimeout(macTimeout)
220                         .withKey(new ElanInstanceKey(elanInstanceName))
221                         .addAugmentation(EtreeInstance.class, etreeInstance).build();
222                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
223                         ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
224                 LOG.debug("Updating the Etree Instance {} with MAC TIME-OUT {} and Description {} ",
225                         updateElanInstance, macTimeout, description);
226             }
227         } else {
228             EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
229             ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
230                     .setMacTimeout(macTimeout).setDescription(description)
231                     .withKey(new ElanInstanceKey(elanInstanceName))
232                     .addAugmentation(EtreeInstance.class, etreeInstance).build();
233             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
234                     ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
235             LOG.debug("Creating the new Etree Instance {}", elanInstance);
236         }
237         return isSuccess;
238     }
239
240     @Override
241     @Nullable
242     public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
243         return elanInterfaceCache.getEtreeInterface(elanInterface).orElse(null);
244     }
245
246     public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
247             String description) {
248         boolean isEqual = false;
249         if (existingElanInstance.getMacTimeout().longValue() == macTimeOut
250                 && Objects.equals(existingElanInstance.getDescription(), description)) {
251             isEqual = true;
252         }
253         return isEqual;
254     }
255
256     @Override
257     public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
258         createElanInstance(elanInstanceName, newMacTimout, newDescription);
259     }
260
261     @Override
262     public boolean deleteEtreeInstance(String etreeInstanceName) {
263         return deleteElanInstance(etreeInstanceName);
264     }
265
266     @Override
267     public boolean deleteElanInstance(String elanInstanceName) {
268         Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
269         if (!existingElanInstance.isPresent()) {
270             LOG.debug("Elan Instance is not present for {}", elanInstanceName);
271             return false;
272         }
273         LOG.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
274         ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
275                 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
276         return true;
277     }
278
279     @Override
280     public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
281             List<String> staticMacAddresses, String description) {
282         Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(etreeInstanceName);
283         if (existingElanInstance.isPresent()
284                 && existingElanInstance.get().augmentation(EtreeInstance.class) != null) {
285             EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
286             ElanInterface elanInterface;
287             if (staticMacAddresses == null) {
288                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
289                         .setDescription(description).setName(interfaceName).withKey(new ElanInterfaceKey(interfaceName))
290                         .addAugmentation(EtreeInterface.class, etreeInterface).build();
291             } else {
292                 List<StaticMacEntries> staticMacEntries = ElanUtils.getStaticMacEntries(staticMacAddresses);
293                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
294                         .setDescription(description).setName(interfaceName)
295                         .setStaticMacEntries(staticMacEntries)
296                         .withKey(new ElanInterfaceKey(interfaceName))
297                         .addAugmentation(EtreeInterface.class, etreeInterface).build();
298             }
299             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
300                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
301             LOG.debug("Creating the new Etree Interface {}", elanInterface);
302         }
303     }
304
305     @Override
306     public void addElanInterface(String elanInstanceName, String interfaceName,
307             @Nullable List<String> staticMacAddresses, @Nullable String description) {
308         Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
309         if (existingElanInstance.isPresent()) {
310             ElanInterfaceBuilder elanInterfaceBuilder = new ElanInterfaceBuilder()
311                     .setElanInstanceName(elanInstanceName)
312                     .setDescription(description).setName(interfaceName)
313                     .withKey(new ElanInterfaceKey(interfaceName));
314             if (staticMacAddresses != null) {
315                 List<StaticMacEntries> staticMacEntries = ElanUtils.getStaticMacEntries(staticMacAddresses);
316                 elanInterfaceBuilder.setStaticMacEntries(staticMacEntries);
317             }
318             ElanInterface elanInterface = elanInterfaceBuilder.build();
319             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
320                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
321             LOG.debug("Created the new ELan Interface {}", elanInterface);
322         }
323     }
324
325     @Override
326     public void updateElanInterface(String elanInstanceName, String interfaceName,
327             List<String> updatedStaticMacAddresses, String newDescription) {
328         Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
329         if (!existingElanInterface.isPresent()) {
330             return;
331         }
332
333         List<StaticMacEntries> updatedStaticMacEntries = ElanUtils.getStaticMacEntries(updatedStaticMacAddresses);
334         LOG.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
335         ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
336                 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedStaticMacEntries)
337                 .withKey(new ElanInterfaceKey(interfaceName)).build();
338         MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
339                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
340     }
341
342     @Override
343     public void deleteEtreeInterface(String interfaceName) {
344         deleteElanInterface(interfaceName);
345         LOG.debug("deleting the Etree Interface {}", interfaceName);
346     }
347
348     @Override
349     public void deleteElanInterface(String interfaceName) {
350         Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
351         if (existingElanInterface.isPresent()) {
352             ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
353                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
354             LOG.debug("deleting the Elan Interface {}", existingElanInterface);
355         }
356     }
357
358     @Override
359     public void addStaticMacAddress(String interfaceName, String macAddress) {
360         Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
361         if (existingElanInterface.isPresent()) {
362             StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
363             StaticMacEntries staticMacEntry = staticMacEntriesBuilder.setMacAddress(
364                     new PhysAddress(macAddress)).build();
365             InstanceIdentifier<StaticMacEntries> staticMacEntriesIdentifier =
366                     ElanUtils.getStaticMacEntriesCfgDataPathIdentifier(interfaceName, macAddress);
367             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, staticMacEntriesIdentifier, staticMacEntry);
368         }
369     }
370
371     @Override
372     public void deleteStaticMacAddress(String interfaceName, String macAddress) {
373         Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
374         if (existingElanInterface.isPresent()) {
375             InstanceIdentifier<StaticMacEntries> staticMacEntriesIdentifier =
376                     ElanUtils.getStaticMacEntriesCfgDataPathIdentifier(interfaceName,
377                     macAddress);
378             MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, staticMacEntriesIdentifier);
379         }
380     }
381
382     @Override
383     public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
384         Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
385         List<MacEntry> macAddress = new ArrayList<>();
386         if (elanInfo == null) {
387             return macAddress;
388         }
389         List<String> elanInterfaces = elanInfo.getElanInterfaces();
390         if (elanInterfaces != null && elanInterfaces.size() > 0) {
391             for (String elanInterface : elanInterfaces) {
392                 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
393                 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
394                         && elanInterfaceMac.getMacEntry().size() > 0) {
395                     macAddress.addAll(elanInterfaceMac.getMacEntry());
396                 }
397             }
398         }
399         return macAddress;
400     }
401
402     @Override
403     public void flushMACTable(String elanInstanceName) {
404         Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
405         if (elanInfo == null) {
406             return;
407         }
408         List<String> elanInterfaces = elanInfo.getElanInterfaces();
409         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
410             return;
411         }
412         for (String elanInterface : elanInterfaces) {
413             ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
414             if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
415                 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
416                 for (MacEntry macEntry : macEntries) {
417                     deleteStaticMacAddress(elanInterface, macEntry.getMacAddress().getValue());
418                 }
419             }
420         }
421
422     }
423
424     @Override
425     @Nullable
426     public ElanInstance getElanInstance(String elanName) {
427         return elanInstanceCache.get(elanName).orElse(null);
428     }
429
430     @Override
431     public List<ElanInstance> getElanInstances() {
432         InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
433                 .build();
434         return ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanInstancesIdentifier).map(
435                 ElanInstances::getElanInstance).orElse(emptyList());
436     }
437
438     @Override
439     @NonNull
440     public List<String> getElanInterfaces(String elanInstanceName) {
441         List<String> elanInterfaces = new ArrayList<>();
442         InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
443                 .build();
444         Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
445                 elanInterfacesIdentifier);
446         if (!elanInterfacesOptional.isPresent()) {
447             return elanInterfaces;
448         }
449         List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().nonnullElanInterface();
450         for (ElanInterface elanInterface : elanInterfaceList) {
451             if (Objects.equals(elanInterface.getElanInstanceName(), elanInstanceName)) {
452                 elanInterfaces.add(elanInterface.getName());
453             }
454         }
455         return elanInterfaces;
456     }
457
458     @Override
459     public void createExternalElanNetworks(Node node) {
460         handleExternalElanNetworks(node, true, (elanInstance, interfaceName) -> {
461             createExternalElanNetwork(elanInstance, interfaceName);
462             return null;
463         });
464     }
465
466     @Override
467     public void createExternalElanNetwork(ElanInstance elanInstance) {
468         handleExternalElanNetwork(elanInstance, false, (elanInstance1, interfaceName) -> {
469             createExternalElanNetwork(elanInstance1, interfaceName);
470             return null;
471         });
472     }
473
474     protected void createExternalElanNetwork(ElanInstance elanInstance, Uint64 dpId) {
475         String providerIntfName = bridgeMgr.getProviderInterfaceName(dpId, elanInstance.getPhysicalNetworkName());
476         String intfName = providerIntfName + IfmConstants.OF_URI_SEPARATOR + elanInstance.getSegmentationId();
477         Interface memberIntf = interfaceManager.getInterfaceInfoFromConfigDataStore(intfName);
478         if (memberIntf == null) {
479             LOG.debug("creating vlan prv intf in elan {}, dpn {}", elanInstance.getElanInstanceName(),
480                     dpId);
481             createExternalElanNetwork(elanInstance, providerIntfName);
482         }
483     }
484
485     private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
486         if (interfaceName == null) {
487             LOG.trace("No physical interface is attached to {}", elanInstance.getPhysicalNetworkName());
488             return;
489         }
490
491         String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
492         addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
493     }
494
495     @Override
496     public void updateExternalElanNetwork(ElanInstance elanInstance) {
497         handleExternalElanNetwork(elanInstance, true, (elanInstance1, interfaceName) -> {
498             createExternalElanNetwork(elanInstance1, interfaceName);
499             return null;
500         });
501     }
502
503     @Override
504     public void deleteExternalElanNetworks(Node node) {
505         handleExternalElanNetworks(node, false, (elanInstance, interfaceName) -> {
506             deleteExternalElanNetwork(elanInstance, interfaceName);
507             return null;
508         });
509     }
510
511     @Override
512     public void deleteExternalElanNetwork(ElanInstance elanInstance) {
513         handleExternalElanNetwork(elanInstance, false, (elanInstance1, interfaceName) -> {
514             deleteExternalElanNetwork(elanInstance1, interfaceName);
515             return null;
516         });
517     }
518
519     protected void deleteExternalElanNetwork(ElanInstance elanInstance, Uint64 dpnId) {
520         String providerIntfName = bridgeMgr.getProviderInterfaceName(dpnId, elanInstance.getPhysicalNetworkName());
521         String intfName = providerIntfName + IfmConstants.OF_URI_SEPARATOR + elanInstance.getSegmentationId();
522         Interface memberIntf = interfaceManager.getInterfaceInfoFromConfigDataStore(intfName);
523         if (memberIntf != null) {
524             deleteElanInterface(intfName);
525             deleteIetfInterface(intfName);
526             LOG.debug("delete vlan prv intf {} in elan {}, dpID {}", intfName,
527                     elanInstance.getElanInstanceName(), dpnId);
528         } else {
529             LOG.debug("vlan prv intf {} not found in interfacemgr config DS", intfName);
530         }
531     }
532
533     private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
534         if (interfaceName == null) {
535             LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
536             return;
537         }
538
539         String elanInstanceName = elanInstance.getElanInstanceName();
540         for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
541             if (elanInterface.startsWith(interfaceName)) {
542                 if (ElanUtils.isVlan(elanInstance)) {
543                     deleteIetfInterface(elanInterface);
544                 }
545                 String trunkInterfaceName = getTrunkInterfaceName(interfaceName);
546                 if (shouldDeleteTrunk(trunkInterfaceName, elanInterface)) {
547                     deleteIetfInterface(trunkInterfaceName);
548                 }
549                 deleteElanInterface(elanInterface);
550             }
551         }
552     }
553
554     private boolean shouldDeleteTrunk(String trunkInterfaceName, String elanInterfaceName) {
555         List<Interface> childInterfaces = interfaceManager.getChildInterfaces(trunkInterfaceName);
556         if (childInterfaces == null || childInterfaces.isEmpty()
557                 || childInterfaces.size() == 1 && elanInterfaceName.equals(childInterfaces.get(0).getName())) {
558             LOG.debug("No more VLAN member interfaces left for trunk {}", trunkInterfaceName);
559             return true;
560         }
561
562         LOG.debug("Trunk interface {} has {} VLAN member interfaces left", trunkInterfaceName, childInterfaces.size());
563         return false;
564     }
565
566     @Override
567     public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
568         if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
569             return;
570         }
571
572         List<ElanInstance> elanInstances = getElanInstances();
573         if (elanInstances == null || elanInstances.isEmpty()) {
574             LOG.trace("No ELAN instances found");
575             return;
576         }
577
578         LOG.trace("updateExternalElanNetworks, orig bridge {} . updated bridge {}", origNode, updatedNode);
579
580         Map<String, String> origProviderMappping = getMapFromOtherConfig(origNode,
581                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
582         Map<String, String> updatedProviderMappping = getMapFromOtherConfig(updatedNode,
583                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
584
585         boolean hasDatapathIdOnOrigNode = bridgeMgr.hasDatapathID(origNode);
586         boolean hasDatapathIdOnUpdatedNode = bridgeMgr.hasDatapathID(updatedNode);
587         Uint64 origDpnID = bridgeMgr.getDatapathId(origNode);
588
589         for (ElanInstance elanInstance : elanInstances) {
590             String physicalNetworkName = elanInstance.getPhysicalNetworkName();
591             boolean createExternalElanNw = true;
592             if (physicalNetworkName != null) {
593                 String origPortName = origProviderMappping.get(physicalNetworkName);
594                 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
595                 /**
596                  * for internal vlan network, vlan provider interface creation should be
597                  * triggered only if there is existing vlan provider intf indicating presence
598                  * of VM ports on the DPN
599                  */
600                 if (hasDatapathIdOnOrigNode && !elanInstance.isExternal()
601                         && ElanUtils.isVlan(elanInstance)) {
602                     String externalIntf = getExternalElanInterface(elanInstance.getElanInstanceName(),
603                             origDpnID);
604                     if (externalIntf == null) {
605                         createExternalElanNw = false;
606                     }
607                 }
608                 if (hasPortNameRemoved(origPortName, updatedPortName)) {
609                     deleteExternalElanNetwork(elanInstance,
610                             bridgeMgr.getProviderInterfaceName(origNode, physicalNetworkName));
611                 }
612
613                 if (createExternalElanNw && (hasPortNameUpdated(origPortName, updatedPortName)
614                         || hasDatapathIdAdded(hasDatapathIdOnOrigNode, hasDatapathIdOnUpdatedNode))) {
615                     createExternalElanNetwork(elanInstance,
616                             bridgeMgr.getProviderInterfaceName(updatedNode, physicalNetworkName));
617                 }
618             }
619         }
620     }
621
622     private static boolean hasDatapathIdAdded(boolean hasDatapathIdOnOrigNode, boolean hasDatapathIdOnUpdatedNode) {
623         return !hasDatapathIdOnOrigNode && hasDatapathIdOnUpdatedNode;
624     }
625
626     private static boolean hasPortNameUpdated(String origPortName, String updatedPortName) {
627         return updatedPortName != null && !updatedPortName.equals(origPortName);
628     }
629
630     private static boolean hasPortNameRemoved(String origPortName, String updatedPortName) {
631         return origPortName != null && !origPortName.equals(updatedPortName);
632     }
633
634     private Map<String, String> getMapFromOtherConfig(Node node, String otherConfigColumn) {
635         return bridgeMgr.getOpenvswitchOtherConfigMap(node, otherConfigColumn);
636     }
637
638     @Override
639     public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
640         List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
641         if (elanInterfaces.isEmpty()) {
642             LOG.trace("No ELAN interfaces defined for {}", elanInstanceName);
643             return Collections.emptySet();
644         }
645
646         Set<String> externalElanInterfaces = new HashSet<>();
647         for (String elanInterface : elanInterfaces) {
648             if (interfaceManager.isExternalInterface(elanInterface)) {
649                 externalElanInterfaces.add(elanInterface);
650             }
651         }
652
653         return externalElanInterfaces;
654     }
655
656     @Override
657     public String getExternalElanInterface(String elanInstanceName, Uint64 dpnId) {
658         return elanUtils.getExternalElanInterface(elanInstanceName, dpnId);
659     }
660
661     @Override
662     public boolean isExternalInterface(String interfaceName) {
663         return interfaceManager.isExternalInterface(interfaceName);
664     }
665
666     @Override
667     @Nullable
668     public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
669         return elanInterfaceCache.get(interfaceName).orElse(null);
670     }
671
672     @Override
673     public void handleKnownL3DmacAddress(String macAddress, String elanInstanceName, int addOrRemove) {
674         if (addOrRemove == NwConstants.ADD_FLOW) {
675             addKnownL3DmacAddress(macAddress, elanInstanceName);
676         } else {
677             removeKnownL3DmacAddress(macAddress, elanInstanceName);
678         }
679     }
680
681     @Override
682     public void addKnownL3DmacAddress(String macAddress, String elanInstanceName) {
683         if (!isL2BeforeL3) {
684             LOG.trace("ELAN service is after L3VPN in the Netvirt pipeline skip known L3DMAC flows installation");
685             return;
686         }
687         ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
688         if (elanInstance == null) {
689             LOG.warn("Null elan instance {}", elanInstanceName);
690             return;
691         }
692
693         List<Uint64> dpnsIdsForElanInstance = elanUtils.getParticipatingDpnsInElanInstance(elanInstanceName);
694         if (dpnsIdsForElanInstance.isEmpty()) {
695             LOG.warn("No DPNs for elan instance {}", elanInstance);
696             return;
697         }
698
699         elanUtils.addDmacRedirectToDispatcherFlows(elanInstance.getElanTag(), elanInstanceName, macAddress,
700                 dpnsIdsForElanInstance);
701     }
702
703     @Override
704     public void removeKnownL3DmacAddress(String macAddress, String elanInstanceName) {
705         if (!isL2BeforeL3) {
706             LOG.trace("ELAN service is after L3VPN in the Netvirt pipeline skip known L3DMAC flows installation");
707             return;
708         }
709         ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
710         if (elanInstance == null) {
711             LOG.warn("Null elan instance {}", elanInstanceName);
712             return;
713         }
714
715         List<Uint64> dpnsIdsForElanInstance = elanUtils.getParticipatingDpnsInElanInstance(elanInstanceName);
716         if (dpnsIdsForElanInstance.isEmpty()) {
717             LOG.warn("No DPNs for elan instance {}", elanInstance);
718             return;
719         }
720
721         elanUtils.removeDmacRedirectToDispatcherFlows(elanInstance.getElanTag(),
722             macAddress, dpnsIdsForElanInstance);
723     }
724
725     /**
726      * Create ietf-interfaces based on the ELAN segment type.<br>
727      * For segment type flat - create transparent interface pointing to the
728      * patch-port attached to the physnet port.<br>
729      * For segment type vlan - create trunk interface pointing to the patch-port
730      * attached to the physnet port + trunk-member interface pointing to the
731      * trunk interface.
732      *
733      * @param elanInstance
734      *            ELAN instance
735      * @param parentRef
736      *            parent interface name
737      * @return the name of the interface to be added to the ELAN instance i.e.
738      *         trunk-member name for vlan network and transparent for flat
739      *         network or null otherwise
740      */
741     private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
742         String interfaceName = null;
743
744         try {
745             String trunkName = getTrunkInterfaceName(parentRef);
746             // trunk interface may have been created by other vlan network
747             Interface trunkInterface = interfaceManager.getInterfaceInfoFromConfigDataStore(trunkName);
748             if (trunkInterface == null) {
749                 interfaceManager.createVLANInterface(trunkName, parentRef, null, null,
750                         IfL2vlan.L2vlanMode.Trunk, true);
751             }
752             if (ElanUtils.isFlat(elanInstance)) {
753                 interfaceName = trunkName;
754             } else if (ElanUtils.isVlan(elanInstance)) {
755                 Long segmentationId = elanInstance.getSegmentationId().toJava();
756                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
757                 interfaceManager.createVLANInterface(interfaceName, trunkName, segmentationId.intValue(), null,
758                         IfL2vlan.L2vlanMode.TrunkMember, true);
759             }
760         } catch (InterfaceAlreadyExistsException e) {
761             LOG.trace("Interface {} was already created", interfaceName);
762         }
763
764         return interfaceName;
765     }
766
767     private void deleteIetfInterface(String interfaceName) {
768         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
769         InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
770                 .child(Interface.class, interfaceKey).build();
771         MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
772         LOG.debug("Deleting IETF interface {}", interfaceName);
773     }
774
775     private void handleExternalElanNetworks(Node node, boolean skipIntVlanNw,
776                                             BiFunction<ElanInstance, String, Void> function) {
777         if (!bridgeMgr.isIntegrationBridge(node)) {
778             return;
779         }
780
781         List<ElanInstance> elanInstances = getElanInstances();
782         if (elanInstances == null || elanInstances.isEmpty()) {
783             LOG.trace("No ELAN instances found");
784             return;
785         }
786
787         for (ElanInstance elanInstance : elanInstances) {
788             if (skipIntVlanNw && !elanInstance.isExternal() && ElanUtils.isVlan(elanInstance)) {
789                 continue;
790             }
791             String interfaceName = bridgeMgr.getProviderInterfaceName(node, elanInstance.getPhysicalNetworkName());
792             if (interfaceName != null) {
793                 function.apply(elanInstance, interfaceName);
794             }
795         }
796     }
797
798     private void handleExternalElanNetwork(ElanInstance elanInstance, boolean update,
799                                            BiFunction<ElanInstance, String, Void> function) {
800         String elanInstanceName = elanInstance.getElanInstanceName();
801         boolean isFlatOrVlanNetwork = (ElanUtils.isFlat(elanInstance) || ElanUtils.isVlan(elanInstance));
802         if (!isFlatOrVlanNetwork) {
803             LOG.error("Network is not of type FLAT/VLAN."
804                     + "Ignoring Elan-interface creation for given ProviderInterface {}",
805                 elanInstance.getPhysicalNetworkName());
806             return;
807         }
808         if (elanInstance.getPhysicalNetworkName() == null) {
809             LOG.trace("No physical network attached to {}", elanInstanceName);
810             return;
811         }
812
813         List<Node> nodes = southboundUtils.getOvsdbNodes();
814         if (nodes == null || nodes.isEmpty()) {
815             LOG.trace("No OVS nodes found while creating external network for ELAN {}",
816                     elanInstance.getElanInstanceName());
817             return;
818         }
819
820         for (Node node : nodes) {
821             if (bridgeMgr.isIntegrationBridge(node)) {
822                 if (update && !elanInstance.isExternal()) {
823                     DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName,
824                             bridgeMgr.getDatapathId(node));
825                     if (dpnInterfaces == null || dpnInterfaces.getInterfaces().isEmpty()) {
826                         continue;
827                     }
828                 }
829                 String interfaceName = bridgeMgr.getProviderInterfaceName(node, elanInstance.getPhysicalNetworkName());
830                 function.apply(elanInstance, interfaceName);
831             }
832         }
833     }
834
835     private static String getTrunkInterfaceName(String parentRef) {
836         return parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
837     }
838
839     private void setIsL2BeforeL3() {
840         short elanServiceRealIndex = ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME,
841                 NwConstants.ELAN_SERVICE_INDEX);
842         short l3vpnServiceRealIndex = ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME,
843                 NwConstants.L3VPN_SERVICE_INDEX);
844         if (elanServiceRealIndex < l3vpnServiceRealIndex) {
845             LOG.info("ELAN service is set before L3VPN service in the Netvirt pipeline");
846             isL2BeforeL3 = true;
847         } else {
848             LOG.info("ELAN service is set after L3VPN service in the Netvirt pipeline");
849             isL2BeforeL3 = false;
850         }
851     }
852
853     @Override
854     public void addArpResponderFlow(ArpResponderInput arpResponderInput) {
855         String ingressInterfaceName = arpResponderInput.getInterfaceName();
856         String macAddress = arpResponderInput.getSha();
857         String ipAddress = arpResponderInput.getSpa();
858         int lportTag = arpResponderInput.getLportTag();
859         Uint64 dpnId = Uint64.valueOf(arpResponderInput.getDpId());
860
861         LOG.info("Installing the ARP responder flow on DPN {} for Interface {} with MAC {} & IP {}", dpnId,
862                 ingressInterfaceName, macAddress, ipAddress);
863         Optional<ElanInterface> elanIface = elanInterfaceCache.get(ingressInterfaceName);
864         ElanInstance elanInstance = elanIface.isPresent()
865                 ? elanInstanceCache.get(elanIface.get().getElanInstanceName()).orElse(null) : null;
866         if (elanInstance == null) {
867             LOG.debug("addArpResponderFlow: elanInstance is null, Failed to install arp responder flow for dpnId {}"
868                     + "for Interface {} with MAC {} & IP {}", dpnId, ingressInterfaceName, macAddress, ipAddress);
869             return;
870         }
871         String flowId = ArpResponderUtil.getFlowId(lportTag, ipAddress);
872         Flow flowEntity =
873             MDSALUtil.buildFlowNew(NwConstants.ARP_RESPONDER_TABLE, flowId, NwConstants.DEFAULT_ARP_FLOW_PRIORITY,
874                 flowId, 0, 0,
875                 ArpResponderUtil.generateCookie(lportTag, ipAddress),
876                 ArpResponderUtil.getMatchCriteria(lportTag, elanInstance, ipAddress),
877                 arpResponderInput.getInstructions());
878         LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
879             tx -> mdsalManager.addFlow(tx, dpnId, flowEntity)), LOG, "Error adding flow {}", flowEntity);
880         LOG.info("Installed the ARP Responder flow for Interface {}", ingressInterfaceName);
881     }
882
883     @Override
884     public void addExternalTunnelArpResponderFlow(ArpResponderInput arpResponderInput, String elanInstanceName) {
885         Uint64 dpnId = Uint64.valueOf(arpResponderInput.getDpId());
886         String ipAddress = arpResponderInput.getSpa();
887         String macAddress = arpResponderInput.getSha();
888
889         LOG.trace("Installing the ExternalTunnel ARP responder flow on DPN {} for ElanInstance {} with MAC {} & IP {}",
890                 dpnId, elanInstanceName, macAddress, ipAddress);
891
892         ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
893         if (elanInstance == null) {
894             LOG.warn("Null elan instance {}", elanInstanceName);
895             return;
896         }
897
898         int lportTag = arpResponderInput.getLportTag();
899         String flowId = ArpResponderUtil.getFlowId(lportTag, ipAddress);
900         Flow flowEntity =
901             MDSALUtil.buildFlowNew(NwConstants.ARP_RESPONDER_TABLE, flowId, NwConstants.DEFAULT_ARP_FLOW_PRIORITY,
902                 flowId, 0, 0,
903                 ArpResponderUtil.generateCookie(lportTag, ipAddress),
904                 ArpResponderUtil.getMatchCriteria(lportTag, elanInstance, ipAddress),
905                 arpResponderInput.getInstructions());
906         LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
907             tx -> mdsalManager.addFlow(tx, dpnId, flowEntity)), LOG, "Error adding flow {}", flowEntity);
908         LOG.trace("Installed the ExternalTunnel ARP Responder flow for ElanInstance {}", elanInstanceName);
909     }
910
911     @Override
912     public void removeArpResponderFlow(ArpResponderInput arpResponderInput) {
913         elanUtils.removeArpResponderFlow(Uint64.valueOf(arpResponderInput.getDpId()),
914                 arpResponderInput.getInterfaceName(),
915                 arpResponderInput.getSpa(), arpResponderInput.getLportTag());
916     }
917
918     /**
919      * Uses the IdManager to retrieve a brand new ElanTag.
920      *
921      * @param idKey
922      *            the id key
923      * @return the integer
924      */
925     @Override
926     public Uint32 retrieveNewElanTag(String idKey) {
927         return ElanUtils.retrieveNewElanTag(idManager, idKey);
928     }
929
930     @Override
931     public InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(
932                                                                 String elanInstanceName, Uint64 dpnId) {
933         return ElanUtils.getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpnId);
934     }
935
936     @Override
937     public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, Uint64 dpId) {
938         return elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpId);
939     }
940
941 }