Merge "BUG-650: Split out CommitCoordinationTask"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / TestingFixedThreadPoolModule.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.threadpool;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.io.Closeable;
13 import javax.annotation.Nullable;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.config.api.ModuleIdentifier;
16 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
17 import org.opendaylight.controller.config.spi.Module;
18
19 @NotThreadSafe
20 public class TestingFixedThreadPoolModule implements
21         TestingFixedThreadPoolConfigMXBean, Module,
22         TestingThreadPoolConfigMXBean, ModifiableThreadPoolServiceInterface {
23     private final AutoCloseable oldCloseable;
24     private final TestingFixedThreadPool oldInstance;
25     private final ModuleIdentifier name;
26     private TestingFixedThreadPool instance;
27     private int threadCount = 0;
28     private boolean triggerNewInstanceCreation;
29
30     TestingFixedThreadPoolModule(ModuleIdentifier name,
31             @Nullable AutoCloseable oldCloseable,
32             @Nullable TestingFixedThreadPool oldInstance) {
33         this.name = name;
34         this.oldCloseable = oldCloseable;
35         this.oldInstance = oldInstance;
36     }
37
38
39     // attributes
40     @Override
41     public void setThreadCount(int threadCount) {
42         this.threadCount = threadCount;
43     }
44
45     @Override
46     public int getThreadCount() {
47         return threadCount;
48     }
49
50     @Override
51     public boolean isTriggerNewInstanceCreation() {
52         return triggerNewInstanceCreation;
53     }
54
55     @Override
56     public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
57         this.triggerNewInstanceCreation = triggerNewInstanceCreation;
58     }
59
60     // operations
61
62     private boolean isReusable() {
63         return oldInstance != null;
64     }
65
66     @Override
67     public void validate() {
68         checkState(threadCount > 0,
69                 "Parameter 'threadCount' must be greater than 0");
70     }
71
72     @Override
73     public Closeable getInstance() {
74         if (instance == null) {
75             if (isReusable() && triggerNewInstanceCreation == false) { // simulate
76                                                                        // big
77                                                                        // change
78                                                                        // using
79                                                                        // triggerNewInstanceCreation
80                 oldInstance.setMaximumNumberOfThreads(threadCount);
81                 instance = oldInstance;
82             } else {
83                 if (oldCloseable != null) {
84                     try {
85                         oldCloseable.close();
86                     } catch (Exception e) {
87                         throw new RuntimeException(e);
88                     }
89                 }
90                 instance = new TestingFixedThreadPool(threadCount,
91                         name.toString());
92             }
93         }
94         return instance;
95     }
96
97     @Override
98     public ModuleIdentifier getIdentifier() {
99         return name;
100     }
101
102 }