2 * Copyright (c) 2020 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.openflowplugin.applications.frm.impl;
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getInventoryConfigDataStoreStatus;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.ThreadFactoryBuilder;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.TimeUnit;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public class ListenerRegistrationHelper {
30 private static final Logger LOG = LoggerFactory.getLogger(ListenerRegistrationHelper.class);
31 private static final long INVENTORY_CHECK_TIMER = 1;
33 private final ListeningExecutorService listeningExecutorService;
34 private final DataBroker dataBroker;
37 public ListenerRegistrationHelper(final DataBroker dataBroker) {
38 this.dataBroker = dataBroker;
39 listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(
40 new ThreadFactoryBuilder()
41 .setNameFormat("frm-listener" + "%d")
43 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
47 public <T extends DataObject, L extends ClusteredDataTreeChangeListener<T>>
48 ListenableFuture<ListenerRegistration<L>> checkedRegisterListener(
49 final DataTreeIdentifier<T> treeId, final L listener) {
50 return listeningExecutorService.submit(() -> {
51 while (!getInventoryConfigDataStoreStatus().equals("OPERATIONAL")) {
53 LOG.debug("Retrying for datastore to become operational for listener {}", listener);
54 Thread.sleep(INVENTORY_CHECK_TIMER * 1000);
55 } catch (InterruptedException e) {
56 LOG.info("registerDataTreeChangeListener thread is interrupted");
57 Thread.currentThread().interrupt();
60 return dataBroker.registerDataTreeChangeListener(treeId, listener);
64 public void close() throws Exception {
65 MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.SECONDS);