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.vpnservice.elan.internal;
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
16 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
17 import org.opendaylight.vpnservice.itm.api.IITMProvider;
18 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
19 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
20 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntryBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntryKey;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class ElanForwardingEntriesHandler extends AbstractDataChangeListener<ElanInterface> implements AutoCloseable {
36 private static final Logger logger = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
37 private DataBroker broker;
38 private ListenerRegistration<DataChangeListener> listenerRegistration;
40 public ElanForwardingEntriesHandler(DataBroker db){
41 super(ElanInterface.class);
45 private InstanceIdentifier<?> getWildCardPath() {
46 return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
49 public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName, String existingInterfaceName, MacEntry mac) {
50 if(existingInterfaceName == interfaceName) {
51 logger.error(String.format("Static MAC address %s has already been added for the same ElanInstance %s on the same Logical Interface Port %s."
52 + " No operation will be done.", mac.getMacAddress().toString(), elanInstanceName, interfaceName));
54 logger.warn(String.format("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 and new static MAC will be added"
56 + "for ElanInstance %s on Logical Interface Port %s", mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName));
57 //Update the ElanInterface Forwarding Container & ElanForwarding Container
58 deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac);
59 createElanInterfaceForwardingTablesList(interfaceName, mac);
60 updateElanForwardingTablesList(elanInstanceName, interfaceName, mac);
65 public void addElanInterfaceForwardingTableList(ElanInstance elanInstance, String interfaceName, PhysAddress physAddress) {
66 MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true).setMacAddress(physAddress).setInterface(interfaceName).setKey(new MacEntryKey(physAddress)).build();
67 createElanForwardingTablesList(elanInstance.getElanInstanceName(), macEntry);
68 createElanInterfaceForwardingTablesList(interfaceName, macEntry);
71 public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac) {
72 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
73 MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
74 if(existingInterfaceMacEntry != null) {
75 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
79 public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac) {
80 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
81 MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
82 if(existingInterfaceMacEntry == null) {
83 MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setInterface(interfaceName).setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
84 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry);
89 public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac) {
90 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, mac.getMacAddress());
91 MacEntry existingMacEntry = ElanUtils.getMacEntryFromElanMacId(macEntryId);
92 if(existingMacEntry != null) {
93 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
94 MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true).setMacAddress(mac.getMacAddress()).setKey(new MacEntryKey(mac.getMacAddress())).build();
95 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
99 private void createElanForwardingTablesList(String elanName, MacEntry macEntry) {
100 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, macEntry.getMacAddress());
101 Optional<MacEntry> existingMacEntry = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
102 if(!existingMacEntry.isPresent()) {
103 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry);
107 public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo, MacEntry macEntry) {
108 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
109 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
110 ElanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry);
113 public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress) {
114 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
115 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
119 protected void remove(InstanceIdentifier<ElanInterface> identifier, ElanInterface del) {
124 protected void update(InstanceIdentifier<ElanInterface> identifier, ElanInterface original, ElanInterface update) {
129 protected void add(InstanceIdentifier<ElanInterface> identifier, ElanInterface add) {
134 public void close() throws Exception {
135 if (listenerRegistration != null) {
137 listenerRegistration.close();
138 } catch (final Exception e) {
139 logger.error("Error when cleaning up Elan DataChangeListener.", e);
141 listenerRegistration = null;