Merge "Fixes Bug 6469 "
[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 java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.Future;
20 import java.util.function.BiFunction;
21
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
27 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
28 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.genius.utils.clustering.EntityOwnerUtils;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
32 import org.opendaylight.netvirt.elan.statusanddiag.ElanStatusMonitor;
33 import org.opendaylight.netvirt.elan.utils.ElanConstants;
34 import org.opendaylight.netvirt.elan.utils.ElanUtils;
35 import org.opendaylight.netvirt.elanmanager.api.IElanService;
36 import org.opendaylight.netvirt.elanmanager.exceptions.MacNotFoundException;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 import com.google.common.base.Optional;
69
70 public class ElanServiceProvider implements IElanService {
71
72     private static final Logger LOG = LoggerFactory.getLogger(ElanServiceProvider.class);
73
74     private final IdManagerService idManager;
75     private final IInterfaceManager interfaceManager;
76     private final ElanInstanceManager elanInstanceManager;
77     private final ElanBridgeManager bridgeMgr;
78     private final DataBroker broker;
79     private final ElanStatusMonitor elanStatusMonitor;
80     private static ElanUtils elanUtils;
81
82     private boolean generateIntBridgeMac = true;
83
84     public ElanServiceProvider(IdManagerService idManager, IInterfaceManager interfaceManager,
85                                ElanInstanceManager elanInstanceManager, ElanBridgeManager bridgeMgr,
86                                DataBroker dataBroker,
87                                ElanInterfaceManager elanInterfaceManager,
88                                ElanStatusMonitor elanStatusMonitor, ElanUtils elanUtils, EntityOwnershipService entityOwnershipService) {
89         this.idManager = idManager;
90         this.interfaceManager = interfaceManager;
91         this.elanInstanceManager = elanInstanceManager;
92         this.bridgeMgr = bridgeMgr;
93         this.broker = dataBroker;
94         this.elanStatusMonitor = elanStatusMonitor;
95         this.elanUtils = elanUtils;
96         elanInterfaceManager.setElanUtils(elanUtils);
97         try {
98             EntityOwnerUtils.registerEntityCandidateForOwnerShip(entityOwnershipService,
99                     HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
100                     null/*listener*/);
101         } catch (CandidateAlreadyRegisteredException e) {
102             LOG.error("failed to register the entity");
103         }
104     }
105
106     public void init() {
107         LOG.info("Starting ElnaServiceProvider");
108         elanStatusMonitor.reportStatus("STARTING");
109         try {
110             createIdPool();
111
112             elanStatusMonitor.reportStatus("OPERATIONAL");
113         } catch (Exception e) {
114             LOG.error("Error initializing services", e);
115             elanStatusMonitor.reportStatus("ERROR");
116         }
117     }
118
119     private void createIdPool() {
120         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
121                 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
122         try {
123             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
124             if (result != null && result.get().isSuccessful()) {
125                 LOG.debug("ELAN Id Pool is created successfully");
126             }
127         } catch (Exception e) {
128             LOG.error("Failed to create ELAN Id pool {}", e);
129         }
130     }
131
132     @Override
133     public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
134         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
135         boolean isSuccess = true;
136         if (existingElanInstance != null) {
137             if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
138                 LOG.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
139                 return true;
140             } else {
141                 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
142                         .setDescription(description).setMacTimeout(macTimeout)
143                         .setKey(new ElanInstanceKey(elanInstanceName)).build();
144                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
145                         ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
146                 LOG.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
147                         updateElanInstance, macTimeout, description);
148             }
149         } else {
150             ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
151                     .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
152                     .build();
153             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
154                     ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
155             LOG.debug("Creating the new Elan Instance {}", elanInstance);
156         }
157         return isSuccess;
158     }
159
160     @Override
161     public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
162         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
163         boolean isSuccess = true;
164         if (existingElanInstance != null) {
165             if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
166                 LOG.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
167                 return true;
168             } else {
169                 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
170                 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
171                         .setDescription(description).setMacTimeout(macTimeout)
172                         .setKey(new ElanInstanceKey(elanInstanceName))
173                         .addAugmentation(EtreeInstance.class, etreeInstance).build();
174                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
175                         ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
176                 LOG.debug("Updating the Etree Instance {} with MAC TIME-OUT %l and Description %s ",
177                         updateElanInstance, macTimeout, description);
178             }
179         } else {
180             EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
181             ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
182                     .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
183                     .addAugmentation(EtreeInstance.class, etreeInstance).build();
184             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
185                     ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
186             LOG.debug("Creating the new Etree Instance {}", elanInstance);
187         }
188         return isSuccess;
189     }
190
191     @Override
192     public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
193         return ElanUtils.getEtreeInterfaceByElanInterfaceName(broker, elanInterface);
194     }
195
196     public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
197             String description) {
198         boolean isEqual = false;
199         if (existingElanInstance.getMacTimeout() == macTimeOut
200                 && existingElanInstance.getDescription().equals(description)) {
201             isEqual = true;
202         }
203         return isEqual;
204     }
205
206     @Override
207     public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
208         createElanInstance(elanInstanceName, newMacTimout, newDescription);
209     }
210
211     @Override
212     public boolean deleteEtreeInstance(String etreeInstanceName) {
213         return deleteElanInstance(etreeInstanceName);
214     }
215
216     @Override
217     public boolean deleteElanInstance(String elanInstanceName) {
218         boolean isSuccess = false;
219         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
220         if (existingElanInstance == null) {
221             LOG.debug("Elan Instance is not present {}", existingElanInstance);
222             return isSuccess;
223         }
224         LOG.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
225         ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
226                 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
227         isSuccess = true;
228         return isSuccess;
229     }
230
231     @Override
232     public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
233             List<String> staticMacAddresses, String description) {
234         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(etreeInstanceName);
235         if (existingElanInstance != null && existingElanInstance.getAugmentation(EtreeInstance.class) != null) {
236             EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
237             ElanInterface elanInterface;
238             if (staticMacAddresses == null) {
239                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
240                         .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
241                         .addAugmentation(EtreeInterface.class, etreeInterface).build();
242             } else {
243                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
244                         .setDescription(description).setName(interfaceName)
245                         .setStaticMacEntries(getPhysAddress(staticMacAddresses))
246                         .setKey(new ElanInterfaceKey(interfaceName))
247                         .addAugmentation(EtreeInterface.class, etreeInterface).build();
248             }
249             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
250                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
251             LOG.debug("Creating the new Etree Interface {}", elanInterface);
252         }
253     }
254
255     @Override
256     public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
257             String description) {
258         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
259         if (existingElanInstance != null) {
260             ElanInterface elanInterface;
261             if (staticMacAddresses == null) {
262                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
263                         .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
264                         .build();
265             } else {
266                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
267                         .setDescription(description).setName(interfaceName)
268                         .setStaticMacEntries(getPhysAddress(staticMacAddresses))
269                         .setKey(new ElanInterfaceKey(interfaceName)).build();
270             }
271             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
272                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
273             LOG.debug("Creating the new ELan Interface {}", elanInterface);
274         }
275     }
276
277     @Override
278     public void updateElanInterface(String elanInstanceName, String interfaceName,
279             List<String> updatedStaticMacAddresses, String newDescription) {
280         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
281         if (existingElanInterface == null) {
282             return;
283         }
284         List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
285         List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
286         List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
287         if (updatedPhysAddress.size() > 0) {
288             LOG.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
289             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
290                     .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
291                     .setKey(new ElanInterfaceKey(interfaceName)).build();
292             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
293                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
294         }
295     }
296
297     @Override
298     public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
299         deleteElanInterface(elanInstanceName, interfaceName);
300         LOG.debug("deleting the Etree Interface {}", interfaceName);
301     }
302
303     @Override
304     public void deleteElanInterface(String elanInstanceName, String interfaceName) {
305         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
306         if (existingElanInterface != null) {
307             ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
308                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
309             LOG.debug("deleting the Elan Interface {}", existingElanInterface);
310         }
311     }
312
313     @Override
314     public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
315         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
316         PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
317         if (existingElanInterface != null) {
318             List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
319             if (existingMacAddress.contains(updateStaticMacAddress)) {
320                 return;
321             }
322             existingMacAddress.add(updateStaticMacAddress);
323             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
324                     .setName(interfaceName).setStaticMacEntries(existingMacAddress)
325                     .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
326                     .build();
327             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
328                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
329         }
330     }
331
332     @Override
333     public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
334             throws MacNotFoundException {
335         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
336         PhysAddress physAddress = new PhysAddress(macAddress);
337         if (existingElanInterface == null) {
338             return;
339         }
340         List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
341         if (existingMacAddress.contains(physAddress)) {
342             existingMacAddress.remove(physAddress);
343             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
344                     .setName(interfaceName).setStaticMacEntries(existingMacAddress)
345                     .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
346                     .build();
347             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
348                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
349         } else {
350             throw new MacNotFoundException("deleteStaticMacAddress did not find MAC: " + macAddress);
351         }
352     }
353
354     @Override
355     public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
356         Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
357         List<MacEntry> macAddress = new ArrayList<>();
358         if (elanInfo == null) {
359             return macAddress;
360         }
361         List<String> elanInterfaces = elanInfo.getElanInterfaces();
362         if (elanInterfaces != null && elanInterfaces.size() > 0) {
363             for (String elanInterface : elanInterfaces) {
364                 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
365                 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
366                         && elanInterfaceMac.getMacEntry().size() > 0) {
367                     macAddress.addAll(elanInterfaceMac.getMacEntry());
368                 }
369             }
370         }
371         return macAddress;
372     }
373
374     @Override
375     public void flushMACTable(String elanInstanceName) {
376         Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
377         if (elanInfo == null) {
378             return;
379         }
380         List<String> elanInterfaces = elanInfo.getElanInterfaces();
381         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
382             return;
383         }
384         for (String elanInterface : elanInterfaces) {
385             ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
386             if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
387                 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
388                 for (MacEntry macEntry : macEntries) {
389                     try {
390                         deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
391                     } catch (MacNotFoundException e) {
392                         LOG.error("Mac Not Found Exception {}", e);
393                     }
394                 }
395             }
396         }
397
398     }
399
400     public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
401         List<PhysAddress> physAddresses = new ArrayList<>();
402         for (String mac : macAddress) {
403             physAddresses.add(new PhysAddress(mac));
404         }
405         return physAddresses;
406     }
407
408     public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
409             List<PhysAddress> updatePhyAddresses) {
410         if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
411             List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
412             if (originalAddresses != null && !originalAddresses.isEmpty()) {
413                 existingClonedPhyAddress.addAll(0, originalAddresses);
414                 originalAddresses.removeAll(updatePhyAddresses);
415                 updatePhyAddresses.removeAll(existingClonedPhyAddress);
416             }
417         }
418         return updatePhyAddresses;
419     }
420
421     @Override
422     public ElanInstance getElanInstance(String elanName) {
423         return ElanUtils.getElanInstanceByName(broker, elanName);
424     }
425
426     @Override
427     public List<ElanInstance> getElanInstances() {
428         List<ElanInstance> elanList = new ArrayList<>();
429         InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
430                 .build();
431         Optional<ElanInstances> elansOptional = elanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
432                 elanInstancesIdentifier);
433         if (elansOptional.isPresent()) {
434             elanList.addAll(elansOptional.get().getElanInstance());
435         }
436         return elanList;
437     }
438
439     @Override
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().getElanInterface();
450         for (ElanInterface elanInterface : elanInterfaceList) {
451             if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
452                 elanInterfaces.add(elanInterface.getName());
453             }
454         }
455         return elanInterfaces;
456     }
457
458     public boolean getGenerateIntBridgeMac() {
459         return generateIntBridgeMac;
460     }
461
462     public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
463         this.generateIntBridgeMac = generateIntBridgeMac;
464     }
465
466     @Override
467     public void createExternalElanNetworks(Node node) {
468         handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
469             createExternalElanNetwork(elanInstance, interfaceName);
470             return null;
471         });
472     }
473
474     @Override
475     public void createExternalElanNetwork(ElanInstance elanInstance) {
476         handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
477             createExternalElanNetwork(elanInstance1, interfaceName);
478             return null;
479         });
480     }
481
482     private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
483         if (interfaceName == null) {
484             LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
485             return;
486         }
487
488         String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
489         addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
490     }
491
492     @Override
493     public void deleteExternalElanNetworks(Node node) {
494         handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
495             deleteExternalElanNetwork(elanInstance, interfaceName);
496             return null;
497         });
498     }
499
500     @Override
501     public void deleteExternalElanNetwork(ElanInstance elanInstance) {
502         handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
503             deleteExternalElanNetwork(elanInstance1, interfaceName);
504             return null;
505         });
506     }
507
508     private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
509         if (interfaceName == null) {
510             LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
511             return;
512         }
513
514         String elanInstanceName = elanInstance.getElanInstanceName();
515         for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
516             if (elanInterface.startsWith(interfaceName)) {
517                 deleteIetfInterface(elanInterface);
518                 deleteElanInterface(elanInstanceName, elanInterface);
519             }
520         }
521     }
522
523     @Override
524     public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
525         if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
526             return;
527         }
528
529         List<ElanInstance> elanInstances = getElanInstances();
530         if (elanInstances == null || elanInstances.isEmpty()) {
531             LOG.trace("No ELAN instances found");
532             return;
533         }
534
535         Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
536                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
537         Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
538                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
539         Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
540         Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
541
542         for (ElanInstance elanInstance : elanInstances) {
543             String physicalNetworkName = elanInstance.getPhysicalNetworkName();
544             if (physicalNetworkName != null) {
545                 String origPortName = origProviderMappping.get(physicalNetworkName);
546                 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
547                 if (origPortName != null && !origPortName.equals(updatedPortName)) {
548                     deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
549                 }
550                 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
551                     createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
552                 }
553             }
554         }
555     }
556
557     @Override
558     public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
559         DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
560         if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
561             LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
562             return null;
563         }
564
565         for (String dpnInterface : dpnInterfaces.getInterfaces()) {
566             if (elanUtils.isExternal(dpnInterface)) {
567                 return dpnInterface;
568             }
569         }
570
571         LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
572         return null;
573     }
574
575     @Override
576     public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
577         List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
578         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
579             LOG.trace("No ELAN interfaces defined for {}", elanInstanceName);
580             return Collections.emptySet();
581         }
582
583         Set<String> externalElanInterfaces = new HashSet<>();
584         for (String elanInterface : elanInterfaces) {
585             if (elanUtils.isExternal(elanInterface)) {
586                 externalElanInterfaces.add(elanInterface);
587             }
588         }
589
590         return externalElanInterfaces;
591     }
592
593     @Override
594     public boolean isExternalInterface(String interfaceName) {
595         return elanUtils.isExternal(interfaceName);
596     }
597
598     @Override
599     public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
600         return ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
601     }
602
603     /**
604      * Create ietf-interfaces based on the ELAN segment type.<br>
605      * For segment type flat - create transparent interface pointing to the
606      * patch-port attached to the physnet port.<br>
607      * For segment type vlan - create trunk interface pointing to the patch-port
608      * attached to the physnet port + trunk-member interface pointing to the
609      * trunk interface.
610      *
611      * @param elanInstance
612      *            ELAN instance
613      * @param parentRef
614      *            parent interface name
615      * @return the name of the interface to be added to the ELAN instance i.e.
616      *         trunk-member name for vlan network and transparent for flat
617      *         network or null otherwise
618      */
619     private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
620         String interfaceName = null;
621
622         try {
623             if (ElanUtils.isFlat(elanInstance)) {
624                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
625                 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
626                         IfL2vlan.L2vlanMode.Transparent, true);
627             } else if (ElanUtils.isVlan(elanInstance)) {
628                 Long segmentationId = elanInstance.getSegmentationId();
629                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
630                 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
631                 // trunk interface may have been created by other vlan network
632                 Interface trunkInterface = ElanUtils.getInterfaceFromConfigDS(trunkName, broker);
633                 if (trunkInterface == null) {
634                     interfaceManager.createVLANInterface(trunkName, parentRef, null, null, null,
635                             IfL2vlan.L2vlanMode.Trunk, true);
636                 }
637                 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
638                         IfL2vlan.L2vlanMode.TrunkMember, true);
639             }
640         } catch (InterfaceAlreadyExistsException e) {
641             LOG.trace("Interface {} was already created", interfaceName);
642         }
643
644         return interfaceName;
645     }
646
647     private void deleteIetfInterface(String interfaceName) {
648         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
649         InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
650                 .child(Interface.class, interfaceKey).build();
651         MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
652         LOG.debug("Deleting IETF interface {}", interfaceName);
653     }
654
655     private String getExtInterfaceName(Node node, String physicalNetworkName) {
656         if (physicalNetworkName == null) {
657             return null;
658         }
659
660         String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
661         if (providerMappingValue == null) {
662             LOG.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
663                     node.getNodeId().getValue());
664             return null;
665         }
666
667         return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
668                 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
669     }
670
671     private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
672         if (!bridgeMgr.isIntegrationBridge(node)) {
673             return;
674         }
675
676         List<ElanInstance> elanInstances = getElanInstances();
677         if (elanInstances == null || elanInstances.isEmpty()) {
678             LOG.trace("No ELAN instances found");
679             return;
680         }
681
682         for (ElanInstance elanInstance : elanInstances) {
683             String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
684             if (interfaceName != null) {
685                 function.apply(elanInstance, interfaceName);
686             }
687         }
688     }
689
690     private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
691         String elanInstanceName = elanInstance.getElanInstanceName();
692         if (elanInstance.getPhysicalNetworkName() == null) {
693             LOG.trace("No physical network attached to {}", elanInstanceName);
694             return;
695         }
696
697         List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
698         if (nodes == null || nodes.isEmpty()) {
699             LOG.trace("No OVS nodes found while creating external network for ELAN {}",
700                     elanInstance.getElanInstanceName());
701             return;
702         }
703
704         for (Node node : nodes) {
705             if (bridgeMgr.isIntegrationBridge(node)) {
706                 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
707                 function.apply(elanInstance, interfaceName);
708             }
709         }
710     }
711
712 }