7e121bd94d47e60aada6b30606c0fdd22bdcc451
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / listeners / AlivenessMonitorListenerImpl.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
9 package org.opendaylight.genius.interfacemanager.listeners;
10
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12
13 import javax.annotation.PostConstruct;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17
18 import org.apache.aries.blueprint.annotation.service.Reference;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
21 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
22 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
23 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.NotificationService;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorEvent;
30 import org.opendaylight.yangtools.yang.common.Uint32;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class listens for interface creation/removal/update in Configuration DS.
37  * This is used to handle interfaces for base of-ports.
38  */
39 @Singleton
40 public class AlivenessMonitorListenerImpl implements AlivenessMonitorListener {
41     private static final Logger LOG = LoggerFactory.getLogger(AlivenessMonitorListenerImpl.class);
42
43     private final ManagedNewTransactionRunner txRunner;
44
45     @Inject
46     public AlivenessMonitorListenerImpl(@Reference final DataBroker dataBroker,
47                                         @Reference final NotificationService notificationService) {
48         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
49         notificationService.registerNotificationListener(this);
50     }
51
52     @PostConstruct
53     public void start() {
54         LOG.info("AlivenessMonitorListener started");
55     }
56
57     @PreDestroy
58     public void close() {
59         LOG.info("AlivenessMonitorListener closed");
60     }
61
62     @Override
63     public void onMonitorEvent(MonitorEvent notification) {
64         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
65             Uint32 monitorId = notification.getEventData().getMonitorId();
66             String tunnelInterface = AlivenessMonitorUtils.getInterfaceFromMonitorId(tx, monitorId);
67             if (tunnelInterface == null) {
68                 LOG.debug("Either monitoring for interface not started by Interfacemgr or it is not LLDP monitoring");
69                 return;
70             }
71             LivenessState livenessState = notification.getEventData().getMonitorState();
72             LOG.debug("received monitor event for {} with livenessstate {}", tunnelInterface, livenessState);
73             OperStatus opState = livenessState == LivenessState.Up ? OperStatus.Up : OperStatus.Down;
74             InterfaceManagerCommonUtils.setOpStateForInterface(tx, tunnelInterface, opState);
75         }), LOG, "Error processing monitor event {}", notification);
76     }
77 }