config-manager: final parameters
[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(final ModuleIdentifier name,
31             @Nullable final AutoCloseable oldCloseable,
32             @Nullable final 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(final 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(final 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 boolean canReuse(final Module oldModule) {
74         return isReusable() && triggerNewInstanceCreation == false;
75     }
76
77     @Override
78     public Closeable getInstance() {
79         if (instance == null) {
80             if (isReusable() && triggerNewInstanceCreation == false) { // simulate
81                                                                        // big
82                                                                        // change
83                                                                        // using
84                                                                        // triggerNewInstanceCreation
85                 oldInstance.setMaximumNumberOfThreads(threadCount);
86                 instance = oldInstance;
87             } else {
88                 if (oldCloseable != null) {
89                     try {
90                         oldCloseable.close();
91                     } catch (final Exception e) {
92                         throw new RuntimeException(e);
93                     }
94                 }
95                 instance = new TestingFixedThreadPool(threadCount,
96                         name.toString());
97             }
98         }
99         return instance;
100     }
101
102     @Override
103     public ModuleIdentifier getIdentifier() {
104         return name;
105     }
106
107 }