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.ElanUtils;
16 import org.opendaylight.netvirt.elan.utils.ElanConstants;
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 {
44 private final DataBroker broker;
45 private IInterfaceManager interfaceManager;
46 private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
48 public ElanSmacFlowEventListener(DataBroker dataBroker) {
51 private SalFlowService salFlowService;
53 public SalFlowService getSalFlowService() {
54 return this.salFlowService;
57 public void setSalFlowService(final SalFlowService salFlowService) {
58 this.salFlowService = salFlowService;
60 public void setInterfaceManager(IInterfaceManager interfaceManager) {
61 this.interfaceManager = interfaceManager;
64 public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
67 public void onFlowAdded(FlowAdded arg0) {
68 // TODO Auto-generated method stub
72 public void onFlowRemoved(FlowRemoved flowRemoved) {
73 short tableId = flowRemoved.getTableId();
74 if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
75 BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
76 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
77 ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
78 if (elanTagInfo == null) {
81 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
82 .getEthernetSource().getAddress().getValue().toUpperCase();
83 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
85 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
88 Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
89 if (!existingInterfaceInfo.isPresent()) {
90 logger.debug("Interface is not available for port Tag {}", portTag);
93 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
94 PhysAddress physAddress = new PhysAddress(srcMacAddress);
95 if (interfaceName == null) {
96 logger.error(String.format("LPort record not found for tag %d", portTag));
99 MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
100 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
101 if(macEntry != null && interfaceInfo != null) {
102 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry);
104 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
105 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
106 WriteTransaction tx = broker.newWriteOnlyTransaction();
107 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
108 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
109 ListenableFuture<Void> writeResult = tx.submit();
112 Futures.addCallback(writeResult, new FutureCallback<Void>() {
114 public void onSuccess(Void noarg) {
115 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
119 public void onFailure(Throwable error) {
120 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
127 public void onFlowUpdated(FlowUpdated arg0) {
128 // TODO Auto-generated method stub
132 public void onNodeErrorNotification(NodeErrorNotification arg0) {
133 // TODO Auto-generated method stub
137 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
138 // TODO Auto-generated method stub
142 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
143 // TODO Auto-generated method stub