Optional clean-up
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInterfaceStateChangeListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.internal;
9
10
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.mdsalutil.MDSALUtil;
19 import org.opendaylight.netvirt.elan.ElanException;
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;
33
34 public class ElanInterfaceStateChangeListener
35         extends AsyncDataTreeChangeListenerBase<Interface, ElanInterfaceStateChangeListener> implements AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceStateChangeListener.class);
38
39     private final DataBroker broker;
40     private final ElanInterfaceManager elanInterfaceManager;
41
42     public ElanInterfaceStateChangeListener(final DataBroker db, final ElanInterfaceManager ifManager) {
43         super(Interface.class, ElanInterfaceStateChangeListener.class);
44         broker = db;
45         elanInterfaceManager = ifManager;
46     }
47
48     public void init() {
49         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
50     }
51
52     @Override
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);
59             return;
60         }
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);
73     }
74
75     @Override
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);
81             return;
82         }
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                         try {
90                             elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(),
91                                     internalTunnel.getDestinationDPN());
92                         } catch (ElanException e) {
93                             LOG.error("Failed to update interface: " + identifier.toString(), e);
94                         }
95                     }
96                 }
97             }
98         }
99     }
100
101     @Override
102     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
103         LOG.trace("Received interface {} up event", intrf);
104         String interfaceName =  intrf.getName();
105         ElanInterface elanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
106         if (elanInterface == null) {
107             if (intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
108                 if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
109                     InternalTunnel internalTunnel = getTunnelState(interfaceName);
110                     if (internalTunnel != null) {
111                         try {
112                             elanInterfaceManager.handleInternalTunnelStateEvent(internalTunnel.getSourceDPN(),
113                                 internalTunnel.getDestinationDPN());
114                         } catch (ElanException e) {
115                             LOG.error("Failed to add interface: " + identifier.toString(), e);
116                         }
117                     }
118                 }
119             }
120             return;
121         }
122         InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
123                 .getElanInterfaceConfigurationDataPathId(interfaceName);
124         elanInterfaceManager.add(elanInterfaceId, elanInterface);
125     }
126
127     @Override
128     public void close() throws Exception {
129
130     }
131
132     public  InternalTunnel getTunnelState(String interfaceName) {
133         InternalTunnel internalTunnel = null;
134         TunnelList tunnelList = ElanUtils.buildInternalTunnel(broker);
135         if (tunnelList != null && tunnelList.getInternalTunnel() != null) {
136             List<InternalTunnel> internalTunnels = tunnelList.getInternalTunnel();
137             for (InternalTunnel tunnel : internalTunnels) {
138                 if (tunnel.getTunnelInterfaceName().equalsIgnoreCase(interfaceName)) {
139                     internalTunnel = tunnel;
140                     break;
141                 }
142             }
143         }
144         return internalTunnel;
145     }
146
147     @Override
148     protected InstanceIdentifier<Interface> getWildCardPath() {
149         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
150     }
151
152
153     @Override
154     protected ElanInterfaceStateChangeListener getDataTreeChangeListener() {
155         return this;
156     }
157
158 }