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