Convert interfacemanager to datastore-constrained
[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 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
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.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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * This class listens for interface creation/removal/update in Configuration DS.
33  * This is used to handle interfaces for base of-ports.
34  */
35 @Singleton
36 public class AlivenessMonitorListenerImpl implements AlivenessMonitorListener {
37     private static final Logger LOG = LoggerFactory.getLogger(AlivenessMonitorListenerImpl.class);
38
39     private final ManagedNewTransactionRunner txRunner;
40
41     @Inject
42     public AlivenessMonitorListenerImpl(final DataBroker dataBroker, final NotificationService notificationService) {
43         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
44         notificationService.registerNotificationListener(this);
45     }
46
47     @PostConstruct
48     public void start() {
49         LOG.info("AlivenessMonitorListener started");
50     }
51
52     @PreDestroy
53     public void close() {
54         LOG.info("AlivenessMonitorListener closed");
55     }
56
57     @Override
58     public void onMonitorEvent(MonitorEvent notification) {
59         ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
60             Long monitorId = notification.getEventData().getMonitorId();
61             String tunnelInterface = AlivenessMonitorUtils.getInterfaceFromMonitorId(tx, monitorId);
62             if (tunnelInterface == null) {
63                 LOG.debug("Either monitoring for interface not started by Interfacemgr or it is not LLDP monitoring");
64                 return;
65             }
66             LivenessState livenessState = notification.getEventData().getMonitorState();
67             LOG.debug("received monitor event for {} with livenessstate {}", tunnelInterface, livenessState);
68             OperStatus opState = livenessState == LivenessState.Up ? OperStatus.Up : OperStatus.Down;
69             InterfaceManagerCommonUtils.setOpStateForInterface(tx, tunnelInterface, opState);
70         }), LOG, "Error processing monitor event {}", notification);
71     }
72 }