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