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 String operational = "OPERATIONAL";
34 private final ListeningExecutorService listeningExecutorService;
35 private final DataBroker dataBroker;
38 public ListenerRegistrationHelper(final DataBroker dataBroker) {
39 this.dataBroker = dataBroker;
40 listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(
41 new ThreadFactoryBuilder()
42 .setNameFormat("frm-listener" + "%d")
44 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
48 public <T extends DataObject, L extends ClusteredDataTreeChangeListener<T>>
49 ListenableFuture<ListenerRegistration<L>> checkedRegisterListener(
50 DataTreeIdentifier<T> treeId, L listener) {
51 return listeningExecutorService.submit(() -> {
52 while (!getInventoryConfigDataStoreStatus().equals(operational)) {
54 LOG.debug("Retrying for datastore to become operational for listener {}", listener);
55 Thread.sleep(INVENTORY_CHECK_TIMER * 1000);
56 } catch (InterruptedException e) {
57 LOG.info("registerDataTreeChangeListener thread is interrupted");
58 Thread.currentThread().interrupt();
61 return dataBroker.registerDataTreeChangeListener(treeId, listener);
65 public void close() throws Exception {
66 MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.SECONDS);