Merge "Remove raw references to Map in XSQL"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolModule.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 static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.io.Closeable;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
18 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.spi.Module;
21
22 /**
23  * This class has two exported interfaces and two runtime beans. Recreation is
24  * triggered by setting Recreate attribute to true.
25  */
26 public class TestingScheduledThreadPoolModule implements Module,
27         TestingScheduledThreadPoolConfigBeanMXBean,
28         RuntimeBeanRegistratorAwareModule,
29         TestingScheduledThreadPoolServiceInterface {
30
31     private final ModuleIdentifier identifier;
32     @Nullable
33     private final AutoCloseable oldCloseable;
34     @Nullable
35     private final TestingScheduledThreadPoolImpl oldInstance;
36
37     private int threadCount = 10;
38     private TestingScheduledThreadPoolImpl instance;
39     private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
40     private boolean recreate;
41
42     public TestingScheduledThreadPoolModule(ModuleIdentifier identifier,
43             @Nullable AutoCloseable oldCloseable,
44             @Nullable TestingScheduledThreadPoolImpl oldInstance) {
45         this.identifier = identifier;
46         this.oldCloseable = oldCloseable;
47         this.oldInstance = oldInstance;
48     }
49
50     @Override
51     public void setRuntimeBeanRegistrator(
52             RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
53         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
54     }
55
56     @Override
57     public void validate() {
58         assertNull(runtimeBeanRegistrator);
59         // check thread count
60         checkState(threadCount > 0,
61                 "Parameter 'ThreadCount' must be greater than 0");
62     }
63
64     @Override
65     public int getThreadCount() {
66         return threadCount;
67     }
68
69     @Override
70     public void setThreadCount(int threadCount) {
71         this.threadCount = threadCount;
72     }
73
74     @Override
75     public Closeable getInstance() {
76         assertNotNull(runtimeBeanRegistrator);
77         if (instance == null) {
78             if (oldInstance != null && recreate == false) {
79                 // reuse old instance
80                 instance = oldInstance;
81             }
82             if (instance == null) {
83                 if (oldCloseable != null) {
84                     try {
85                         oldCloseable.close();
86                     } catch (Exception e) {
87                         throw new RuntimeException(e);
88                     }
89                 }
90                 // close old threadpool and esp. unregister runtime beans
91                 instance = new TestingScheduledThreadPoolImpl(
92                         runtimeBeanRegistrator, threadCount);
93             }
94         }
95         return instance;
96     }
97
98     // getters and setters
99     @Override
100     public boolean isRecreate() {
101         return recreate;
102     }
103
104     @Override
105     public void setRecreate(boolean recreate) {
106         this.recreate = recreate;
107     }
108
109     @Override
110     public ModuleIdentifier getIdentifier() {
111         return identifier;
112     }
113
114
115 }