added implementation of Identifier and Identifiable from yangtools.concepts
[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
41     // attributes
42     @Override
43     public void setThreadCount(int threadCount) {
44         this.threadCount = threadCount;
45     }
46
47     @Override
48     public int getThreadCount() {
49         return threadCount;
50     }
51
52     @Override
53     public boolean isTriggerNewInstanceCreation() {
54         return triggerNewInstanceCreation;
55     }
56
57     @Override
58     public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
59         this.triggerNewInstanceCreation = triggerNewInstanceCreation;
60     }
61
62     // operations
63
64     private boolean isReusable() {
65         return oldInstance != null;
66     }
67
68     @Override
69     public void validate() {
70         checkState(threadCount > 0,
71                 "Parameter 'threadCount' must be greater than 0");
72     }
73
74     @Override
75     public Closeable getInstance() {
76         if (instance == null) {
77             if (isReusable() && triggerNewInstanceCreation == false) { // simulate
78                                                                        // big
79                                                                        // change
80                                                                        // using
81                                                                        // triggerNewInstanceCreation
82                 oldInstance.setMaximumNumberOfThreads(threadCount);
83                 instance = oldInstance;
84             } else {
85                 if (oldCloseable != null) {
86                     try {
87                         oldCloseable.close();
88                     } catch (Exception e) {
89                         throw new RuntimeException(e);
90                     }
91                 }
92                 instance = new TestingFixedThreadPool(threadCount,
93                         name.toString());
94             }
95         }
96         return instance;
97     }
98
99     @Override
100     public ModuleIdentifier getIdentifier() {
101         return name;
102     }
103
104 }