2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.elan.internal;
11 import com.google.common.base.Optional;
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;
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;
82 public class ElanServiceProvider implements BindingAwareProvider, IElanService, AutoCloseable {
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;
92 public IdManagerService getIdManager() {
96 public ElanForwardingEntriesHandler getElanForwardingEntriesHandler() {
97 return elanForwardingEntriesHandler;
100 public ElanPacketInHandler getElanPacketInHandler() {
101 return elanPacketInHandler;
104 public ElanSmacFlowEventListener getElanSmacFlowEventListener() {
105 return elanSmacFlowEventListener;
108 public ElanInterfaceStateChangeListener getElanInterfaceStateChangeListener() {
109 return elanInterfaceStateChangeListener;
112 public ElanInterfaceStateClusteredListener getInfStateChangeClusteredListener() {
113 return infStateChangeClusteredListener;
116 public ElanDpnInterfaceClusteredListener getElanDpnInterfaceClusteredListener() {
117 return elanDpnInterfaceClusteredListener;
120 public ElanNodeListener getElanNodeListener() {
121 return elanNodeListener;
124 public NotificationService getNotificationService() {
125 return notificationService;
128 public RpcProviderRegistry getRpcProviderRegistry() {
129 return rpcProviderRegistry;
132 public ElanL2GatewayProvider getElanL2GatewayProvider() {
133 return elanL2GatewayProvider;
136 public static ElanStatusMonitor getElanstatusmonitor() {
137 return elanStatusMonitor;
140 public ElanItmEventListener getElanItmEventListener() {
141 return elanItmEventListener;
144 public static Logger getLogger() {
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;
169 private boolean generateIntBridgeMac = true;
171 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
172 dataStoreJobCoordinator = ds;
175 public void setBroker(DataBroker broker) {
176 this.broker = broker;
179 public static DataStoreJobCoordinator getDataStoreJobCoordinator() {
180 if (dataStoreJobCoordinator == null) {
181 dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
183 return dataStoreJobCoordinator;
186 public ElanServiceProvider(RpcProviderRegistry rpcRegistry) {
187 rpcProviderRegistry = rpcRegistry;
188 elanStatusMonitor.registerMbean();
191 // private ElanInterfaceStateChangeListener elanInterfaceEventListener;
192 private ElanItmEventListener elanItmEventListener;
194 private static final Logger logger = LoggerFactory.getLogger(ElanServiceProvider.class);
197 public void onSessionInitiated(ProviderContext session) {
198 elanStatusMonitor.reportStatus("STARTING");
201 getDataStoreJobCoordinator();
202 broker = session.getSALService(DataBroker.class);
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");
237 public void setIdManager(IdManagerService idManager) {
238 this.idManager = idManager;
241 public void setMdsalManager(IMdsalApiManager mdsalManager) {
242 this.mdsalManager = mdsalManager;
245 public void setInterfaceManager(IInterfaceManager interfaceManager) {
246 this.interfaceManager = interfaceManager;
249 public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
250 this.entityOwnershipService = entityOwnershipService;
253 public IInterfaceManager getInterfaceManager() {
254 return this.interfaceManager;
257 public IMdsalApiManager getMdsalManager() {
261 public IITMProvider getItmManager() {
265 public DataBroker getBroker() {
269 public void setNotificationService(NotificationService notificationService) {
270 this.notificationService = notificationService;
273 public void setInterfaceManagerRpcService(OdlInterfaceRpcService interfaceManager) {
274 this.interfaceManagerRpcService = interfaceManager;
277 public OdlInterfaceRpcService getInterfaceManagerRpcService() {
278 return interfaceManagerRpcService;
281 public void setItmManager(IITMProvider itmManager) {
282 this.itmManager = itmManager;
285 public void setItmRpcService(ItmRpcService itmRpcService) {
286 this.itmRpcService = itmRpcService;
289 public ItmRpcService getItmRpcService() {
290 return itmRpcService;
293 public ElanInstanceManager getElanInstanceManager() {
294 return elanInstanceManager;
297 public ElanInterfaceManager getElanInterfaceManager() {
298 return elanInterfaceManager;
301 public EntityOwnershipService getEntityOwnershipService() {
302 return entityOwnershipService;
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();
309 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
310 if ((result != null) && (result.get().isSuccessful())) {
311 logger.debug("ELAN Id Pool is created successfully");
313 } catch (Exception e) {
314 logger.error("Failed to create ELAN Id pool {}", e);
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);
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);
336 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
337 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
339 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
340 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
341 logger.debug("Creating the new Elan Instance {}", elanInstance);
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);
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);
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);
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)) {
388 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
389 createElanInstance(elanInstanceName, newMacTimout, newDescription);
393 public boolean deleteEtreeInstance(String etreeInstanceName) {
394 return deleteElanInstance(etreeInstanceName);
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);
405 logger.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
406 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
407 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
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();
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();
430 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
431 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
432 logger.debug("Creating the new Etree Interface {}", elanInterface);
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))
447 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
448 .setDescription(description).setName(interfaceName)
449 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
450 .setKey(new ElanInterfaceKey(interfaceName)).build();
452 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
453 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
454 logger.debug("Creating the new ELan Interface {}", elanInterface);
459 public void updateElanInterface(String elanInstanceName, String interfaceName,
460 List<String> updatedStaticMacAddresses, String newDescription) {
461 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
462 if (existingElanInterface == null) {
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);
479 public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
480 deleteElanInterface(elanInstanceName, interfaceName);
481 logger.debug("deleting the Etree Interface {}", interfaceName);
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);
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)) {
503 existingMacAddress.add(updateStaticMacAddress);
504 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
505 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
506 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
508 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
509 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
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) {
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))
528 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
529 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
531 throw new MacNotFoundException("Mac Not Found Exception");
536 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
537 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
538 List<MacEntry> macAddress = new ArrayList<>();
539 if (elanInfo == null) {
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());
556 public void flushMACTable(String elanInstanceName) {
557 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
558 if (elanInfo == null) {
561 List<String> elanInterfaces = elanInfo.getElanInterfaces();
562 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
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) {
571 deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
572 } catch (MacNotFoundException e) {
573 logger.error("Mac Not Found Exception {}", e);
583 public void close() throws Exception {
584 this.elanInstanceManager.close();
585 this.elanL2GatewayProvider.close();
586 this.cacheElanInstanceListener.close();
587 this.cacheElanInterfaceListener.close();
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));
595 return physAddresses;
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);
608 return updatePhyAddresses;
612 public ElanInstance getElanInstance(String elanName) {
613 return ElanUtils.getElanInstanceByName(elanName);
617 public List<ElanInstance> getElanInstances() {
618 List<ElanInstance> elanList = new ArrayList<>();
619 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
621 Optional<ElanInstances> elansOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
622 elanInstancesIdentifier);
623 if (elansOptional.isPresent()) {
624 elanList.addAll(elansOptional.get().getElanInstance());
630 public List<String> getElanInterfaces(String elanInstanceName) {
631 List<String> elanInterfaces = new ArrayList<>();
632 InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
634 Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
635 elanInterfacesIdentifier);
636 if (!elanInterfacesOptional.isPresent()) {
637 return elanInterfaces;
639 List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().getElanInterface();
640 for (ElanInterface elanInterface : elanInterfaceList) {
641 if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
642 elanInterfaces.add(elanInterface.getName());
645 return elanInterfaces;
648 public boolean getGenerateIntBridgeMac() {
649 return generateIntBridgeMac;
652 public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
653 this.generateIntBridgeMac = generateIntBridgeMac;
657 public void createExternalElanNetworks(Node node) {
658 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
661 public Void apply(ElanInstance elanInstance, String interfaceName) {
662 createExternalElanNetwork(elanInstance, interfaceName);
669 public void createExternalElanNetwork(ElanInstance elanInstance) {
670 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
673 public Void apply(ElanInstance elanInstance, String interfaceName) {
674 createExternalElanNetwork(elanInstance, interfaceName);
682 public void deleteExternalElanNetworks(Node node) {
683 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
686 public Void apply(ElanInstance elanInstance, String interfaceName) {
687 deleteExternalElanNetwork(elanInstance, interfaceName);
694 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
695 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
698 public Void apply(ElanInstance elanInstance, String interfaceName) {
699 deleteExternalElanNetwork(elanInstance, interfaceName);
707 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
708 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
712 List<ElanInstance> elanInstances = getElanInstances();
713 if (elanInstances == null || elanInstances.isEmpty()) {
714 logger.trace("No ELAN instances found");
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());
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));
733 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
734 createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
740 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
741 if (interfaceName == null) {
742 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
746 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
747 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
750 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
751 if (interfaceName == null) {
752 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
756 String elanInstanceName = elanInstance.getElanInstanceName();
757 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
758 if (elanInterface.startsWith(interfaceName)) {
759 deleteIetfInterface(elanInterface);
760 deleteElanInterface(elanInstanceName, elanInterface);
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);
773 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
774 if (ElanUtils.isExternal(dpnInterface)) {
779 logger.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
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();
791 Set<String> externalElanInterfaces = new HashSet<>();
792 for (String elanInterface : elanInterfaces) {
793 if (ElanUtils.isExternal(elanInterface)) {
794 externalElanInterfaces.add(elanInterface);
798 return externalElanInterfaces;
802 public boolean isExternalInterface(String interfaceName) {
803 return ElanUtils.isExternal(interfaceName);
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
814 * @param elanInstance
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
822 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
823 String interfaceName = null;
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);
837 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
838 IfL2vlan.L2vlanMode.TrunkMember, true);
840 } catch (InterfaceAlreadyExistsException e) {
841 logger.trace("Interface {} was already created", interfaceName);
844 return interfaceName;
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);
855 private String getExtInterfaceName(Node node, String physicalNetworkName) {
856 if (physicalNetworkName == null) {
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());
867 return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
868 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
871 private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
872 if (!bridgeMgr.isIntegrationBridge(node)) {
876 List<ElanInstance> elanInstances = getElanInstances();
877 if (elanInstances == null || elanInstances.isEmpty()) {
878 logger.trace("No ELAN instances found");
882 for (ElanInstance elanInstance : elanInstances) {
883 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
884 if (interfaceName != null) {
885 function.apply(elanInstance, interfaceName);
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);
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());
904 for (Node node : nodes) {
905 if (bridgeMgr.isIntegrationBridge(node)) {
906 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
907 function.apply(elanInstance, interfaceName);