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