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.internal;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
19 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
20 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.netvirt.elan.utils.ElanUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 @SuppressWarnings("deprecation")
39 public class ElanSmacFlowEventListener implements SalFlowListener {
41 private static final Logger LOG = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
43 private final DataBroker broker;
44 private final IInterfaceManager interfaceManager;
45 private final ElanUtils elanUtils;
47 public ElanSmacFlowEventListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils) {
49 this.interfaceManager = interfaceManager;
50 this.elanUtils = elanUtils;
54 public void onFlowAdded(FlowAdded arg0) {
55 // TODO Auto-generated method stub
60 public void onFlowRemoved(FlowRemoved flowRemoved) {
61 short tableId = flowRemoved.getTableId();
62 if (tableId == NwConstants.ELAN_SMAC_TABLE) {
63 BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
64 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
65 ElanTagName elanTagInfo = elanUtils.getElanInfoByElanTag(elanTag);
66 if (elanTagInfo == null) {
69 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
70 .getEthernetSource().getAddress().getValue().toUpperCase();
71 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
73 LOG.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
76 Optional<IfIndexInterface> existingInterfaceInfo = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
77 if (!existingInterfaceInfo.isPresent()) {
78 LOG.debug("Interface is not available for port Tag {}", portTag);
81 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
82 PhysAddress physAddress = new PhysAddress(srcMacAddress);
83 if (interfaceName == null) {
84 LOG.error(String.format("LPort record not found for tag %d", portTag));
87 MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
88 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
89 if (macEntry != null && interfaceInfo != null) {
90 WriteTransaction deleteFlowTx = broker.newWriteOnlyTransaction();
91 elanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanTagInfo.getName()), interfaceInfo,
92 macEntry, deleteFlowTx);
93 ListenableFuture<Void> result = deleteFlowTx.submit();
94 addCallBack(result, srcMacAddress);
96 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
97 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
98 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
99 .getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
100 WriteTransaction tx = broker.newWriteOnlyTransaction();
101 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
102 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
103 ListenableFuture<Void> writeResult = tx.submit();
104 addCallBack(writeResult, srcMacAddress);
109 private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
111 Futures.addCallback(writeResult, new FutureCallback<Void>() {
113 public void onSuccess(Void noarg) {
114 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
118 public void onFailure(Throwable error) {
119 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
125 public void onFlowUpdated(FlowUpdated arg0) {
126 // TODO Auto-generated method stub
131 public void onNodeErrorNotification(NodeErrorNotification arg0) {
132 // TODO Auto-generated method stub
137 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
138 // TODO Auto-generated method stub
143 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {