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 java.util.Optional;
11 import java.math.BigInteger;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
21 import org.opendaylight.netvirt.cloudservicechain.utils.ElanServiceChainUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev160711.ElanServiceChainState;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev160711.elan.to.pseudo.port.data.list.ElanToPseudoPortData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Listener responsible for maintaining the Elan-Pseudo ports installed
34 * wherever the ELAN is. Listens for ElanInterfaces being added/removed
35 * to/from a DPN so that Elan2Scf and Scf2Elan flows are installed/removed
39 public class ElanDpnInterfacesListener
40 extends AsyncDataTreeChangeListenerBase<DpnInterfaces, ElanDpnInterfacesListener> {
42 private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfacesListener.class);
43 private final DataBroker broker;
44 private final IMdsalApiManager mdsalManager;
47 public ElanDpnInterfacesListener(final DataBroker db, final IMdsalApiManager mdsalMgr) {
48 super(DpnInterfaces.class, ElanDpnInterfacesListener.class);
50 this.mdsalManager = mdsalMgr;
56 LOG.info("{} init", getClass().getSimpleName());
57 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
61 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
62 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
63 .child(DpnInterfaces.class).build();
67 protected ElanDpnInterfacesListener getDataTreeChangeListener() {
68 return ElanDpnInterfacesListener.this;
72 protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
73 final String elanName = getElanName(identifier);
74 BigInteger addDpnId = dpnInterfaces.getDpId();
75 LOG.debug("ELAN interfaces {} added on DPN {} for Elan {}", dpnInterfaces.getInterfaces(), addDpnId, elanName);
76 Optional<ElanServiceChainState> elanServiceChainState = ElanServiceChainUtils
77 .getElanServiceChainState(broker, elanName);
78 if (elanServiceChainState.isPresent()) {
79 List<ElanToPseudoPortData> elanToPseudoPortDataList = elanServiceChainState.get().getElanToPseudoPortData();
80 for (ElanToPseudoPortData elanToPseudoPortData : elanToPseudoPortDataList) {
81 Long scfTag = elanToPseudoPortData.getScfTag();
82 Long elanLportTag = elanToPseudoPortData.getElanLportTag();
83 if (elanLportTag != null && scfTag != null) {
84 short tableId = NwConstants.SCF_DOWN_SUB_FILTER_TCP_BASED_TABLE;
85 handleUpdate(addDpnId, elanName, tableId, elanLportTag.intValue() /*21 bit*/ ,
86 scfTag, NwConstants.ADD_FLOW);
88 LOG.debug("Could not find lportTag for ELAN={}", elanName);
95 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
96 final String elanName = getElanName(identifier);
97 BigInteger removeDpnId = dpnInterfaces.getDpId();
98 LOG.debug("ELAN interfaces {} removed from on DPN {} for ELAN {}",
99 dpnInterfaces.getInterfaces(), removeDpnId, elanName);
100 Optional<ElanServiceChainState> elanServiceChainState = ElanServiceChainUtils
101 .getElanServiceChainState(broker, elanName);
102 if (elanServiceChainState.isPresent()) {
103 List<ElanToPseudoPortData> elanToPseudoPortDataList = elanServiceChainState.get().getElanToPseudoPortData();
104 for (ElanToPseudoPortData elanToPseudoPortData : elanToPseudoPortDataList) {
105 Long scfTag = elanToPseudoPortData.getScfTag();
106 Long elanLportTag = elanToPseudoPortData.getElanLportTag();
107 if (scfTag != null && elanLportTag != null) {
108 handleUpdate(removeDpnId, elanName, (short) 0 /* tableId, ignored in removals */,
109 elanLportTag.intValue() /*21 bit*/ , 0 /* scfTag, ignored in removals */,
110 NwConstants.DEL_FLOW);
112 LOG.debug("One of scfTag or lPortTag is null for ELAN={}: scfTag={} lportTag={}",
113 elanName, scfTag, elanLportTag);
120 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
121 final DpnInterfaces dpnInterfaces) {
125 private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
126 return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
129 private void handleUpdate(BigInteger dpnId, String elanName, short tableId, int elanLportTag, long scfTag,
131 Optional<ElanInstance> elanInstance = ElanServiceChainUtils.getElanInstanceByName(broker, elanName);
132 if (!elanInstance.isPresent()) {
133 LOG.debug("Could not find an ELAN Instance with name={}", elanName);
137 Long vni = elanInstance.get().getSegmentationId();
138 int elanTag = elanInstance.get().getElanTag().intValue();
140 ElanServiceChainUtils.programLPortDispatcherToScf(mdsalManager, dpnId, elanTag, elanLportTag, tableId, scfTag,
142 ElanServiceChainUtils.programLPortDispatcherFromScf(mdsalManager, dpnId, elanLportTag, elanTag, addOrRemove);
143 ElanServiceChainUtils.programExternalTunnelTable(mdsalManager, dpnId, elanLportTag, vni, elanTag, addOrRemove);