Merge "Using Table Ids from NwConstants.java"
[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.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.Future;
19 import java.util.function.BiFunction;
20
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.netvirt.elan.utils.ElanForwardingEntriesHandler;
29 import org.opendaylight.netvirt.elanmanager.api.IElanService;
30 import org.opendaylight.netvirt.elanmanager.exceptions.MacNotFoundException;
31 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
32 import org.opendaylight.netvirt.elan.l2gw.internal.ElanL2GatewayProvider;
33 import org.opendaylight.netvirt.elan.statisitcs.ElanStatisticsImpl;
34 import org.opendaylight.netvirt.elan.statusanddiag.ElanStatusMonitor;
35 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
36 import org.opendaylight.netvirt.elan.utils.ElanConstants;
37 import org.opendaylight.netvirt.elan.utils.ElanUtils;
38 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
39 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
40 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
41 import org.opendaylight.genius.itm.api.IITMProvider;
42 import org.opendaylight.genius.mdsalutil.MDSALUtil;
43 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.common.RpcResult;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 import com.google.common.base.Optional;
73
74
75 public class ElanServiceProvider implements BindingAwareProvider, IElanService, AutoCloseable {
76
77     private IdManagerService idManager;
78     private IMdsalApiManager mdsalManager;
79     private IInterfaceManager interfaceManager;
80     private OdlInterfaceRpcService interfaceManagerRpcService;
81     private ElanInstanceManager elanInstanceManager;
82     private ElanForwardingEntriesHandler elanForwardingEntriesHandler;
83     private ElanBridgeManager bridgeMgr;
84
85     public IdManagerService getIdManager() {
86         return idManager;
87     }
88
89     public ElanForwardingEntriesHandler getElanForwardingEntriesHandler() {
90         return elanForwardingEntriesHandler;
91     }
92
93     public ElanPacketInHandler getElanPacketInHandler() {
94         return elanPacketInHandler;
95     }
96
97     public ElanSmacFlowEventListener getElanSmacFlowEventListener() {
98         return elanSmacFlowEventListener;
99     }
100
101     public ElanInterfaceStateChangeListener getElanInterfaceStateChangeListener() {
102         return elanInterfaceStateChangeListener;
103     }
104
105     public ElanInterfaceStateClusteredListener getInfStateChangeClusteredListener() {
106         return infStateChangeClusteredListener;
107     }
108
109     public ElanDpnInterfaceClusteredListener getElanDpnInterfaceClusteredListener() {
110         return elanDpnInterfaceClusteredListener;
111     }
112
113     public ElanNodeListener getElanNodeListener() {
114         return elanNodeListener;
115     }
116
117     public NotificationService getNotificationService() {
118         return notificationService;
119     }
120
121     public RpcProviderRegistry getRpcProviderRegistry() {
122         return rpcProviderRegistry;
123     }
124
125     public ElanL2GatewayProvider getElanL2GatewayProvider() {
126         return elanL2GatewayProvider;
127     }
128
129     public static ElanStatusMonitor getElanstatusmonitor() {
130         return elanStatusMonitor;
131     }
132
133     public ElanItmEventListener getElanItmEventListener() {
134         return elanItmEventListener;
135     }
136
137     public static Logger getLogger() {
138         return logger;
139     }
140
141     private ElanInterfaceManager elanInterfaceManager;
142     private ElanPacketInHandler elanPacketInHandler;
143     private ElanSmacFlowEventListener elanSmacFlowEventListener;
144     private ElanInterfaceStateChangeListener elanInterfaceStateChangeListener;
145     private ElanInterfaceStateClusteredListener infStateChangeClusteredListener;
146     private ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
147     private ElanNodeListener elanNodeListener;
148     private NotificationService notificationService;
149     private RpcProviderRegistry rpcProviderRegistry;
150     private IITMProvider itmManager;
151     private ItmRpcService itmRpcService;
152     private DataBroker broker;
153     private ElanL2GatewayProvider elanL2GatewayProvider;
154     private ElanStatisticsService interfaceStatsService;
155     private EntityOwnershipService entityOwnershipService;
156     private static final ElanStatusMonitor elanStatusMonitor = ElanStatusMonitor.getInstance();
157     static DataStoreJobCoordinator dataStoreJobCoordinator;
158     private ElanOvsdbNodeListener elanOvsdbNodeListener;
159
160     private boolean generateIntBridgeMac = true;
161
162     public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
163         dataStoreJobCoordinator = ds;
164     }
165
166     public void setBroker(DataBroker broker) {
167         this.broker = broker;
168     }
169
170     public static DataStoreJobCoordinator getDataStoreJobCoordinator() {
171         if (dataStoreJobCoordinator == null) {
172             dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
173         }
174         return dataStoreJobCoordinator;
175     }
176
177
178     public ElanServiceProvider(RpcProviderRegistry rpcRegistry) {
179         rpcProviderRegistry = rpcRegistry;
180         elanStatusMonitor.registerMbean();
181     }
182
183     // private ElanInterfaceStateChangeListener elanInterfaceEventListener;
184     private ElanItmEventListener elanItmEventListener;
185
186     private static final Logger logger = LoggerFactory.getLogger(ElanServiceProvider.class);
187
188     @Override
189     public void onSessionInitiated(ProviderContext session) {
190         elanStatusMonitor.reportStatus("STARTING");
191         try {
192             createIdPool();
193             getDataStoreJobCoordinator();
194             broker = session.getSALService(DataBroker.class);
195
196             bridgeMgr = new ElanBridgeManager(broker);
197             elanOvsdbNodeListener = new ElanOvsdbNodeListener(broker, generateIntBridgeMac, bridgeMgr, this);
198             ElanUtils.setElanServiceProvider(this);
199             elanForwardingEntriesHandler = ElanForwardingEntriesHandler.getElanForwardingEntriesHandler(this);
200             elanInterfaceManager = ElanInterfaceManager.getElanInterfaceManager(this);
201             elanInstanceManager = ElanInstanceManager.getElanInstanceManager(this);
202             elanNodeListener  = ElanNodeListener.getElanNodeListener(this);
203             elanPacketInHandler = ElanPacketInHandler.getElanPacketInHandler(this);
204             elanSmacFlowEventListener = ElanSmacFlowEventListener.getElanSmacFlowEventListener(this);
205             // Initialize statistics rpc provider for elan
206             interfaceStatsService = ElanStatisticsImpl.getElanStatisticsService(this);
207             rpcProviderRegistry.addRpcImplementation(ElanStatisticsService.class, interfaceStatsService);
208             elanInterfaceStateChangeListener = ElanInterfaceStateChangeListener.getElanInterfaceStateChangeListener(this);
209             infStateChangeClusteredListener = ElanInterfaceStateClusteredListener.getElanInterfaceStateClusteredListener(this);
210             elanDpnInterfaceClusteredListener = ElanDpnInterfaceClusteredListener.getElanDpnInterfaceClusteredListener(this);
211             ElanClusterUtils.setElanServiceProvider(this);
212             this.elanL2GatewayProvider = new ElanL2GatewayProvider(this);
213             elanInterfaceManager.registerListener(LogicalDatastoreType.CONFIGURATION,broker);
214             elanInstanceManager.registerListener(LogicalDatastoreType.CONFIGURATION,broker);
215             notificationService.registerNotificationListener(elanSmacFlowEventListener);
216             notificationService.registerNotificationListener(elanPacketInHandler);
217             elanStatusMonitor.reportStatus("OPERATIONAL");
218         } catch (Exception e) {
219             logger.error("Error initializing services", e);
220             elanStatusMonitor.reportStatus("ERROR");
221         }
222     }
223
224
225
226     public void setIdManager(IdManagerService idManager) {
227         this.idManager = idManager;
228     }
229
230     public void setMdsalManager(IMdsalApiManager mdsalManager) {
231         this.mdsalManager = mdsalManager;
232     }
233
234     public void setInterfaceManager(IInterfaceManager interfaceManager) {
235         this.interfaceManager = interfaceManager;
236     }
237
238     public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
239         this.entityOwnershipService = entityOwnershipService;
240     }
241
242     public IInterfaceManager getInterfaceManager() {
243         return this.interfaceManager;
244     }
245
246     public IMdsalApiManager getMdsalManager() {
247         return mdsalManager;
248     }
249
250     public IITMProvider getItmManager() {
251         return itmManager;
252     }
253
254     public DataBroker getBroker() {
255         return broker;
256     }
257
258     public void setNotificationService(NotificationService notificationService) {
259         this.notificationService = notificationService;
260     }
261
262     public void setInterfaceManagerRpcService(OdlInterfaceRpcService interfaceManager) {
263         this.interfaceManagerRpcService = interfaceManager;
264     }
265
266     public OdlInterfaceRpcService getInterfaceManagerRpcService() {
267         return interfaceManagerRpcService;
268     }
269
270     public void setItmManager(IITMProvider itmManager) {
271         this.itmManager = itmManager;
272     }
273
274     public void setItmRpcService(ItmRpcService itmRpcService) {
275         this.itmRpcService = itmRpcService;
276     }
277
278     public ItmRpcService getItmRpcService() {
279         return itmRpcService;
280     }
281
282     public ElanInstanceManager getElanInstanceManager() {
283         return elanInstanceManager;
284     }
285
286     public ElanInterfaceManager getElanInterfaceManager() {
287         return elanInterfaceManager;
288     }
289
290     public EntityOwnershipService getEntityOwnershipService() {
291         return entityOwnershipService;
292     }
293
294     private void createIdPool() {
295         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
296             .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
297         try {
298             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
299             if ((result != null) && (result.get().isSuccessful())) {
300                 logger.debug("ELAN Id Pool is created successfully");
301             }
302         } catch (Exception e) {
303             logger.error("Failed to create ELAN Id pool {}", e);
304         }
305     }
306
307     @Override
308     public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
309         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
310         boolean isSuccess = true;
311         if (existingElanInstance != null) {
312             if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
313                 logger.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
314                 return true;
315             } else {
316                 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
317                     .setDescription(description).setMacTimeout(macTimeout)
318                     .setKey(new ElanInstanceKey(elanInstanceName)).build();
319                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
320                     ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
321                 logger.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
322                     updateElanInstance, macTimeout, description);
323             }
324         } else {
325             ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
326                 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
327                 .build();
328             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
329                 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
330             logger.debug("Creating the new Elan Instance {}", elanInstance);
331         }
332         return isSuccess;
333     }
334
335     public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
336                                                           String description) {
337         boolean isEqual = false;
338         if (existingElanInstance.getMacTimeout() == macTimeOut
339             && existingElanInstance.getDescription().equals(description)) {
340             isEqual = true;
341         }
342         return isEqual;
343     }
344
345     @Override
346     public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
347         createElanInstance(elanInstanceName, newMacTimout, newDescription);
348     }
349
350     @Override
351     public boolean deleteElanInstance(String elanInstanceName) {
352         boolean isSuccess = false;
353         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
354         if (existingElanInstance == null) {
355             logger.debug("Elan Instance is not present {}", existingElanInstance);
356             return isSuccess;
357         }
358         logger.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
359         ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
360             ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
361         isSuccess = true;
362         return isSuccess;
363     }
364
365     @Override
366     public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
367                                  String description) {
368         ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
369         if (existingElanInstance != null) {
370             ElanInterface elanInterface;
371             if (staticMacAddresses == null) {
372                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
373                     .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
374                     .build();
375             } else {
376                 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
377                     .setDescription(description).setName(interfaceName)
378                     .setStaticMacEntries(getPhysAddress(staticMacAddresses))
379                     .setKey(new ElanInterfaceKey(interfaceName)).build();
380             }
381             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
382                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
383             logger.debug("Creating the new ELan Interface {}", elanInterface);
384         }
385
386     }
387
388     @Override
389     public void updateElanInterface(String elanInstanceName, String interfaceName,
390                                     List<String> updatedStaticMacAddresses, String newDescription) {
391         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
392         if (existingElanInterface == null) {
393             return;
394         }
395         List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
396         List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
397         List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
398         if (updatedPhysAddress.size() > 0) {
399             logger.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
400             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
401                 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
402                 .setKey(new ElanInterfaceKey(interfaceName)).build();
403             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
404                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
405         }
406     }
407
408     @Override
409     public void deleteElanInterface(String elanInstanceName, String interfaceName) {
410         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
411         if (existingElanInterface != null) {
412             ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
413                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
414             logger.debug("deleting the Elan Interface {}", existingElanInterface);
415         }
416     }
417
418     @Override
419     public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
420         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
421         PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
422         if (existingElanInterface != null) {
423             List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
424             if (existingMacAddress.contains(updateStaticMacAddress)) {
425                 return;
426             }
427             existingMacAddress.add(updateStaticMacAddress);
428             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
429                 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
430                 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
431                 .build();
432             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
433                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
434         }
435     }
436
437     @Override
438     public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
439         throws MacNotFoundException {
440         ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
441         PhysAddress physAddress = new PhysAddress(macAddress);
442         if (existingElanInterface == null) {
443             return;
444         }
445         List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
446         if (existingMacAddress.contains(physAddress)) {
447             existingMacAddress.remove(physAddress);
448             ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
449                 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
450                 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
451                 .build();
452             MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
453                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
454         } else {
455             throw new MacNotFoundException("Mac Not Found Exception");
456         }
457     }
458
459     @Override
460     public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
461         Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
462         List<MacEntry> macAddress = new ArrayList<>();
463         if (elanInfo == null) {
464             return macAddress;
465         }
466         List<String> elanInterfaces = elanInfo.getElanInterfaces();
467         if (elanInterfaces != null && elanInterfaces.size() > 0) {
468             for (String elanInterface : elanInterfaces) {
469                 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
470                 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
471                     && elanInterfaceMac.getMacEntry().size() > 0) {
472                     macAddress.addAll(elanInterfaceMac.getMacEntry());
473                 }
474             }
475         }
476         return macAddress;
477     }
478
479     @Override
480     public void flushMACTable(String elanInstanceName) {
481         Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
482         if (elanInfo == null) {
483             return;
484         }
485         List<String> elanInterfaces = elanInfo.getElanInterfaces();
486         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
487             return;
488         }
489         for (String elanInterface : elanInterfaces) {
490             ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
491             if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
492                 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
493                 for (MacEntry macEntry : macEntries) {
494                     try {
495                         deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
496                     } catch (MacNotFoundException e) {
497                         logger.error("Mac Not Found Exception {}", e);
498                         e.printStackTrace();
499                     }
500                 }
501             }
502         }
503
504     }
505
506     @Override
507     public void close() throws Exception {
508         this.elanInstanceManager.close();
509         this.elanL2GatewayProvider.close();
510     }
511
512     public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
513         List<PhysAddress> physAddresses = new ArrayList<>();
514         for (String mac : macAddress) {
515             physAddresses.add(new PhysAddress(mac));
516         }
517         return physAddresses;
518     }
519
520     public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
521                                                   List<PhysAddress> updatePhyAddresses) {
522         if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
523             List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
524             if (originalAddresses != null && !originalAddresses.isEmpty()) {
525                 existingClonedPhyAddress.addAll(0, originalAddresses);
526                 originalAddresses.removeAll(updatePhyAddresses);
527                 updatePhyAddresses.removeAll(existingClonedPhyAddress);
528             }
529         }
530         return updatePhyAddresses;
531     }
532
533     @Override
534     public ElanInstance getElanInstance(String elanName) {
535         return ElanUtils.getElanInstanceByName(elanName);
536     }
537
538     @Override
539     public List<ElanInstance> getElanInstances() {
540         List<ElanInstance> elanList = new ArrayList<>();
541         InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
542             .build();
543         Optional<ElanInstances> elansOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
544             elanInstancesIdentifier);
545         if (elansOptional.isPresent()) {
546             elanList.addAll(elansOptional.get().getElanInstance());
547         }
548         return elanList;
549     }
550
551     @Override
552     public List<String> getElanInterfaces(String elanInstanceName) {
553         List<String> elanInterfaces = new ArrayList<>();
554         InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
555             .build();
556         Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
557             elanInterfacesIdentifier);
558         if (!elanInterfacesOptional.isPresent()) {
559             return elanInterfaces;
560         }
561         List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().getElanInterface();
562         for (ElanInterface elanInterface : elanInterfaceList) {
563             if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
564                 elanInterfaces.add(elanInterface.getName());
565             }
566         }
567         return elanInterfaces;
568     }
569
570     public boolean getGenerateIntBridgeMac() {
571         return generateIntBridgeMac;
572     }
573
574     public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
575         this.generateIntBridgeMac = generateIntBridgeMac;
576     }
577
578     @Override
579     public void createExternalElanNetworks(Node node) {
580         handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
581
582             @Override
583             public Void apply(ElanInstance elanInstance, String interfaceName) {
584                 createExternalElanNetwork(elanInstance, interfaceName);
585                 return null;
586             }
587         });
588     }
589
590     @Override
591     public void createExternalElanNetwork(ElanInstance elanInstance) {
592         handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
593
594             @Override
595             public Void apply(ElanInstance elanInstance, String interfaceName) {
596                 createExternalElanNetwork(elanInstance, interfaceName);
597                 return null;
598             }
599
600         });
601     }
602
603     @Override
604     public void deleteExternalElanNetworks(Node node) {
605         handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
606
607             @Override
608             public Void apply(ElanInstance elanInstance, String interfaceName) {
609                 deleteExternalElanNetwork(elanInstance, interfaceName);
610                 return null;
611             }
612         });
613     }
614
615     @Override
616     public void deleteExternalElanNetwork(ElanInstance elanInstance) {
617         handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
618
619             @Override
620             public Void apply(ElanInstance elanInstance, String interfaceName) {
621                 deleteExternalElanNetwork(elanInstance, interfaceName);
622                 return null;
623             }
624
625         });
626     }
627
628     @Override
629     public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
630         if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
631             return;
632         }
633
634         List<ElanInstance> elanInstances = getElanInstances();
635         if (elanInstances == null || elanInstances.isEmpty()) {
636             logger.trace("No ELAN instances found");
637             return;
638         }
639
640         Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
641                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
642         Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
643                 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
644         Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
645         Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
646
647         for (ElanInstance elanInstance : elanInstances) {
648             String physicalNetworkName = elanInstance.getPhysicalNetworkName();
649             if (physicalNetworkName != null) {
650                 String origPortName = origProviderMappping.get(physicalNetworkName);
651                 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
652                 if (origPortName != null && !origPortName.equals(updatedPortName)) {
653                     deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
654                 }
655                 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
656                     createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
657                 }
658             }
659         }
660     }
661
662     private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
663         if (interfaceName == null) {
664             logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
665             return;
666         }
667
668         String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
669         addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
670     }
671
672     private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
673         if (interfaceName == null) {
674             logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
675             return;
676         }
677
678         String elanInstanceName = elanInstance.getElanInstanceName();
679         for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
680             if (elanInterface.startsWith(interfaceName)) {
681                 deleteIetfInterface(elanInterface);
682                 deleteElanInterface(elanInstanceName, elanInterface);
683             }
684         }
685     }
686
687     @Override
688     public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
689         List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
690         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
691             logger.trace("No ELAN interfaces defined for {}", elanInstanceName);
692             return Collections.emptySet();
693         }
694
695         Set<String> externalElanInterfaces = new HashSet<>();
696         for (String elanInterface : elanInterfaces) {
697             if (ElanUtils.isExternal(elanInterface)) {
698                 externalElanInterfaces.add(elanInterface);
699             }
700         }
701
702         return externalElanInterfaces;
703     }
704
705     /**
706      * Create ietf-interfaces based on the ELAN segment type.<br>
707      * For segment type flat - create transparent interface pointing to the
708      * patch-port attached to the physnet port.<br>
709      * For segment type vlan - create trunk interface pointing to the patch-port
710      * attached to the physnet port + trunk-member interface pointing to the
711      * trunk interface.
712      *
713      * @param elanInstance
714      *            ELAN instance
715      * @param parentRef
716      *            parent interface name
717      * @return the name of the interface to be added to the ELAN instance i.e.
718      *         trunk-member name for vlan network and transparent for flat
719      *         network or null otherwise
720      */
721     private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
722         String interfaceName = null;
723
724         try {
725             if (ElanUtils.isFlat(elanInstance)) {
726                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
727                 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
728                         IfL2vlan.L2vlanMode.Transparent, true);
729             } else if (ElanUtils.isVlan(elanInstance)) {
730                 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
731                 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
732                         IfL2vlan.L2vlanMode.Trunk, true);
733                 Long segmentationId = elanInstance.getSegmentationId();
734                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
735                 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
736                         IfL2vlan.L2vlanMode.TrunkMember, true);
737             }
738         } catch (InterfaceAlreadyExistsException e) {
739             logger.trace("Interface {} was already created", interfaceName);
740         }
741
742         return interfaceName;
743     }
744
745     private void deleteIetfInterface(String interfaceName) {
746         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
747         InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier
748                 .builder(Interfaces.class).child(Interface.class, interfaceKey).build();
749         MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
750         logger.debug("Deleting IETF interface {}", interfaceName);
751     }
752
753     private String getExtInterfaceName(Node node, String physicalNetworkName) {
754         if (physicalNetworkName == null) {
755             return null;
756         }
757
758         String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
759         if (providerMappingValue == null) {
760             logger.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
761                     node.getNodeId().getValue());
762             return null;
763         }
764
765         return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
766                 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
767     }
768
769     private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
770         if (!bridgeMgr.isIntegrationBridge(node)) {
771             return;
772         }
773
774         List<ElanInstance> elanInstances = getElanInstances();
775         if (elanInstances == null || elanInstances.isEmpty()) {
776             logger.trace("No ELAN instances found");
777             return;
778         }
779
780         for (ElanInstance elanInstance : elanInstances) {
781             String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
782             if (interfaceName != null) {
783                 function.apply(elanInstance, interfaceName);
784             }
785         }
786     }
787
788     private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
789         String elanInstanceName = elanInstance.getElanInstanceName();
790         if (elanInstance.getPhysicalNetworkName() == null) {
791             logger.trace("No physical network attached to {}", elanInstanceName);
792             return;
793         }
794
795         List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
796         if (nodes == null || nodes.isEmpty()) {
797             logger.trace("No OVS nodes found while creating external network for ELAN {}",
798                     elanInstance.getElanInstanceName());
799             return;
800         }
801
802         for (Node node : nodes) {
803             if (bridgeMgr.isIntegrationBridge(node)) {
804                 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
805                 function.apply(elanInstance, interfaceName);
806             }
807         }
808     }
809
810 }