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.ElanClusterUtils;
41 import org.opendaylight.netvirt.elan.utils.ElanConstants;
42 import org.opendaylight.netvirt.elan.utils.ElanForwardingEntriesHandler;
43 import org.opendaylight.netvirt.elan.utils.ElanUtils;
44 import org.opendaylight.netvirt.elanmanager.api.IElanService;
45 import org.opendaylight.netvirt.elanmanager.exceptions.MacNotFoundException;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.RpcResult;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
80 public class ElanServiceProvider implements BindingAwareProvider, IElanService, AutoCloseable {
82 private IdManagerService idManager;
83 private IMdsalApiManager mdsalManager;
84 private IInterfaceManager interfaceManager;
85 private OdlInterfaceRpcService interfaceManagerRpcService;
86 private ElanInstanceManager elanInstanceManager;
87 private ElanForwardingEntriesHandler elanForwardingEntriesHandler;
88 private ElanBridgeManager bridgeMgr;
90 public IdManagerService getIdManager() {
94 public ElanForwardingEntriesHandler getElanForwardingEntriesHandler() {
95 return elanForwardingEntriesHandler;
98 public ElanPacketInHandler getElanPacketInHandler() {
99 return elanPacketInHandler;
102 public ElanSmacFlowEventListener getElanSmacFlowEventListener() {
103 return elanSmacFlowEventListener;
106 public ElanInterfaceStateChangeListener getElanInterfaceStateChangeListener() {
107 return elanInterfaceStateChangeListener;
110 public ElanInterfaceStateClusteredListener getInfStateChangeClusteredListener() {
111 return infStateChangeClusteredListener;
114 public ElanDpnInterfaceClusteredListener getElanDpnInterfaceClusteredListener() {
115 return elanDpnInterfaceClusteredListener;
118 public ElanNodeListener getElanNodeListener() {
119 return elanNodeListener;
122 public NotificationService getNotificationService() {
123 return notificationService;
126 public RpcProviderRegistry getRpcProviderRegistry() {
127 return rpcProviderRegistry;
130 public ElanL2GatewayProvider getElanL2GatewayProvider() {
131 return elanL2GatewayProvider;
134 public static ElanStatusMonitor getElanstatusmonitor() {
135 return elanStatusMonitor;
138 public ElanItmEventListener getElanItmEventListener() {
139 return elanItmEventListener;
142 public static Logger getLogger() {
146 private ElanInterfaceManager elanInterfaceManager;
147 private ElanPacketInHandler elanPacketInHandler;
148 private ElanSmacFlowEventListener elanSmacFlowEventListener;
149 private ElanInterfaceStateChangeListener elanInterfaceStateChangeListener;
150 private ElanInterfaceStateClusteredListener infStateChangeClusteredListener;
151 private ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
152 private ElanNodeListener elanNodeListener;
153 private NotificationService notificationService;
154 private RpcProviderRegistry rpcProviderRegistry;
155 private IITMProvider itmManager;
156 private ItmRpcService itmRpcService;
157 private DataBroker broker;
158 private ElanL2GatewayProvider elanL2GatewayProvider;
159 private ElanStatisticsService interfaceStatsService;
160 private EntityOwnershipService entityOwnershipService;
161 private static final ElanStatusMonitor elanStatusMonitor = ElanStatusMonitor.getInstance();
162 static DataStoreJobCoordinator dataStoreJobCoordinator;
163 private ElanOvsdbNodeListener elanOvsdbNodeListener;
165 private boolean generateIntBridgeMac = true;
167 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
168 dataStoreJobCoordinator = ds;
171 public void setBroker(DataBroker broker) {
172 this.broker = broker;
175 public static DataStoreJobCoordinator getDataStoreJobCoordinator() {
176 if (dataStoreJobCoordinator == null) {
177 dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
179 return dataStoreJobCoordinator;
182 public ElanServiceProvider(RpcProviderRegistry rpcRegistry) {
183 rpcProviderRegistry = rpcRegistry;
184 elanStatusMonitor.registerMbean();
187 // private ElanInterfaceStateChangeListener elanInterfaceEventListener;
188 private ElanItmEventListener elanItmEventListener;
190 private static final Logger logger = LoggerFactory.getLogger(ElanServiceProvider.class);
193 public void onSessionInitiated(ProviderContext session) {
194 elanStatusMonitor.reportStatus("STARTING");
197 getDataStoreJobCoordinator();
198 broker = session.getSALService(DataBroker.class);
200 bridgeMgr = new ElanBridgeManager(broker);
201 elanOvsdbNodeListener = new ElanOvsdbNodeListener(broker, generateIntBridgeMac, bridgeMgr, this);
202 ElanUtils.setElanServiceProvider(this);
203 elanForwardingEntriesHandler = ElanForwardingEntriesHandler.getElanForwardingEntriesHandler(this);
204 elanInterfaceManager = ElanInterfaceManager.getElanInterfaceManager(this);
205 elanInstanceManager = ElanInstanceManager.getElanInstanceManager(this);
206 elanNodeListener = ElanNodeListener.getElanNodeListener(this);
207 elanPacketInHandler = ElanPacketInHandler.getElanPacketInHandler(this);
208 elanSmacFlowEventListener = ElanSmacFlowEventListener.getElanSmacFlowEventListener(this);
209 // Initialize statistics rpc provider for elan
210 interfaceStatsService = ElanStatisticsImpl.getElanStatisticsService(this);
211 rpcProviderRegistry.addRpcImplementation(ElanStatisticsService.class, interfaceStatsService);
212 elanInterfaceStateChangeListener = ElanInterfaceStateChangeListener
213 .getElanInterfaceStateChangeListener(this);
214 infStateChangeClusteredListener = ElanInterfaceStateClusteredListener
215 .getElanInterfaceStateClusteredListener(this);
216 elanDpnInterfaceClusteredListener = ElanDpnInterfaceClusteredListener
217 .getElanDpnInterfaceClusteredListener(this);
218 ElanClusterUtils.setElanServiceProvider(this);
219 this.elanL2GatewayProvider = new ElanL2GatewayProvider(this);
220 elanInterfaceManager.registerListener(LogicalDatastoreType.CONFIGURATION, broker);
221 elanInstanceManager.registerListener(LogicalDatastoreType.CONFIGURATION, broker);
222 notificationService.registerNotificationListener(elanSmacFlowEventListener);
223 notificationService.registerNotificationListener(elanPacketInHandler);
224 elanStatusMonitor.reportStatus("OPERATIONAL");
225 } catch (Exception e) {
226 logger.error("Error initializing services", e);
227 elanStatusMonitor.reportStatus("ERROR");
231 public void setIdManager(IdManagerService idManager) {
232 this.idManager = idManager;
235 public void setMdsalManager(IMdsalApiManager mdsalManager) {
236 this.mdsalManager = mdsalManager;
239 public void setInterfaceManager(IInterfaceManager interfaceManager) {
240 this.interfaceManager = interfaceManager;
243 public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
244 this.entityOwnershipService = entityOwnershipService;
247 public IInterfaceManager getInterfaceManager() {
248 return this.interfaceManager;
251 public IMdsalApiManager getMdsalManager() {
255 public IITMProvider getItmManager() {
259 public DataBroker getBroker() {
263 public void setNotificationService(NotificationService notificationService) {
264 this.notificationService = notificationService;
267 public void setInterfaceManagerRpcService(OdlInterfaceRpcService interfaceManager) {
268 this.interfaceManagerRpcService = interfaceManager;
271 public OdlInterfaceRpcService getInterfaceManagerRpcService() {
272 return interfaceManagerRpcService;
275 public void setItmManager(IITMProvider itmManager) {
276 this.itmManager = itmManager;
279 public void setItmRpcService(ItmRpcService itmRpcService) {
280 this.itmRpcService = itmRpcService;
283 public ItmRpcService getItmRpcService() {
284 return itmRpcService;
287 public ElanInstanceManager getElanInstanceManager() {
288 return elanInstanceManager;
291 public ElanInterfaceManager getElanInterfaceManager() {
292 return elanInterfaceManager;
295 public EntityOwnershipService getEntityOwnershipService() {
296 return entityOwnershipService;
299 private void createIdPool() {
300 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
301 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
303 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
304 if ((result != null) && (result.get().isSuccessful())) {
305 logger.debug("ELAN Id Pool is created successfully");
307 } catch (Exception e) {
308 logger.error("Failed to create ELAN Id pool {}", e);
313 public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
314 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
315 boolean isSuccess = true;
316 if (existingElanInstance != null) {
317 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
318 logger.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
321 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
322 .setDescription(description).setMacTimeout(macTimeout)
323 .setKey(new ElanInstanceKey(elanInstanceName)).build();
324 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
325 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
326 logger.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
327 updateElanInstance, macTimeout, description);
330 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
331 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
333 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
334 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
335 logger.debug("Creating the new Elan Instance {}", elanInstance);
341 public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
342 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
343 boolean isSuccess = true;
344 if (existingElanInstance != null) {
345 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
346 logger.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
349 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
350 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
351 .setDescription(description).setMacTimeout(macTimeout)
352 .setKey(new ElanInstanceKey(elanInstanceName))
353 .addAugmentation(EtreeInstance.class, etreeInstance).build();
354 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
355 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
356 logger.debug("Updating the Etree Instance {} with MAC TIME-OUT %l and Description %s ",
357 updateElanInstance, macTimeout, description);
360 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
361 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
362 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
363 .addAugmentation(EtreeInstance.class, etreeInstance).build();
364 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
365 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
366 logger.debug("Creating the new Etree Instance {}", elanInstance);
371 public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
372 String description) {
373 boolean isEqual = false;
374 if (existingElanInstance.getMacTimeout() == macTimeOut
375 && existingElanInstance.getDescription().equals(description)) {
382 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
383 createElanInstance(elanInstanceName, newMacTimout, newDescription);
387 public boolean deleteEtreeInstance(String etreeInstanceName) {
388 return deleteElanInstance(etreeInstanceName);
392 public boolean deleteElanInstance(String elanInstanceName) {
393 boolean isSuccess = false;
394 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
395 if (existingElanInstance == null) {
396 logger.debug("Elan Instance is not present {}", existingElanInstance);
399 logger.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
400 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
401 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
407 public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
408 List<String> staticMacAddresses, String description) {
409 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(etreeInstanceName);
410 if (existingElanInstance != null && existingElanInstance.getAugmentation(EtreeInstance.class) != null) {
411 EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
412 ElanInterface elanInterface;
413 if (staticMacAddresses == null) {
414 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
415 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
416 .addAugmentation(EtreeInterface.class, etreeInterface).build();
418 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
419 .setDescription(description).setName(interfaceName)
420 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
421 .setKey(new ElanInterfaceKey(interfaceName))
422 .addAugmentation(EtreeInterface.class, etreeInterface).build();
424 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
425 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
426 logger.debug("Creating the new Etree Interface {}", elanInterface);
431 public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
432 String description) {
433 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
434 if (existingElanInstance != null) {
435 ElanInterface elanInterface;
436 if (staticMacAddresses == null) {
437 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
438 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
441 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
442 .setDescription(description).setName(interfaceName)
443 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
444 .setKey(new ElanInterfaceKey(interfaceName)).build();
446 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
447 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
448 logger.debug("Creating the new ELan Interface {}", elanInterface);
453 public void updateElanInterface(String elanInstanceName, String interfaceName,
454 List<String> updatedStaticMacAddresses, String newDescription) {
455 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
456 if (existingElanInterface == null) {
459 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
460 List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
461 List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
462 if (updatedPhysAddress.size() > 0) {
463 logger.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
464 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
465 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
466 .setKey(new ElanInterfaceKey(interfaceName)).build();
467 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
468 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
473 public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
474 deleteElanInterface(elanInstanceName, interfaceName);
475 logger.debug("deleting the Etree Interface {}", interfaceName);
479 public void deleteElanInterface(String elanInstanceName, String interfaceName) {
480 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
481 if (existingElanInterface != null) {
482 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
483 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
484 logger.debug("deleting the Elan Interface {}", existingElanInterface);
489 public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
490 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
491 PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
492 if (existingElanInterface != null) {
493 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
494 if (existingMacAddress.contains(updateStaticMacAddress)) {
497 existingMacAddress.add(updateStaticMacAddress);
498 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
499 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
500 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
502 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
503 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
508 public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
509 throws MacNotFoundException {
510 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
511 PhysAddress physAddress = new PhysAddress(macAddress);
512 if (existingElanInterface == null) {
515 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
516 if (existingMacAddress.contains(physAddress)) {
517 existingMacAddress.remove(physAddress);
518 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
519 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
520 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
522 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
523 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
525 throw new MacNotFoundException("Mac Not Found Exception");
530 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
531 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
532 List<MacEntry> macAddress = new ArrayList<>();
533 if (elanInfo == null) {
536 List<String> elanInterfaces = elanInfo.getElanInterfaces();
537 if (elanInterfaces != null && elanInterfaces.size() > 0) {
538 for (String elanInterface : elanInterfaces) {
539 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
540 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
541 && elanInterfaceMac.getMacEntry().size() > 0) {
542 macAddress.addAll(elanInterfaceMac.getMacEntry());
550 public void flushMACTable(String elanInstanceName) {
551 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
552 if (elanInfo == null) {
555 List<String> elanInterfaces = elanInfo.getElanInterfaces();
556 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
559 for (String elanInterface : elanInterfaces) {
560 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
561 if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
562 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
563 for (MacEntry macEntry : macEntries) {
565 deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
566 } catch (MacNotFoundException e) {
567 logger.error("Mac Not Found Exception {}", e);
577 public void close() throws Exception {
578 this.elanInstanceManager.close();
579 this.elanL2GatewayProvider.close();
582 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
583 List<PhysAddress> physAddresses = new ArrayList<>();
584 for (String mac : macAddress) {
585 physAddresses.add(new PhysAddress(mac));
587 return physAddresses;
590 public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
591 List<PhysAddress> updatePhyAddresses) {
592 if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
593 List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
594 if (originalAddresses != null && !originalAddresses.isEmpty()) {
595 existingClonedPhyAddress.addAll(0, originalAddresses);
596 originalAddresses.removeAll(updatePhyAddresses);
597 updatePhyAddresses.removeAll(existingClonedPhyAddress);
600 return updatePhyAddresses;
604 public ElanInstance getElanInstance(String elanName) {
605 return ElanUtils.getElanInstanceByName(elanName);
609 public List<ElanInstance> getElanInstances() {
610 List<ElanInstance> elanList = new ArrayList<>();
611 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
613 Optional<ElanInstances> elansOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
614 elanInstancesIdentifier);
615 if (elansOptional.isPresent()) {
616 elanList.addAll(elansOptional.get().getElanInstance());
622 public List<String> getElanInterfaces(String elanInstanceName) {
623 List<String> elanInterfaces = new ArrayList<>();
624 InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
626 Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
627 elanInterfacesIdentifier);
628 if (!elanInterfacesOptional.isPresent()) {
629 return elanInterfaces;
631 List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().getElanInterface();
632 for (ElanInterface elanInterface : elanInterfaceList) {
633 if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
634 elanInterfaces.add(elanInterface.getName());
637 return elanInterfaces;
640 public boolean getGenerateIntBridgeMac() {
641 return generateIntBridgeMac;
644 public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
645 this.generateIntBridgeMac = generateIntBridgeMac;
649 public void createExternalElanNetworks(Node node) {
650 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
653 public Void apply(ElanInstance elanInstance, String interfaceName) {
654 createExternalElanNetwork(elanInstance, interfaceName);
661 public void createExternalElanNetwork(ElanInstance elanInstance) {
662 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
665 public Void apply(ElanInstance elanInstance, String interfaceName) {
666 createExternalElanNetwork(elanInstance, interfaceName);
674 public void deleteExternalElanNetworks(Node node) {
675 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
678 public Void apply(ElanInstance elanInstance, String interfaceName) {
679 deleteExternalElanNetwork(elanInstance, interfaceName);
686 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
687 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
690 public Void apply(ElanInstance elanInstance, String interfaceName) {
691 deleteExternalElanNetwork(elanInstance, interfaceName);
699 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
700 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
704 List<ElanInstance> elanInstances = getElanInstances();
705 if (elanInstances == null || elanInstances.isEmpty()) {
706 logger.trace("No ELAN instances found");
710 Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
711 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
712 Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
713 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
714 Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
715 Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
717 for (ElanInstance elanInstance : elanInstances) {
718 String physicalNetworkName = elanInstance.getPhysicalNetworkName();
719 if (physicalNetworkName != null) {
720 String origPortName = origProviderMappping.get(physicalNetworkName);
721 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
722 if (origPortName != null && !origPortName.equals(updatedPortName)) {
723 deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
725 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
726 createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
732 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
733 if (interfaceName == null) {
734 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
738 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
739 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
742 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
743 if (interfaceName == null) {
744 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
748 String elanInstanceName = elanInstance.getElanInstanceName();
749 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
750 if (elanInterface.startsWith(interfaceName)) {
751 deleteIetfInterface(elanInterface);
752 deleteElanInterface(elanInstanceName, elanInterface);
758 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
759 DpnInterfaces dpnInterfaces = ElanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
760 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
761 logger.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
765 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
766 if (ElanUtils.isExternal(dpnInterface)) {
771 logger.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
776 public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
777 List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
778 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
779 logger.trace("No ELAN interfaces defined for {}", elanInstanceName);
780 return Collections.emptySet();
783 Set<String> externalElanInterfaces = new HashSet<>();
784 for (String elanInterface : elanInterfaces) {
785 if (ElanUtils.isExternal(elanInterface)) {
786 externalElanInterfaces.add(elanInterface);
790 return externalElanInterfaces;
794 public boolean isExternalInterface(String interfaceName) {
795 return ElanUtils.isExternal(interfaceName);
799 * Create ietf-interfaces based on the ELAN segment type.<br>
800 * For segment type flat - create transparent interface pointing to the
801 * patch-port attached to the physnet port.<br>
802 * For segment type vlan - create trunk interface pointing to the patch-port
803 * attached to the physnet port + trunk-member interface pointing to the
806 * @param elanInstance
809 * parent interface name
810 * @return the name of the interface to be added to the ELAN instance i.e.
811 * trunk-member name for vlan network and transparent for flat
812 * network or null otherwise
814 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
815 String interfaceName = null;
818 if (ElanUtils.isFlat(elanInstance)) {
819 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
820 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
821 IfL2vlan.L2vlanMode.Transparent, true);
822 } else if (ElanUtils.isVlan(elanInstance)) {
823 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
824 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
825 IfL2vlan.L2vlanMode.Trunk, true);
826 Long segmentationId = elanInstance.getSegmentationId();
827 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
828 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
829 IfL2vlan.L2vlanMode.TrunkMember, true);
831 } catch (InterfaceAlreadyExistsException e) {
832 logger.trace("Interface {} was already created", interfaceName);
835 return interfaceName;
838 private void deleteIetfInterface(String interfaceName) {
839 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
840 InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
841 .child(Interface.class, interfaceKey).build();
842 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
843 logger.debug("Deleting IETF interface {}", interfaceName);
846 private String getExtInterfaceName(Node node, String physicalNetworkName) {
847 if (physicalNetworkName == null) {
851 String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
852 if (providerMappingValue == null) {
853 logger.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
854 node.getNodeId().getValue());
858 return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
859 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
862 private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
863 if (!bridgeMgr.isIntegrationBridge(node)) {
867 List<ElanInstance> elanInstances = getElanInstances();
868 if (elanInstances == null || elanInstances.isEmpty()) {
869 logger.trace("No ELAN instances found");
873 for (ElanInstance elanInstance : elanInstances) {
874 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
875 if (interfaceName != null) {
876 function.apply(elanInstance, interfaceName);
881 private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
882 String elanInstanceName = elanInstance.getElanInstanceName();
883 if (elanInstance.getPhysicalNetworkName() == null) {
884 logger.trace("No physical network attached to {}", elanInstanceName);
888 List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
889 if (nodes == null || nodes.isEmpty()) {
890 logger.trace("No OVS nodes found while creating external network for ELAN {}",
891 elanInstance.getElanInstanceName());
895 for (Node node : nodes) {
896 if (bridgeMgr.isIntegrationBridge(node)) {
897 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
898 function.apply(elanInstance, interfaceName);