Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolImpl.java
1 /*
2  * Copyright (c) 2013, 2017 Cisco Systems, Inc. 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.controller.config.manager.testingservices.scheduledthreadpool;
9
10 import com.google.common.collect.Lists;
11 import java.io.Closeable;
12 import java.util.List;
13 import java.util.concurrent.Executor;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.ScheduledThreadPoolExecutor;
16 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
17 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
18 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.runtimebeans.TestingScheduledRuntimeBean;
19 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
20
21 public class TestingScheduledThreadPoolImpl implements TestingThreadPoolIfc, TestingScheduledThreadPoolIfc, Closeable {
22     private static volatile int numberOfCloseMethodCalls = 0;
23     private final ScheduledThreadPoolExecutor executor;
24     private final RootRuntimeBeanRegistrator runtimeBeanRegistrator;
25
26     public static final List<ScheduledThreadPoolExecutor> ALLEXECUTORS = Lists.newLinkedList();
27
28     public TestingScheduledThreadPoolImpl(final RootRuntimeBeanRegistrator runtimeBeanRegistrator,
29             final int corePoolSize) {
30         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
31         executor = new ScheduledThreadPoolExecutor(corePoolSize);
32         ALLEXECUTORS.add(executor);
33         HierarchicalRuntimeBeanRegistration hierarchicalRuntimeBeanRegistration = runtimeBeanRegistrator
34                 .registerRoot(new TestingScheduledRuntimeBean());
35         hierarchicalRuntimeBeanRegistration.register("a", "b", new TestingScheduledRuntimeBean());
36     }
37
38     @Override
39     public void close() {
40         numberOfCloseMethodCalls++;
41         runtimeBeanRegistrator.close();
42         executor.shutdown();
43     }
44
45     @Override
46     public ScheduledExecutorService getScheduledExecutor() {
47         return executor;
48     }
49
50     @Override
51     public Executor getExecutor() {
52         return executor;
53     }
54
55     @Override
56     public int getMaxNumberOfThreads() {
57         return executor.getCorePoolSize();
58     }
59
60     public static void cleanUp() {
61         for (ScheduledThreadPoolExecutor executor : ALLEXECUTORS) {
62             executor.shutdown();
63         }
64         ALLEXECUTORS.clear();
65         numberOfCloseMethodCalls = 0;
66     }
67
68     public static int getNumberOfCloseMethodCalls() {
69         return numberOfCloseMethodCalls;
70     }
71 }