2 * Copyright (c) 2016, 2017 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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
21 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
22 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
23 import org.opendaylight.netvirt.elan.utils.ElanConstants;
24 import org.opendaylight.netvirt.elan.utils.ElanUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class ElanInterfaceStateChangeListener
37 extends AsyncDataTreeChangeListenerBase<Interface, ElanInterfaceStateChangeListener> {
39 private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceStateChangeListener.class);
41 private final DataBroker broker;
42 private final ElanInterfaceManager elanInterfaceManager;
43 private final JobCoordinator jobCoordinator;
44 private final ElanInstanceCache elanInstanceCache;
45 private final ElanInterfaceCache elanInterfaceCache;
48 public ElanInterfaceStateChangeListener(final DataBroker db, final ElanInterfaceManager ifManager,
49 final JobCoordinator jobCoordinator, final ElanInstanceCache elanInstanceCache,
50 final ElanInterfaceCache elanInterfaceCache) {
51 super(Interface.class, ElanInterfaceStateChangeListener.class);
53 elanInterfaceManager = ifManager;
54 this.jobCoordinator = jobCoordinator;
55 this.elanInstanceCache = elanInstanceCache;
56 this.elanInterfaceCache = elanInterfaceCache;
62 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
66 protected void remove(InstanceIdentifier<Interface> identifier, Interface delIf) {
67 if (!L2vlan.class.equals(delIf.getType())) {
70 LOG.trace("Received interface {} Down event", delIf);
71 String interfaceName = delIf.getName();
72 Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
73 if (!elanInterface.isPresent()) {
74 LOG.debug("No Elan Interface is created for the interface:{} ", interfaceName);
77 NodeConnectorId nodeConnectorId = new NodeConnectorId(delIf.getLowerLayerIf().get(0));
78 BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
79 InterfaceInfo interfaceInfo = new InterfaceInfo(dpId, nodeConnectorId.getValue());
80 interfaceInfo.setInterfaceName(interfaceName);
81 interfaceInfo.setInterfaceType(InterfaceInfo.InterfaceType.VLAN_INTERFACE);
82 interfaceInfo.setInterfaceTag(delIf.getIfIndex());
83 String elanInstanceName = elanInterface.get().getElanInstanceName();
84 ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orNull();
85 if (elanInstance == null) {
86 LOG.debug("No Elan instance is available for the interface:{} ", interfaceName);
89 InterfaceRemoveWorkerOnElan removeWorker = new InterfaceRemoveWorkerOnElan(elanInstanceName, elanInstance,
90 interfaceName, interfaceInfo, elanInterfaceManager);
91 jobCoordinator.enqueueJob(elanInstanceName, removeWorker, ElanConstants.JOB_MAX_RETRIES);
95 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
99 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
100 if (!L2vlan.class.equals(intrf.getType())) {
103 LOG.trace("Received interface {} up event", intrf);
104 String interfaceName = intrf.getName();
105 Optional<ElanInterface> elanInterface = elanInterfaceCache.get(interfaceName);
106 if (!elanInterface.isPresent()) {
109 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
110 .getElanInterfaceConfigurationDataPathId(interfaceName);
111 elanInterfaceManager.add(elanInterfaceId, elanInterface.get());
115 protected InstanceIdentifier<Interface> getWildCardPath() {
116 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
121 protected ElanInterfaceStateChangeListener getDataTreeChangeListener() {