Do not guard registration with retries
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ListenerRegistrationHelper.java
1 /*
2  * Copyright (c) 2020 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.openflowplugin.applications.frm.impl;
9
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getInventoryConfigDataStoreStatus;
11
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;
27
28 @Singleton
29 public class ListenerRegistrationHelper {
30     private static final Logger LOG = LoggerFactory.getLogger(ListenerRegistrationHelper.class);
31     private static final long INVENTORY_CHECK_TIMER = 1;
32
33     private final String operational = "OPERATIONAL";
34     private final ListeningExecutorService listeningExecutorService;
35     private final DataBroker dataBroker;
36
37     @Inject
38     public ListenerRegistrationHelper(final DataBroker dataBroker) {
39         this.dataBroker = dataBroker;
40         listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(
41             new ThreadFactoryBuilder()
42                 .setNameFormat("frm-listener" + "%d")
43                 .setDaemon(false)
44                 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
45                 .build()));
46     }
47
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)) {
53                 try {
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();
59                 }
60             }
61             return dataBroker.registerDataTreeChangeListener(treeId, listener);
62         });
63     }
64
65     public void close() throws Exception {
66         MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.SECONDS);
67     }
68 }