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