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 java.math.BigInteger;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.netvirt.elan.utils.ElanConstants;
16 import org.opendaylight.netvirt.elan.utils.ElanUtils;
17 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
20 import org.opendaylight.genius.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.flow.service.rev130819.FlowAdded;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.base.Optional;
38 import com.google.common.util.concurrent.FutureCallback;
39 import com.google.common.util.concurrent.Futures;
40 import com.google.common.util.concurrent.ListenableFuture;
42 @SuppressWarnings("deprecation")
43 public class ElanSmacFlowEventListener implements SalFlowListener {
45 private ElanServiceProvider elanServiceProvider = null;
46 private static volatile ElanSmacFlowEventListener elanSmacFlowEventListener = null;
47 private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
49 public ElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
50 this.elanServiceProvider = elanServiceProvider;
53 public static ElanSmacFlowEventListener getElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
54 if (elanSmacFlowEventListener == null)
55 synchronized (ElanPacketInHandler.class) {
56 if (elanSmacFlowEventListener == null) {
57 elanSmacFlowEventListener = new ElanSmacFlowEventListener(elanServiceProvider);
60 return elanSmacFlowEventListener;
64 public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
67 public void onFlowAdded(FlowAdded arg0) {
68 // TODO Auto-generated method stub
73 public void onFlowRemoved(FlowRemoved flowRemoved) {
74 short tableId = flowRemoved.getTableId();
75 if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
76 BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
77 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
78 ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
79 if (elanTagInfo == null) {
82 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
83 .getEthernetSource().getAddress().getValue().toUpperCase();
84 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
86 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
89 Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
90 if (!existingInterfaceInfo.isPresent()) {
91 logger.debug("Interface is not available for port Tag {}", portTag);
94 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
95 PhysAddress physAddress = new PhysAddress(srcMacAddress);
96 if (interfaceName == null) {
97 logger.error(String.format("LPort record not found for tag %d", portTag));
100 MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
101 InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(interfaceName);
102 if (macEntry != null && interfaceInfo != null) {
103 WriteTransaction deleteFlowTx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
104 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry, deleteFlowTx);
105 ListenableFuture<Void> result = deleteFlowTx.submit();
106 addCallBack(result, srcMacAddress);
108 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
109 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
110 WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
111 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
112 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
113 ListenableFuture<Void> writeResult = tx.submit();
114 addCallBack(writeResult, srcMacAddress);
119 private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
121 Futures.addCallback(writeResult, new FutureCallback<Void>() {
123 public void onSuccess(Void noarg) {
124 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
128 public void onFailure(Throwable error) {
129 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
135 public void onFlowUpdated(FlowUpdated arg0) {
136 // TODO Auto-generated method stub
141 public void onNodeErrorNotification(NodeErrorNotification arg0) {
142 // TODO Auto-generated method stub
147 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
148 // TODO Auto-generated method stub
153 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {