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.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
16 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
17 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.netvirt.elan.utils.ElanConstants;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
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
35 extends AsyncDataTreeChangeListenerBase<Interface, ElanInterfaceStateChangeListener> implements AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceStateChangeListener.class);
39 private final DataBroker broker;
40 private final ElanInterfaceManager elanInterfaceManager;
42 public ElanInterfaceStateChangeListener(final DataBroker db, final ElanInterfaceManager ifManager) {
43 super(Interface.class, ElanInterfaceStateChangeListener.class);
45 elanInterfaceManager = ifManager;
49 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
53 protected void remove(InstanceIdentifier<Interface> identifier, Interface delIf) {
54 LOG.trace("Received interface {} Down event", delIf);
55 String interfaceName = delIf.getName();
56 ElanInterface elanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
57 if (elanInterface == null) {
58 LOG.debug("No Elan Interface is created for the interface:{} ", interfaceName);
61 NodeConnectorId nodeConnectorId = new NodeConnectorId(delIf.getLowerLayerIf().get(0));
62 BigInteger dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
63 InterfaceInfo interfaceInfo = new InterfaceInfo(dpId, nodeConnectorId.getValue());
64 interfaceInfo.setInterfaceName(interfaceName);
65 interfaceInfo.setInterfaceType(InterfaceInfo.InterfaceType.VLAN_INTERFACE);
66 interfaceInfo.setInterfaceTag(delIf.getIfIndex());
67 String elanInstanceName = elanInterface.getElanInstanceName();
68 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanInstanceName);
69 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
70 InterfaceRemoveWorkerOnElan removeWorker = new InterfaceRemoveWorkerOnElan(elanInstanceName, elanInstance,
71 interfaceName, interfaceInfo, true, elanInterfaceManager);
72 coordinator.enqueueJob(elanInstanceName, removeWorker, ElanConstants.JOB_MAX_RETRIES);
76 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
77 LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
78 String interfaceName = update.getName();
79 if (update.getType() == null) {
80 LOG.trace("Interface type for interface {} is null", interfaceName);
83 if (update.getType().equals(Tunnel.class)) {
84 if (!original.getOperStatus().equals(Interface.OperStatus.Unknown)
85 && !update.getOperStatus().equals(Interface.OperStatus.Unknown)) {
86 if (update.getOperStatus().equals(Interface.OperStatus.Up)) {
87 InternalTunnel internalTunnel = getTunnelState(interfaceName);
88 if (internalTunnel != null) {
89 elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(),
90 internalTunnel.getDestinationDPN());
98 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
99 LOG.trace("Received interface {} up event", intrf);
100 String interfaceName = intrf.getName();
101 ElanInterface elanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
102 if (elanInterface == null) {
103 if (intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
104 if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
105 InternalTunnel internalTunnel = getTunnelState(interfaceName);
106 if (internalTunnel != null) {
107 elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(),
108 internalTunnel.getDestinationDPN());
114 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
115 .getElanInterfaceConfigurationDataPathId(interfaceName);
116 elanInterfaceManager.add(elanInterfaceId, elanInterface);
120 public void close() throws Exception {
124 public InternalTunnel getTunnelState(String interfaceName) {
125 InternalTunnel internalTunnel = null;
126 TunnelList tunnelList = ElanUtils.buildInternalTunnel(broker);
127 if (tunnelList != null && tunnelList.getInternalTunnel() != null) {
128 List<InternalTunnel> internalTunnels = tunnelList.getInternalTunnel();
129 for (InternalTunnel tunnel : internalTunnels) {
130 if (tunnel.getTunnelInterfaceName().equalsIgnoreCase(interfaceName)) {
131 internalTunnel = tunnel;
136 return internalTunnel;
140 protected InstanceIdentifier<Interface> getWildCardPath() {
141 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
146 protected ElanInterfaceStateChangeListener getDataTreeChangeListener() {