Bump upstreams
[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 ListeningExecutorService listeningExecutorService;
34     private final DataBroker dataBroker;
35
36     @Inject
37     public ListenerRegistrationHelper(final DataBroker dataBroker) {
38         this.dataBroker = dataBroker;
39         listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(
40             new ThreadFactoryBuilder()
41                 .setNameFormat("frm-listener" + "%d")
42                 .setDaemon(false)
43                 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
44                 .build()));
45     }
46
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")) {
52                 try {
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();
58                 }
59             }
60             return dataBroker.registerDataTreeChangeListener(treeId, listener);
61         });
62     }
63
64     public void close() throws Exception {
65         MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.SECONDS);
66     }
67 }