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