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