d6599a9c50a0778275ff1fb23945f66df4fb9b30
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / utils / ElanForwardingEntriesHandler.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.utils;
9
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;
13
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.ListenableFuture;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import java.util.concurrent.ExecutionException;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.genius.infra.Datastore.Operational;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
31 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
32 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 @Singleton
44 public class ElanForwardingEntriesHandler {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
47
48     private final ManagedNewTransactionRunner txRunner;
49     private final ElanUtils elanUtils;
50
51     @Inject
52     public ElanForwardingEntriesHandler(DataBroker dataBroker, ElanUtils elanUtils) {
53         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
54         this.elanUtils = elanUtils;
55     }
56
57     public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName,
58             String existingInterfaceName, MacEntry mac, TypedReadWriteTransaction<Operational> tx)
59             throws ExecutionException, InterruptedException {
60         if (existingInterfaceName.equals(interfaceName)) {
61             LOG.error("Static MAC address {} has already been added for the same ElanInstance "
62                             + "{} on the same Logical Interface Port {}."
63                             + " No operation will be done.",
64                     mac.getMacAddress().toString(), elanInstanceName, interfaceName);
65         } else {
66             LOG.warn("Static MAC address {} had already been added for ElanInstance {} on Logical Interface Port {}. "
67                             + "This would be considered as MAC movement scenario and old static mac will be removed "
68                             + "and new static MAC will be added"
69                             + "for ElanInstance {} on Logical Interface Port {}",
70                     mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName);
71             //Update the  ElanInterface Forwarding Container & ElanForwarding Container
72             deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
73             createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
74             updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
75         }
76
77     }
78
79     public void addElanInterfaceForwardingTableList(String elanInstanceName, String interfaceName,
80             StaticMacEntries staticMacEntries, TypedReadWriteTransaction<Operational> tx)
81             throws ExecutionException, InterruptedException {
82         MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true)
83                 .setMacAddress(staticMacEntries.getMacAddress())
84                 .setIpPrefix(staticMacEntries.getIpPrefix())
85                 .setInterface(interfaceName).withKey(new MacEntryKey(staticMacEntries.getMacAddress())).build();
86
87         createElanForwardingTablesList(elanInstanceName, macEntry, tx);
88         createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
89     }
90
91     public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac,
92                                                         TypedReadWriteTransaction<Operational> interfaceTx)
93             throws ExecutionException, InterruptedException {
94         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
95                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
96         MacEntry existingInterfaceMacEntry = elanUtils
97                 .getInterfaceMacEntriesOperationalDataPathFromId(interfaceTx, existingMacEntryId);
98         if (existingInterfaceMacEntry != null) {
99             interfaceTx.delete(existingMacEntryId);
100         }
101     }
102
103     public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac,
104             TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
105         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
106                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
107         MacEntry existingInterfaceMacEntry = elanUtils
108                 .getInterfaceMacEntriesOperationalDataPathFromId(tx, existingMacEntryId);
109         if (existingInterfaceMacEntry == null) {
110             MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
111                     .setInterface(interfaceName)
112                     .setIsStaticAddress(true).withKey(new MacEntryKey(mac.getMacAddress())).build();
113             tx.put(existingMacEntryId, macEntry, CREATE_MISSING_PARENTS);
114         }
115     }
116
117     public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac,
118             TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
119         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
120                 mac.getMacAddress());
121         MacEntry existingMacEntry = elanUtils.getMacEntryFromElanMacId(tx, macEntryId);
122         if (existingMacEntry != null && elanUtils.getElanMacTable(elanName) != null) {
123             MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true)
124                     .setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
125                     .withKey(new MacEntryKey(mac.getMacAddress())).build();
126             tx.put(macEntryId, newMacEntry);
127         }
128     }
129
130     private void createElanForwardingTablesList(String elanName, MacEntry macEntry,
131             TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
132         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
133                 macEntry.getMacAddress());
134         Optional<MacEntry> existingMacEntry = tx.read(macEntryId).get();
135         if (!existingMacEntry.isPresent() && elanUtils.getElanMacTable(elanName) != null) {
136             tx.put(macEntryId, macEntry, CREATE_MISSING_PARENTS);
137         }
138     }
139
140     public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
141             MacEntry macEntry) {
142         List<ListenableFuture<Void>> futures = new ArrayList<>();
143         futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, interfaceTx -> {
144             InstanceIdentifier<MacEntry> macEntryId = ElanUtils
145                     .getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
146             interfaceTx.delete(macEntryId);
147             deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, interfaceTx);
148             futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
149                 flowTx -> elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, flowTx)));
150         }));
151         for (ListenableFuture<Void> future : futures) {
152             ListenableFutures.addErrorLogging(future, LOG, "Error deleting ELAN interface forwarding entries");
153         }
154     }
155
156     public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress,
157             WriteTransaction tx) {
158         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
159                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
160         tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
161     }
162
163
164 }