2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.testingservices.threadpool;
10 import static com.google.common.base.Preconditions.checkState;
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;
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;
30 TestingFixedThreadPoolModule(final ModuleIdentifier name,
31 @Nullable final AutoCloseable oldCloseable,
32 @Nullable final TestingFixedThreadPool oldInstance) {
34 this.oldCloseable = oldCloseable;
35 this.oldInstance = oldInstance;
40 public void setThreadCount(final int threadCount) {
41 this.threadCount = threadCount;
45 public int getThreadCount() {
50 public boolean isTriggerNewInstanceCreation() {
51 return triggerNewInstanceCreation;
55 public void setTriggerNewInstanceCreation(final boolean triggerNewInstanceCreation) {
56 this.triggerNewInstanceCreation = triggerNewInstanceCreation;
61 private boolean isReusable() {
62 return oldInstance != null;
66 public void validate() {
67 checkState(threadCount > 0,
68 "Parameter 'threadCount' must be greater than 0");
72 public boolean canReuse(final Module oldModule) {
73 return isReusable() && triggerNewInstanceCreation == false;
77 @SuppressWarnings("IllegalCatch")
78 public Closeable getInstance() {
79 if (instance == null) {
80 if (isReusable() && triggerNewInstanceCreation == false) { // simulate
84 // triggerNewInstanceCreation
85 oldInstance.setMaximumNumberOfThreads(threadCount);
86 instance = oldInstance;
88 if (oldCloseable != null) {
91 } catch (final Exception e) {
92 throw new RuntimeException(e);
95 instance = new TestingFixedThreadPool(threadCount,
103 public ModuleIdentifier getIdentifier() {