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.vpnservice.elan.internal;
10 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
16 import org.opendaylight.vpnservice.elan.utils.ElanClusterUtils;
17 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.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.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class ElanInterfaceStateClusteredListener extends
28 AsyncClusteredDataChangeListenerBase<Interface, ElanInterfaceStateClusteredListener> implements AutoCloseable {
29 private DataBroker broker;
30 private ElanInterfaceManager elanInterfaceManager;
31 private ListenerRegistration<DataChangeListener> listenerRegistration;
33 private static final Logger logger = LoggerFactory.getLogger(ElanInterfaceStateClusteredListener.class);
35 public ElanInterfaceStateClusteredListener(final DataBroker db, final ElanInterfaceManager ifManager) {
36 super(Interface.class, ElanInterfaceStateClusteredListener.class);
38 elanInterfaceManager = ifManager;
42 private void registerListener(final DataBroker db) {
44 listenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
45 getWildCardPath(), ElanInterfaceStateClusteredListener.this, AsyncDataBroker.DataChangeScope.BASE);
46 } catch (final Exception e) {
47 logger.error("Elan Interfaces DataChange listener registration fail!", e);
48 throw new IllegalStateException("ElanInterface registration Listener failed.", e);
53 public InstanceIdentifier<Interface> getWildCardPath() {
54 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
58 protected ClusteredDataChangeListener getDataChangeListener() {
59 return ElanInterfaceStateClusteredListener.this;
63 protected AsyncDataBroker.DataChangeScope getDataChangeScope() {
64 return AsyncDataBroker.DataChangeScope.BASE;
68 protected void remove(InstanceIdentifier<Interface> identifier, Interface delIf) {
72 protected void update(InstanceIdentifier<Interface> identifier, Interface original, final Interface update) {
73 add(identifier, update);
77 protected void add(InstanceIdentifier<Interface> identifier, final Interface intrf) {
78 if (intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
79 if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
80 final String interfaceName = intrf.getName();
82 ElanClusterUtils.runOnlyInLeaderNode(new Runnable() {
85 logger.debug("running external tunnel update job for interface {} added", interfaceName);
86 handleExternalTunnelUpdate(interfaceName, intrf);
93 private void handleExternalTunnelUpdate(String interfaceName, Interface update) {
94 ExternalTunnel externalTunnel = ElanUtils.getExternalTunnel(interfaceName, LogicalDatastoreType.CONFIGURATION);
95 if (externalTunnel != null) {
96 logger.debug("handling external tunnel update event for ext device dst {} src {} ",
97 externalTunnel.getDestinationDevice(), externalTunnel.getSourceDevice());
98 elanInterfaceManager.handleExternalTunnelStateEvent(externalTunnel, update);
100 logger.trace("External tunnel not found with interfaceName: {}", interfaceName);