150dfbaa1c6fc52c57b0af59867ad3e8db12f086
[netvirt.git] /
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 final ElanUtils elanUtils;
34
35     @Inject
36     public ElanForwardingEntriesHandler(DataBroker dataBroker, ElanUtils elanUtils) {
37         this.broker = dataBroker;
38         this.elanUtils = elanUtils;
39     }
40
41     public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName,
42             String existingInterfaceName, MacEntry mac, WriteTransaction tx) {
43         if (existingInterfaceName.equals(interfaceName)) {
44             LOG.error(String.format(
45                     "Static MAC address %s has already been added for the same ElanInstance "
46                             + "%s on the same Logical Interface Port %s."
47                             + " No operation will be done.",
48                     mac.getMacAddress().toString(), elanInstanceName, interfaceName));
49         } else {
50             LOG.warn(String.format(
51                     "Static MAC address %s had already been added for ElanInstance %s on Logical Interface Port %s. "
52                             + "This would be considered as MAC movement scenario and old static mac will be removed "
53                             + "and new static MAC will be added"
54                             + "for ElanInstance %s on Logical Interface Port %s",
55                     mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName));
56             //Update the  ElanInterface Forwarding Container & ElanForwarding Container
57             deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
58             createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
59             updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
60         }
61
62     }
63
64     public void addElanInterfaceForwardingTableList(String elanInstanceName, String interfaceName,
65                                                     StaticMacEntries staticMacEntries, WriteTransaction tx) {
66         MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true)
67                 .setMacAddress(staticMacEntries.getMacAddress())
68                 .setIpPrefix(staticMacEntries.getIpPrefix())
69                 .setInterface(interfaceName).setKey(new MacEntryKey(staticMacEntries.getMacAddress())).build();
70
71         createElanForwardingTablesList(elanInstanceName, macEntry, tx);
72         createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
73     }
74
75     public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
76         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
77                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
78         MacEntry existingInterfaceMacEntry = elanUtils
79                 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
80         if (existingInterfaceMacEntry != null) {
81             tx.delete(LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
82         }
83     }
84
85     public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
86         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
87                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
88         MacEntry existingInterfaceMacEntry = elanUtils
89                 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
90         if (existingInterfaceMacEntry == null) {
91             MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
92                     .setInterface(interfaceName)
93                     .setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
94             tx.put(LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry,
95                     WriteTransaction.CREATE_MISSING_PARENTS);
96         }
97     }
98
99     public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac,
100             WriteTransaction tx) {
101         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
102                 mac.getMacAddress());
103         MacEntry existingMacEntry = elanUtils.getMacEntryFromElanMacId(macEntryId);
104         if (existingMacEntry != null && elanUtils.getElanMacTable(elanName) != null) {
105             MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true)
106                     .setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
107                     .setKey(new MacEntryKey(mac.getMacAddress())).build();
108             tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
109         }
110     }
111
112     private void createElanForwardingTablesList(String elanName, MacEntry macEntry, WriteTransaction tx) {
113         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
114                 macEntry.getMacAddress());
115         Optional<MacEntry> existingMacEntry = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
116         if (!existingMacEntry.isPresent() && elanUtils.getElanMacTable(elanName) != null) {
117             tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry, WriteTransaction.CREATE_MISSING_PARENTS);
118         }
119     }
120
121     public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
122             MacEntry macEntry, WriteTransaction tx) {
123         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
124                 .getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
125         tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
126         deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, tx);
127         WriteTransaction deleteFlowtx = broker.newWriteOnlyTransaction();
128         elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, deleteFlowtx);
129         deleteFlowtx.submit();
130     }
131
132     public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress,
133             WriteTransaction tx) {
134         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
135                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
136         tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
137     }
138
139
140 }