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.cloudservicechain.listeners;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev170511.elan.to.pseudo.port.data.list.ElanToPseudoPortData;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
22 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.netvirt.cloudservicechain.utils.ElanServiceChainUtils;
24 import org.opendaylight.netvirt.cloudservicechain.CloudServiceChainConstants;
25 import org.opendaylight.genius.mdsalutil.NwConstants;
26 import java.math.BigInteger;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Optional;
32 public class ElanDpnInterfacesListener extends AbstractDataChangeListener<DpnInterfaces> implements AutoCloseable {
34 private ListenerRegistration<DataChangeListener> listenerRegistration;
35 private static final Logger logger = LoggerFactory.getLogger(ElanDpnInterfacesListener.class);
36 private final DataBroker broker;
37 private IMdsalApiManager mdsalManager;
39 public ElanDpnInterfacesListener(final DataBroker db, final IMdsalApiManager mdsalMgr) {
40 super(DpnInterfaces.class);
42 this.mdsalManager = mdsalMgr;
46 private void registerListener(final DataBroker db) {
48 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
50 ElanDpnInterfacesListener.this,
51 AsyncDataBroker.DataChangeScope.SUBTREE);
52 } catch (final Exception e) {
53 logger.error("ElanDpnInterfacesListener: DataChange listener registration fail!", e);
54 throw new IllegalStateException("ElanDpnInterfacesListener: registration Listener failed.", e);
58 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
59 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
60 .child(DpnInterfaces.class).build();
64 protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
65 final String elanName = getElanName(identifier);
66 BigInteger addDpnId = dpnInterfaces.getDpId();
67 Optional<ElanToPseudoPortData> elanLPortListOpc = ElanServiceChainUtils.getElanToLportTagList(broker, elanName);
68 if (elanLPortListOpc.isPresent()) {
69 int scfTag = elanLPortListOpc.get().getScfTag();
70 Long elanLportTag = elanLPortListOpc.get().getElanLportTag();
71 if ( elanLportTag != null ) {
72 short tableId = NwConstants.SCF_DOWN_SUB_FILTER_TCP_BASED_TABLE;
73 handleUpdate(addDpnId, elanName, tableId, elanLportTag.intValue(), scfTag, NwConstants.ADD_FLOW);
75 logger.debug("Could not find lportTag for elan={}", elanName);
82 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
83 final String elanName = getElanName(identifier);
84 BigInteger removeDpnId = dpnInterfaces.getDpId();
85 Optional<ElanToPseudoPortData> elanLPortListOpc = ElanServiceChainUtils.getElanToLportTagList(broker, elanName);
86 if (elanLPortListOpc.isPresent()) {
87 Integer scfTag = elanLPortListOpc.get().getScfTag();
88 Long elanLportTag = elanLPortListOpc.get().getElanLportTag();
89 if ( scfTag != null && elanLportTag != null ) {
90 handleUpdate(removeDpnId, elanName, (short) 0 /* tableId, ignored in removals */,
91 elanLportTag.intValue(), 0 /* scfTag, ignored in removals */, NwConstants.DEL_FLOW);
93 logger.debug("One of scfTag or lPortTag is null for elan={}: scfTag={} lportTag={}",
94 elanName, scfTag, elanLportTag);
100 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
101 final DpnInterfaces dpnInterfaces) {
106 public void close() {
107 if (listenerRegistration != null) {
109 listenerRegistration.close();
110 } catch (final Exception e) {
111 logger.error("Error when cleaning up DataChangeListener.", e);
113 listenerRegistration = null;
115 logger.info("ElanDpnInterfaces listener Closed");
118 private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
119 return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
122 private void handleUpdate(BigInteger dpnId, String elanName, short tableId, int elanLportTag, int scfTag, int addOrRemove) {
123 Optional<ElanInstance> elanInstance = ElanServiceChainUtils.getElanInstanceByName(broker, elanName);
124 if ( !elanInstance.isPresent() ) {
125 logger.debug("Could not find an Elan Instance with name={}", elanName);
129 Long vni = elanInstance.get().getSegmentationId();
130 int elanTag = elanInstance.get().getElanTag().intValue();
132 ElanServiceChainUtils.programLPortDispatcherToScf(mdsalManager, dpnId, elanTag, elanLportTag, tableId, scfTag,
134 ElanServiceChainUtils.programLPortDispatcherFromScf(mdsalManager, dpnId, elanLportTag, elanTag, addOrRemove);
135 ElanServiceChainUtils.programExternalTunnelTable(mdsalManager, dpnId, elanLportTag, vni, elanTag, addOrRemove);