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