632440e23df2b7608a49f3c8e906ba7b331f77fd
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInterfaceStateClusteredListener.java
1 /*
2  * Copyright (c) 2016, 2017 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 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
13 import org.opendaylight.netvirt.elan.ElanException;
14 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
15 import org.opendaylight.netvirt.elan.utils.ElanUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class ElanInterfaceStateClusteredListener extends
25     AsyncClusteredDataTreeChangeListenerBase<Interface, ElanInterfaceStateClusteredListener> {
26
27     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceStateClusteredListener.class);
28
29     private final DataBroker broker;
30     private final ElanInterfaceManager elanInterfaceManager;
31     private final ElanUtils elanUtils;
32     private final ElanClusterUtils elanClusterUtils;
33
34     /* FIXME:
35      * Why do we have ElanInterfaceStateChangeListener and ElanInterfaceStateClusteredListener
36      * both within same module? Refactor this code into single listener.
37      */
38     public ElanInterfaceStateClusteredListener(DataBroker broker, ElanInterfaceManager elanInterfaceManager,
39                                                ElanUtils elanUtils, ElanClusterUtils elanClusterUtils) {
40         this.broker = broker;
41         this.elanInterfaceManager = elanInterfaceManager;
42         this.elanUtils = elanUtils;
43         this.elanClusterUtils = elanClusterUtils;
44     }
45
46     public void init() {
47         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
48     }
49
50     @Override
51     public InstanceIdentifier<Interface> getWildCardPath() {
52         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
53     }
54
55     @Override
56     protected void remove(InstanceIdentifier<Interface> identifier, Interface delIf) {
57     }
58
59     @Override
60     protected void update(InstanceIdentifier<Interface> identifier, Interface original, final Interface update) {
61         add(identifier, update);
62     }
63
64     @Override
65     protected void add(InstanceIdentifier<Interface> identifier, final Interface intrf) {
66         if (intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
67             if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
68                 final String interfaceName = intrf.getName();
69
70                 elanClusterUtils.runOnlyInOwnerNode("external tunnel update", () -> {
71                     LOG.debug("running external tunnel update job for interface {} added", interfaceName);
72                     try {
73                         handleExternalTunnelUpdate(interfaceName, intrf);
74                     } catch (ElanException e) {
75                         LOG.error("Failed to add Interface: " + identifier.toString());
76                     }
77                 });
78             }
79         }
80     }
81
82     private void handleExternalTunnelUpdate(String interfaceName, Interface update) throws ElanException {
83         ExternalTunnel externalTunnel = elanUtils.getExternalTunnel(interfaceName, LogicalDatastoreType.CONFIGURATION);
84         if (externalTunnel != null) {
85             LOG.debug("handling external tunnel update event for ext device dst {}  src {} ",
86                 externalTunnel.getDestinationDevice(), externalTunnel.getSourceDevice());
87             elanInterfaceManager.handleExternalTunnelStateEvent(externalTunnel, update);
88         } else {
89             LOG.trace("External tunnel not found with interfaceName: {}", interfaceName);
90         }
91     }
92
93     /* (non-Javadoc)
94      * @see org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase#getDataTreeChangeListener()
95      */
96     @Override
97     protected ElanInterfaceStateClusteredListener getDataTreeChangeListener() {
98         return this;
99     }
100
101 }