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
8 package org.opendaylight.netvirt.elan.utils;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.genius.infra.Datastore.Operational;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
24 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
25 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class ElanForwardingEntriesHandler {
39 private static final Logger LOG = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
41 private final ManagedNewTransactionRunner txRunner;
42 private final ElanUtils elanUtils;
45 public ElanForwardingEntriesHandler(DataBroker dataBroker, ElanUtils elanUtils) {
46 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
47 this.elanUtils = elanUtils;
50 public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName,
51 String existingInterfaceName, MacEntry mac, TypedReadWriteTransaction<Operational> tx)
52 throws ExecutionException, InterruptedException {
53 if (existingInterfaceName.equals(interfaceName)) {
54 LOG.error("Static MAC address {} has already been added for the same ElanInstance "
55 + "{} on the same Logical Interface Port {}."
56 + " No operation will be done.",
57 mac.getMacAddress().toString(), elanInstanceName, interfaceName);
59 LOG.warn("Static MAC address {} had already been added for ElanInstance {} on Logical Interface Port {}. "
60 + "This would be considered as MAC movement scenario and old static mac will be removed "
61 + "and new static MAC will be added"
62 + "for ElanInstance {} on Logical Interface Port {}",
63 mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName);
64 //Update the ElanInterface Forwarding Container & ElanForwarding Container
65 deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
66 createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
67 updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
72 public void addElanInterfaceForwardingTableList(String elanInstanceName, String interfaceName,
73 StaticMacEntries staticMacEntries, TypedReadWriteTransaction<Operational> tx)
74 throws ExecutionException, InterruptedException {
75 MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true)
76 .setMacAddress(staticMacEntries.getMacAddress())
77 .setIpPrefix(staticMacEntries.getIpPrefix())
78 .setInterface(interfaceName).withKey(new MacEntryKey(staticMacEntries.getMacAddress())).build();
80 createElanForwardingTablesList(elanInstanceName, macEntry, tx);
81 createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
84 public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac,
85 TypedReadWriteTransaction<Operational> interfaceTx)
86 throws ExecutionException, InterruptedException {
87 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
88 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
89 MacEntry existingInterfaceMacEntry = elanUtils
90 .getInterfaceMacEntriesOperationalDataPathFromId(interfaceTx, existingMacEntryId);
91 if (existingInterfaceMacEntry != null) {
92 interfaceTx.delete(existingMacEntryId);
96 public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac,
97 TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
98 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
99 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
100 MacEntry existingInterfaceMacEntry = elanUtils
101 .getInterfaceMacEntriesOperationalDataPathFromId(tx, existingMacEntryId);
102 if (existingInterfaceMacEntry == null) {
103 MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
104 .setInterface(interfaceName)
105 .setIsStaticAddress(true).withKey(new MacEntryKey(mac.getMacAddress())).build();
106 tx.mergeParentStructurePut(existingMacEntryId, macEntry);
110 public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac,
111 TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
112 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
113 mac.getMacAddress());
114 MacEntry existingMacEntry = elanUtils.getMacEntryFromElanMacId(tx, macEntryId);
115 if (existingMacEntry != null && elanUtils.getElanMacTable(elanName) != null) {
116 MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true)
117 .setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
118 .withKey(new MacEntryKey(mac.getMacAddress())).build();
119 tx.put(macEntryId, newMacEntry);
123 private void createElanForwardingTablesList(String elanName, MacEntry macEntry,
124 TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
125 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
126 macEntry.getMacAddress());
127 Optional<MacEntry> existingMacEntry = tx.read(macEntryId).get();
128 if (!existingMacEntry.isPresent() && elanUtils.getElanMacTable(elanName) != null) {
129 tx.mergeParentStructurePut(macEntryId, macEntry);
133 public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
135 List<ListenableFuture<Void>> futures = new ArrayList<>();
136 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, interfaceTx -> {
137 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
138 .getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
139 interfaceTx.delete(macEntryId);
140 deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, interfaceTx);
141 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
142 flowTx -> elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, flowTx)));
144 for (ListenableFuture<Void> future : futures) {
145 LoggingFutures.addErrorLogging(future, LOG, "Error deleting ELAN interface forwarding entries");