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