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;
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.vpnservice.elan.utils.ElanConstants;
16 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
17 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
18 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
19 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
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.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.vpnservice.elan.rev150602.elan.tag.name.map.ElanTagName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._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;
65 public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
68 public void onFlowAdded(FlowAdded arg0) {
69 // TODO Auto-generated method stub
74 public void onFlowRemoved(FlowRemoved arg0) {
75 // TODO Auto-generated method stub
80 public void onFlowUpdated(FlowUpdated arg0) {
81 // TODO Auto-generated method stub
86 public void onNodeErrorNotification(NodeErrorNotification arg0) {
87 // TODO Auto-generated method stub
92 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
93 // TODO Auto-generated method stub
98 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
99 short tableId = switchFlowRemoved.getTableId();
100 if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
101 BigInteger metadata = switchFlowRemoved.getMatch().getMetadata().getMetadata();
102 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
103 ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
104 if (elanTagInfo == null) {
107 final String srcMacAddress = switchFlowRemoved.getMatch().getEthernetMatch()
108 .getEthernetSource().getAddress().getValue().toUpperCase();
109 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
111 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
114 Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
115 if (!existingInterfaceInfo.isPresent()) {
116 logger.debug("Interface is not available for port Tag {}", portTag);
119 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
120 PhysAddress physAddress = new PhysAddress(srcMacAddress);
121 if (interfaceName == null) {
122 logger.error(String.format("LPort record not found for tag %d", portTag));
125 MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
126 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
127 if(macEntry != null && interfaceInfo != null) {
128 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry);
130 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
131 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
132 WriteTransaction tx = broker.newWriteOnlyTransaction();
133 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
134 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
135 ListenableFuture<Void> writeResult = tx.submit();
138 Futures.addCallback(writeResult, new FutureCallback<Void>() {
140 public void onSuccess(Void noarg) {
141 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
145 public void onFailure(Throwable error) {
146 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);