49ce7e0a24e52baab6785de55832c378cf4a1270
[netvirt.git] / vpnservice / elanmanager / 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 com.google.common.base.Optional;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Singleton
28 public class ElanForwardingEntriesHandler {
29
30     private static final Logger LOG = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
31
32     private final DataBroker broker;
33     private ElanUtils elanUtils;
34
35     @Inject
36     public ElanForwardingEntriesHandler(DataBroker dataBroker) {
37         this.broker = dataBroker;
38     }
39
40     public void setElanUtils(ElanUtils elanUtils) {
41         this.elanUtils = elanUtils;
42     }
43
44     public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName,
45             String existingInterfaceName, MacEntry mac, WriteTransaction tx) {
46         if (existingInterfaceName.equals(interfaceName)) {
47             LOG.error(String.format(
48                     "Static MAC address %s has already been added for the same ElanInstance "
49                             + "%s on the same Logical Interface Port %s."
50                             + " No operation will be done.",
51                     mac.getMacAddress().toString(), elanInstanceName, interfaceName));
52         } else {
53             LOG.warn(String.format(
54                     "Static MAC address %s had already been added for ElanInstance %s on Logical Interface Port %s. "
55                             + "This would be considered as MAC movement scenario and old static mac will be removed "
56                             + "and new static MAC will be added"
57                             + "for ElanInstance %s on Logical Interface Port %s",
58                     mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName));
59             //Update the  ElanInterface Forwarding Container & ElanForwarding Container
60             deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
61             createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
62             updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
63         }
64
65     }
66
67     public void addElanInterfaceForwardingTableList(ElanInstance elanInstance, String interfaceName,
68                                                     StaticMacEntries staticMacEntries, WriteTransaction tx) {
69         MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true)
70                 .setMacAddress(staticMacEntries.getMacAddress())
71                 .setIpPrefix(staticMacEntries.getIpPrefix())
72                 .setInterface(interfaceName).setKey(new MacEntryKey(staticMacEntries.getMacAddress())).build();
73
74         createElanForwardingTablesList(elanInstance.getElanInstanceName(), macEntry, tx);
75         createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
76     }
77
78     public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
79         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
80                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
81         MacEntry existingInterfaceMacEntry = elanUtils
82                 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
83         if (existingInterfaceMacEntry != null) {
84             tx.delete(LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
85         }
86     }
87
88     public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
89         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
90                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
91         MacEntry existingInterfaceMacEntry = elanUtils
92                 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
93         if (existingInterfaceMacEntry == null) {
94             MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
95                     .setInterface(interfaceName)
96                     .setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
97             tx.put(LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry, true);
98         }
99     }
100
101     public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac,
102             WriteTransaction tx) {
103         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
104                 mac.getMacAddress());
105         MacEntry existingMacEntry = elanUtils.getMacEntryFromElanMacId(macEntryId);
106         if (existingMacEntry != null && elanUtils.getElanMacTable(elanName) != null) {
107             MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true)
108                     .setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
109                     .setKey(new MacEntryKey(mac.getMacAddress())).build();
110             tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
111         }
112     }
113
114     private void createElanForwardingTablesList(String elanName, MacEntry macEntry, WriteTransaction tx) {
115         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
116                 macEntry.getMacAddress());
117         Optional<MacEntry> existingMacEntry = elanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
118         if (!existingMacEntry.isPresent() && elanUtils.getElanMacTable(elanName) != null) {
119             tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry, true);
120         }
121     }
122
123     public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
124             MacEntry macEntry, WriteTransaction tx) {
125         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
126                 .getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
127         tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
128         deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, tx);
129         WriteTransaction deleteFlowtx = broker.newWriteOnlyTransaction();
130         elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, deleteFlowtx);
131         deleteFlowtx.submit();
132     }
133
134     public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress,
135             WriteTransaction tx) {
136         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
137                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
138         tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
139     }
140
141
142 }