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;
11 import java.math.BigInteger;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
16 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
17 import org.opendaylight.netvirt.elan.utils.ElanConstants;
18 import org.opendaylight.netvirt.elan.utils.ElanUtils;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class ElanInterfaceStateChangeListener extends AsyncDataTreeChangeListenerBase<Interface,ElanInterfaceStateChangeListener> implements AutoCloseable {
36 private static final Logger logger = LoggerFactory.getLogger(ElanInterfaceStateChangeListener.class);
38 private final DataBroker broker;
39 private final ElanInterfaceManager elanInterfaceManager;
40 private final IInterfaceManager interfaceManager;
41 private final ElanUtils elanUtils;
43 public ElanInterfaceStateChangeListener(final DataBroker db, final ElanInterfaceManager ifManager,
44 final IInterfaceManager interfaceManager, ElanUtils elanUtils) {
45 super(Interface.class, ElanInterfaceStateChangeListener.class);
47 elanInterfaceManager = ifManager;
48 this.interfaceManager = interfaceManager;
49 this.elanUtils = elanUtils;
53 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
57 protected void remove(InstanceIdentifier<Interface> identifier, Interface delIf) {
58 logger.trace("Received interface {} Down event", delIf);
59 String interfaceName = delIf.getName();
60 ElanInterface elanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
61 if (elanInterface == null) {
62 logger.debug("No Elan Interface is created for the interface:{} ", interfaceName);
65 NodeConnectorId nodeConnectorId = new NodeConnectorId(delIf.getLowerLayerIf().get(0));
66 BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
67 InterfaceInfo interfaceInfo = new InterfaceInfo(dpId, nodeConnectorId.getValue());
68 interfaceInfo.setInterfaceName(interfaceName);
69 interfaceInfo.setInterfaceType(InterfaceInfo.InterfaceType.VLAN_INTERFACE);
70 interfaceInfo.setInterfaceTag(delIf.getIfIndex());
71 String elanInstanceName = elanInterface.getElanInstanceName();
72 ElanInstance elanInstance = elanUtils.getElanInstanceByName(elanInstanceName);
73 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
74 InterfaceRemoveWorkerOnElan removeWorker = new InterfaceRemoveWorkerOnElan(elanInstanceName, elanInstance,
75 interfaceName, interfaceInfo, true, elanInterfaceManager);
76 coordinator.enqueueJob(elanInstanceName, removeWorker, ElanConstants.JOB_MAX_RETRIES);
80 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
81 logger.trace("Operation Interface update event - Old: {}, New: {}", original, update);
82 String interfaceName = update.getName();
83 if (update.getType() == null) {
84 logger.trace("Interface type for interface {} is null", interfaceName);
87 if (update.getType().equals(Tunnel.class)) {
88 if (!original.getOperStatus().equals(Interface.OperStatus.Unknown) && !update.getOperStatus().equals(Interface.OperStatus.Unknown)){
89 if (update.getOperStatus().equals(Interface.OperStatus.Up)) {
90 InternalTunnel internalTunnel = getTunnelState(interfaceName);
91 if (internalTunnel != null) {
92 elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(), internalTunnel.getDestinationDPN());
100 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
101 logger.trace("Received interface {} up event", intrf);
102 String interfaceName = intrf.getName();
103 ElanInterface elanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
104 if (elanInterface == null) {
105 if (intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
106 if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
107 InternalTunnel internalTunnel = getTunnelState(interfaceName);
108 if (internalTunnel != null) {
109 elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(),
110 internalTunnel.getDestinationDPN());
116 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName);
117 elanInterfaceManager.add(elanInterfaceId, elanInterface);
121 public void close() throws Exception {
125 public InternalTunnel getTunnelState(String interfaceName) {
126 InternalTunnel internalTunnel = null;
127 TunnelList tunnelList = elanUtils.buildInternalTunnel(broker);
128 if (tunnelList != null && tunnelList.getInternalTunnel() != null) {
129 List<InternalTunnel> internalTunnels = tunnelList.getInternalTunnel();
130 for (InternalTunnel tunnel : internalTunnels) {
131 if (tunnel.getTunnelInterfaceName().equalsIgnoreCase(interfaceName)) {
132 internalTunnel = tunnel;
137 return internalTunnel;
140 protected InstanceIdentifier<Interface> getWildCardPath() {
141 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
146 protected ElanInterfaceStateChangeListener getDataTreeChangeListener() {