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;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
19 import java.util.concurrent.Future;
20 import java.util.function.BiFunction;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
29 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
30 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
31 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
32 import org.opendaylight.genius.itm.api.IITMProvider;
33 import org.opendaylight.genius.mdsalutil.MDSALUtil;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.netvirt.elan.l2gw.internal.ElanL2GatewayProvider;
36 import org.opendaylight.netvirt.elan.statisitcs.ElanStatisticsImpl;
37 import org.opendaylight.netvirt.elan.statusanddiag.ElanStatusMonitor;
38 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
39 import org.opendaylight.netvirt.elan.utils.ElanConstants;
40 import org.opendaylight.netvirt.elan.utils.ElanForwardingEntriesHandler;
41 import org.opendaylight.netvirt.elan.utils.ElanUtils;
42 import org.opendaylight.netvirt.elanmanager.api.IElanService;
43 import org.opendaylight.netvirt.elanmanager.exceptions.MacNotFoundException;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.statistics.rev150824.ElanStatisticsService;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 public class ElanServiceProvider implements BindingAwareProvider, IElanService, AutoCloseable {
79 private IdManagerService idManager;
80 private IMdsalApiManager mdsalManager;
81 private IInterfaceManager interfaceManager;
82 private OdlInterfaceRpcService interfaceManagerRpcService;
83 private ElanInstanceManager elanInstanceManager;
84 private ElanForwardingEntriesHandler elanForwardingEntriesHandler;
85 private ElanBridgeManager bridgeMgr;
87 public IdManagerService getIdManager() {
91 public ElanForwardingEntriesHandler getElanForwardingEntriesHandler() {
92 return elanForwardingEntriesHandler;
95 public ElanPacketInHandler getElanPacketInHandler() {
96 return elanPacketInHandler;
99 public ElanSmacFlowEventListener getElanSmacFlowEventListener() {
100 return elanSmacFlowEventListener;
103 public ElanInterfaceStateChangeListener getElanInterfaceStateChangeListener() {
104 return elanInterfaceStateChangeListener;
107 public ElanInterfaceStateClusteredListener getInfStateChangeClusteredListener() {
108 return infStateChangeClusteredListener;
111 public ElanDpnInterfaceClusteredListener getElanDpnInterfaceClusteredListener() {
112 return elanDpnInterfaceClusteredListener;
115 public ElanNodeListener getElanNodeListener() {
116 return elanNodeListener;
119 public NotificationService getNotificationService() {
120 return notificationService;
123 public RpcProviderRegistry getRpcProviderRegistry() {
124 return rpcProviderRegistry;
127 public ElanL2GatewayProvider getElanL2GatewayProvider() {
128 return elanL2GatewayProvider;
131 public static ElanStatusMonitor getElanstatusmonitor() {
132 return elanStatusMonitor;
135 public ElanItmEventListener getElanItmEventListener() {
136 return elanItmEventListener;
139 public static Logger getLogger() {
143 private ElanInterfaceManager elanInterfaceManager;
144 private ElanPacketInHandler elanPacketInHandler;
145 private ElanSmacFlowEventListener elanSmacFlowEventListener;
146 private ElanInterfaceStateChangeListener elanInterfaceStateChangeListener;
147 private ElanInterfaceStateClusteredListener infStateChangeClusteredListener;
148 private ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener;
149 private ElanNodeListener elanNodeListener;
150 private NotificationService notificationService;
151 private RpcProviderRegistry rpcProviderRegistry;
152 private IITMProvider itmManager;
153 private ItmRpcService itmRpcService;
154 private DataBroker broker;
155 private ElanL2GatewayProvider elanL2GatewayProvider;
156 private ElanStatisticsService interfaceStatsService;
157 private EntityOwnershipService entityOwnershipService;
158 private static final ElanStatusMonitor elanStatusMonitor = ElanStatusMonitor.getInstance();
159 static DataStoreJobCoordinator dataStoreJobCoordinator;
160 private ElanOvsdbNodeListener elanOvsdbNodeListener;
162 private boolean generateIntBridgeMac = true;
164 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
165 dataStoreJobCoordinator = ds;
168 public void setBroker(DataBroker broker) {
169 this.broker = broker;
172 public static DataStoreJobCoordinator getDataStoreJobCoordinator() {
173 if (dataStoreJobCoordinator == null) {
174 dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
176 return dataStoreJobCoordinator;
179 public ElanServiceProvider(RpcProviderRegistry rpcRegistry) {
180 rpcProviderRegistry = rpcRegistry;
181 elanStatusMonitor.registerMbean();
184 // private ElanInterfaceStateChangeListener elanInterfaceEventListener;
185 private ElanItmEventListener elanItmEventListener;
187 private static final Logger logger = LoggerFactory.getLogger(ElanServiceProvider.class);
190 public void onSessionInitiated(ProviderContext session) {
191 elanStatusMonitor.reportStatus("STARTING");
194 getDataStoreJobCoordinator();
195 broker = session.getSALService(DataBroker.class);
197 bridgeMgr = new ElanBridgeManager(broker);
198 elanOvsdbNodeListener = new ElanOvsdbNodeListener(broker, generateIntBridgeMac, bridgeMgr, this);
199 ElanUtils.setElanServiceProvider(this);
200 elanForwardingEntriesHandler = ElanForwardingEntriesHandler.getElanForwardingEntriesHandler(this);
201 elanInterfaceManager = ElanInterfaceManager.getElanInterfaceManager(this);
202 elanInstanceManager = ElanInstanceManager.getElanInstanceManager(this);
203 elanNodeListener = ElanNodeListener.getElanNodeListener(this);
204 elanPacketInHandler = ElanPacketInHandler.getElanPacketInHandler(this);
205 elanSmacFlowEventListener = ElanSmacFlowEventListener.getElanSmacFlowEventListener(this);
206 // Initialize statistics rpc provider for elan
207 interfaceStatsService = ElanStatisticsImpl.getElanStatisticsService(this);
208 rpcProviderRegistry.addRpcImplementation(ElanStatisticsService.class, interfaceStatsService);
209 elanInterfaceStateChangeListener = ElanInterfaceStateChangeListener
210 .getElanInterfaceStateChangeListener(this);
211 infStateChangeClusteredListener = ElanInterfaceStateClusteredListener
212 .getElanInterfaceStateClusteredListener(this);
213 elanDpnInterfaceClusteredListener = ElanDpnInterfaceClusteredListener
214 .getElanDpnInterfaceClusteredListener(this);
215 ElanClusterUtils.setElanServiceProvider(this);
216 this.elanL2GatewayProvider = new ElanL2GatewayProvider(this);
217 elanInterfaceManager.registerListener(LogicalDatastoreType.CONFIGURATION, broker);
218 elanInstanceManager.registerListener(LogicalDatastoreType.CONFIGURATION, broker);
219 notificationService.registerNotificationListener(elanSmacFlowEventListener);
220 notificationService.registerNotificationListener(elanPacketInHandler);
221 elanStatusMonitor.reportStatus("OPERATIONAL");
222 } catch (Exception e) {
223 logger.error("Error initializing services", e);
224 elanStatusMonitor.reportStatus("ERROR");
228 public void setIdManager(IdManagerService idManager) {
229 this.idManager = idManager;
232 public void setMdsalManager(IMdsalApiManager mdsalManager) {
233 this.mdsalManager = mdsalManager;
236 public void setInterfaceManager(IInterfaceManager interfaceManager) {
237 this.interfaceManager = interfaceManager;
240 public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
241 this.entityOwnershipService = entityOwnershipService;
244 public IInterfaceManager getInterfaceManager() {
245 return this.interfaceManager;
248 public IMdsalApiManager getMdsalManager() {
252 public IITMProvider getItmManager() {
256 public DataBroker getBroker() {
260 public void setNotificationService(NotificationService notificationService) {
261 this.notificationService = notificationService;
264 public void setInterfaceManagerRpcService(OdlInterfaceRpcService interfaceManager) {
265 this.interfaceManagerRpcService = interfaceManager;
268 public OdlInterfaceRpcService getInterfaceManagerRpcService() {
269 return interfaceManagerRpcService;
272 public void setItmManager(IITMProvider itmManager) {
273 this.itmManager = itmManager;
276 public void setItmRpcService(ItmRpcService itmRpcService) {
277 this.itmRpcService = itmRpcService;
280 public ItmRpcService getItmRpcService() {
281 return itmRpcService;
284 public ElanInstanceManager getElanInstanceManager() {
285 return elanInstanceManager;
288 public ElanInterfaceManager getElanInterfaceManager() {
289 return elanInterfaceManager;
292 public EntityOwnershipService getEntityOwnershipService() {
293 return entityOwnershipService;
296 private void createIdPool() {
297 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
298 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
300 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
301 if ((result != null) && (result.get().isSuccessful())) {
302 logger.debug("ELAN Id Pool is created successfully");
304 } catch (Exception e) {
305 logger.error("Failed to create ELAN Id pool {}", e);
310 public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
311 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
312 boolean isSuccess = true;
313 if (existingElanInstance != null) {
314 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
315 logger.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
318 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
319 .setDescription(description).setMacTimeout(macTimeout)
320 .setKey(new ElanInstanceKey(elanInstanceName)).build();
321 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
322 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
323 logger.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
324 updateElanInstance, macTimeout, description);
327 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
328 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
330 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
331 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
332 logger.debug("Creating the new Elan Instance {}", elanInstance);
338 public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
339 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
340 boolean isSuccess = true;
341 if (existingElanInstance != null) {
342 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
343 logger.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
346 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
347 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
348 .setDescription(description).setMacTimeout(macTimeout)
349 .setKey(new ElanInstanceKey(elanInstanceName))
350 .addAugmentation(EtreeInstance.class, etreeInstance).build();
351 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
352 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
353 logger.debug("Updating the Etree Instance {} with MAC TIME-OUT %l and Description %s ",
354 updateElanInstance, macTimeout, description);
357 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
358 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
359 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
360 .addAugmentation(EtreeInstance.class, etreeInstance).build();
361 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
362 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
363 logger.debug("Creating the new Etree Instance {}", elanInstance);
368 public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
369 String description) {
370 boolean isEqual = false;
371 if (existingElanInstance.getMacTimeout() == macTimeOut
372 && existingElanInstance.getDescription().equals(description)) {
379 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
380 createElanInstance(elanInstanceName, newMacTimout, newDescription);
384 public boolean deleteEtreeInstance(String etreeInstanceName) {
385 return deleteElanInstance(etreeInstanceName);
389 public boolean deleteElanInstance(String elanInstanceName) {
390 boolean isSuccess = false;
391 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
392 if (existingElanInstance == null) {
393 logger.debug("Elan Instance is not present {}", existingElanInstance);
396 logger.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
397 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
398 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
404 public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
405 List<String> staticMacAddresses, String description) {
406 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(etreeInstanceName);
407 if (existingElanInstance != null && existingElanInstance.getAugmentation(EtreeInstance.class) != null) {
408 EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
409 ElanInterface elanInterface;
410 if (staticMacAddresses == null) {
411 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
412 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
413 .addAugmentation(EtreeInterface.class, etreeInterface).build();
415 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
416 .setDescription(description).setName(interfaceName)
417 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
418 .setKey(new ElanInterfaceKey(interfaceName))
419 .addAugmentation(EtreeInterface.class, etreeInterface).build();
421 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
422 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
423 logger.debug("Creating the new Etree Interface {}", elanInterface);
428 public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
429 String description) {
430 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
431 if (existingElanInstance != null) {
432 ElanInterface elanInterface;
433 if (staticMacAddresses == null) {
434 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
435 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
438 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
439 .setDescription(description).setName(interfaceName)
440 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
441 .setKey(new ElanInterfaceKey(interfaceName)).build();
443 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
444 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
445 logger.debug("Creating the new ELan Interface {}", elanInterface);
450 public void updateElanInterface(String elanInstanceName, String interfaceName,
451 List<String> updatedStaticMacAddresses, String newDescription) {
452 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
453 if (existingElanInterface == null) {
456 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
457 List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
458 List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
459 if (updatedPhysAddress.size() > 0) {
460 logger.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
461 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
462 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
463 .setKey(new ElanInterfaceKey(interfaceName)).build();
464 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
465 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
470 public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
471 deleteElanInterface(elanInstanceName, interfaceName);
472 logger.debug("deleting the Etree Interface {}", interfaceName);
476 public void deleteElanInterface(String elanInstanceName, String interfaceName) {
477 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
478 if (existingElanInterface != null) {
479 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
480 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
481 logger.debug("deleting the Elan Interface {}", existingElanInterface);
486 public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
487 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
488 PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
489 if (existingElanInterface != null) {
490 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
491 if (existingMacAddress.contains(updateStaticMacAddress)) {
494 existingMacAddress.add(updateStaticMacAddress);
495 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
496 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
497 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
499 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
500 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
505 public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
506 throws MacNotFoundException {
507 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
508 PhysAddress physAddress = new PhysAddress(macAddress);
509 if (existingElanInterface == null) {
512 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
513 if (existingMacAddress.contains(physAddress)) {
514 existingMacAddress.remove(physAddress);
515 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
516 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
517 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
519 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
520 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
522 throw new MacNotFoundException("Mac Not Found Exception");
527 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
528 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
529 List<MacEntry> macAddress = new ArrayList<>();
530 if (elanInfo == null) {
533 List<String> elanInterfaces = elanInfo.getElanInterfaces();
534 if (elanInterfaces != null && elanInterfaces.size() > 0) {
535 for (String elanInterface : elanInterfaces) {
536 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
537 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
538 && elanInterfaceMac.getMacEntry().size() > 0) {
539 macAddress.addAll(elanInterfaceMac.getMacEntry());
547 public void flushMACTable(String elanInstanceName) {
548 Elan elanInfo = ElanUtils.getElanByName(elanInstanceName);
549 if (elanInfo == null) {
552 List<String> elanInterfaces = elanInfo.getElanInterfaces();
553 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
556 for (String elanInterface : elanInterfaces) {
557 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
558 if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
559 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
560 for (MacEntry macEntry : macEntries) {
562 deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
563 } catch (MacNotFoundException e) {
564 logger.error("Mac Not Found Exception {}", e);
574 public void close() throws Exception {
575 this.elanInstanceManager.close();
576 this.elanL2GatewayProvider.close();
579 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
580 List<PhysAddress> physAddresses = new ArrayList<>();
581 for (String mac : macAddress) {
582 physAddresses.add(new PhysAddress(mac));
584 return physAddresses;
587 public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
588 List<PhysAddress> updatePhyAddresses) {
589 if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
590 List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
591 if (originalAddresses != null && !originalAddresses.isEmpty()) {
592 existingClonedPhyAddress.addAll(0, originalAddresses);
593 originalAddresses.removeAll(updatePhyAddresses);
594 updatePhyAddresses.removeAll(existingClonedPhyAddress);
597 return updatePhyAddresses;
601 public ElanInstance getElanInstance(String elanName) {
602 return ElanUtils.getElanInstanceByName(elanName);
606 public List<ElanInstance> getElanInstances() {
607 List<ElanInstance> elanList = new ArrayList<>();
608 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
610 Optional<ElanInstances> elansOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
611 elanInstancesIdentifier);
612 if (elansOptional.isPresent()) {
613 elanList.addAll(elansOptional.get().getElanInstance());
619 public List<String> getElanInterfaces(String elanInstanceName) {
620 List<String> elanInterfaces = new ArrayList<>();
621 InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
623 Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
624 elanInterfacesIdentifier);
625 if (!elanInterfacesOptional.isPresent()) {
626 return elanInterfaces;
628 List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().getElanInterface();
629 for (ElanInterface elanInterface : elanInterfaceList) {
630 if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
631 elanInterfaces.add(elanInterface.getName());
634 return elanInterfaces;
637 public boolean getGenerateIntBridgeMac() {
638 return generateIntBridgeMac;
641 public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
642 this.generateIntBridgeMac = generateIntBridgeMac;
646 public void createExternalElanNetworks(Node node) {
647 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
650 public Void apply(ElanInstance elanInstance, String interfaceName) {
651 createExternalElanNetwork(elanInstance, interfaceName);
658 public void createExternalElanNetwork(ElanInstance elanInstance) {
659 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
662 public Void apply(ElanInstance elanInstance, String interfaceName) {
663 createExternalElanNetwork(elanInstance, interfaceName);
671 public void deleteExternalElanNetworks(Node node) {
672 handleExternalElanNetworks(node, new BiFunction<ElanInstance, String, Void>() {
675 public Void apply(ElanInstance elanInstance, String interfaceName) {
676 deleteExternalElanNetwork(elanInstance, interfaceName);
683 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
684 handleExternalElanNetwork(elanInstance, new BiFunction<ElanInstance, String, Void>() {
687 public Void apply(ElanInstance elanInstance, String interfaceName) {
688 deleteExternalElanNetwork(elanInstance, interfaceName);
696 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
697 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
701 List<ElanInstance> elanInstances = getElanInstances();
702 if (elanInstances == null || elanInstances.isEmpty()) {
703 logger.trace("No ELAN instances found");
707 Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
708 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
709 Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
710 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
711 Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
712 Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
714 for (ElanInstance elanInstance : elanInstances) {
715 String physicalNetworkName = elanInstance.getPhysicalNetworkName();
716 if (physicalNetworkName != null) {
717 String origPortName = origProviderMappping.get(physicalNetworkName);
718 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
719 if (origPortName != null && !origPortName.equals(updatedPortName)) {
720 deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
722 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
723 createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
729 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
730 if (interfaceName == null) {
731 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
735 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
736 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
739 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
740 if (interfaceName == null) {
741 logger.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
745 String elanInstanceName = elanInstance.getElanInstanceName();
746 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
747 if (elanInterface.startsWith(interfaceName)) {
748 deleteIetfInterface(elanInterface);
749 deleteElanInterface(elanInstanceName, elanInterface);
755 public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
756 List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
757 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
758 logger.trace("No ELAN interfaces defined for {}", elanInstanceName);
759 return Collections.emptySet();
762 Set<String> externalElanInterfaces = new HashSet<>();
763 for (String elanInterface : elanInterfaces) {
764 if (ElanUtils.isExternal(elanInterface)) {
765 externalElanInterfaces.add(elanInterface);
769 return externalElanInterfaces;
773 * Create ietf-interfaces based on the ELAN segment type.<br>
774 * For segment type flat - create transparent interface pointing to the
775 * patch-port attached to the physnet port.<br>
776 * For segment type vlan - create trunk interface pointing to the patch-port
777 * attached to the physnet port + trunk-member interface pointing to the
780 * @param elanInstance
783 * parent interface name
784 * @return the name of the interface to be added to the ELAN instance i.e.
785 * trunk-member name for vlan network and transparent for flat
786 * network or null otherwise
788 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
789 String interfaceName = null;
792 if (ElanUtils.isFlat(elanInstance)) {
793 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
794 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
795 IfL2vlan.L2vlanMode.Transparent, true);
796 } else if (ElanUtils.isVlan(elanInstance)) {
797 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
798 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
799 IfL2vlan.L2vlanMode.Trunk, true);
800 Long segmentationId = elanInstance.getSegmentationId();
801 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
802 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
803 IfL2vlan.L2vlanMode.TrunkMember, true);
805 } catch (InterfaceAlreadyExistsException e) {
806 logger.trace("Interface {} was already created", interfaceName);
809 return interfaceName;
812 private void deleteIetfInterface(String interfaceName) {
813 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
814 InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
815 .child(Interface.class, interfaceKey).build();
816 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
817 logger.debug("Deleting IETF interface {}", interfaceName);
820 private String getExtInterfaceName(Node node, String physicalNetworkName) {
821 if (physicalNetworkName == null) {
825 String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
826 if (providerMappingValue == null) {
827 logger.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
828 node.getNodeId().getValue());
832 return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
833 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
836 private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
837 if (!bridgeMgr.isIntegrationBridge(node)) {
841 List<ElanInstance> elanInstances = getElanInstances();
842 if (elanInstances == null || elanInstances.isEmpty()) {
843 logger.trace("No ELAN instances found");
847 for (ElanInstance elanInstance : elanInstances) {
848 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
849 if (interfaceName != null) {
850 function.apply(elanInstance, interfaceName);
855 private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
856 String elanInstanceName = elanInstance.getElanInstanceName();
857 if (elanInstance.getPhysicalNetworkName() == null) {
858 logger.trace("No physical network attached to {}", elanInstanceName);
862 List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
863 if (nodes == null || nodes.isEmpty()) {
864 logger.trace("No OVS nodes found while creating external network for ELAN {}",
865 elanInstance.getElanInstanceName());
869 for (Node node : nodes) {
870 if (bridgeMgr.isIntegrationBridge(node)) {
871 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
872 function.apply(elanInstance, interfaceName);