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.utils;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.netvirt.elan.internal.ElanServiceProvider;
19 import org.opendaylight.netvirt.elan.utils.ElanUtils;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Optional;
37 public class ElanForwardingEntriesHandler {
39 private static final Logger logger = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
40 private ElanServiceProvider elanServiceProvider = null;
41 private static volatile ElanForwardingEntriesHandler elanForwardingEntriesHandler = null;
43 public ElanServiceProvider getElanServiceProvider() {
44 return elanServiceProvider;
46 public void setElanServiceProvider(ElanServiceProvider elanServiceProvider) {
47 this.elanServiceProvider = elanServiceProvider;
50 public ElanForwardingEntriesHandler(ElanServiceProvider elanServiceProvider){
52 this.elanServiceProvider = elanServiceProvider;
54 public static ElanForwardingEntriesHandler getElanForwardingEntriesHandler(
55 ElanServiceProvider elanServiceProvider) {
56 if (elanForwardingEntriesHandler == null) {
57 synchronized (ElanForwardingEntriesHandler.class) {
58 if (elanForwardingEntriesHandler == null) {
59 elanForwardingEntriesHandler = new ElanForwardingEntriesHandler(elanServiceProvider);
63 return elanForwardingEntriesHandler;
65 public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName, String existingInterfaceName, MacEntry mac, WriteTransaction tx) {
66 if (existingInterfaceName.equals(interfaceName)) {
67 logger.error(String.format("Static MAC address %s has already been added for the same ElanInstance %s on the same Logical Interface Port %s."
68 + " No operation will be done.", mac.getMacAddress().toString(), elanInstanceName, interfaceName));
70 logger.warn(String.format("Static MAC address %s had already been added for ElanInstance %s on Logical Interface Port %s. "
71 + "This would be considered as MAC movement scenario and old static mac will be removed and new static MAC will be added"
72 + "for ElanInstance %s on Logical Interface Port %s", mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName));
73 //Update the ElanInterface Forwarding Container & ElanForwarding Container
74 deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
75 createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
76 updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
81 public void addElanInterfaceForwardingTableList(ElanInstance elanInstance, String interfaceName, PhysAddress physAddress, WriteTransaction tx) {
82 MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true).setMacAddress(physAddress).setInterface(interfaceName).setKey(new MacEntryKey(physAddress)).build();
83 createElanForwardingTablesList(elanInstance.getElanInstanceName(), macEntry, tx);
84 createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
87 public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
88 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
89 MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
90 if (existingInterfaceMacEntry != null) {
91 tx.delete(LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
95 public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
96 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
97 MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
98 if (existingInterfaceMacEntry == null) {
99 MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setInterface(interfaceName).setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
100 tx.put(LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry);
105 public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac, WriteTransaction tx) {
106 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, mac.getMacAddress());
107 MacEntry existingMacEntry = ElanUtils.getMacEntryFromElanMacId(macEntryId);
108 if (existingMacEntry != null) {
109 // Fix for TR HU71400.
110 // ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
111 MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true).setMacAddress(mac.getMacAddress()).setKey(new MacEntryKey(mac.getMacAddress())).build();
112 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
116 private void createElanForwardingTablesList(String elanName, MacEntry macEntry, WriteTransaction tx) {
117 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, macEntry.getMacAddress());
118 Optional<MacEntry> existingMacEntry = ElanUtils.read(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, macEntryId);
119 if (!existingMacEntry.isPresent()) {
120 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry);
124 public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo, MacEntry macEntry, WriteTransaction tx) {
125 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
126 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
127 deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, tx);
128 WriteTransaction deleteFlowtx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
129 ElanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, deleteFlowtx);
130 deleteFlowtx.submit();
133 public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress, WriteTransaction tx) {
134 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
135 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);