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
diff --git a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/testingservices/threadpool/TestingFixedThreadPoolModule.java b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/testingservices/threadpool/TestingFixedThreadPoolModule.java
new file mode 100644 (file)
index 0000000..ff33b16
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.manager.testingservices.threadpool;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.io.Closeable;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.NotThreadSafe;
+
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
+import org.opendaylight.controller.config.spi.Module;
+
+@NotThreadSafe
+public class TestingFixedThreadPoolModule implements
+        TestingFixedThreadPoolConfigMXBean, Module,
+        TestingThreadPoolConfigMXBean, ModifiableThreadPoolServiceInterface {
+    private final AutoCloseable oldCloseable;
+    private final TestingFixedThreadPool oldInstance;
+    private final ModuleIdentifier name;
+    private TestingFixedThreadPool instance;
+    private int threadCount = 0;
+    private boolean triggerNewInstanceCreation;
+
+    TestingFixedThreadPoolModule(ModuleIdentifier name,
+            @Nullable AutoCloseable oldCloseable,
+            @Nullable TestingFixedThreadPool oldInstance) {
+        this.name = name;
+        this.oldCloseable = oldCloseable;
+        this.oldInstance = oldInstance;
+    }
+
+    @Override
+    public ModuleIdentifier getName() {
+        return name;
+    }
+
+    // attributes
+    @Override
+    public void setThreadCount(int threadCount) {
+        this.threadCount = threadCount;
+    }
+
+    @Override
+    public int getThreadCount() {
+        return threadCount;
+    }
+
+    @Override
+    public boolean isTriggerNewInstanceCreation() {
+        return triggerNewInstanceCreation;
+    }
+
+    @Override
+    public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
+        this.triggerNewInstanceCreation = triggerNewInstanceCreation;
+    }
+
+    // operations
+
+    private boolean isReusable() {
+        return oldInstance != null;
+    }
+
+    @Override
+    public void validate() {
+        checkState(threadCount > 0,
+                "Parameter 'threadCount' must be greater than 0");
+    }
+
+    @Override
+    public Closeable getInstance() {
+        if (instance == null) {
+            if (isReusable() && triggerNewInstanceCreation == false) { // simulate
+                                                                       // big
+                                                                       // change
+                                                                       // using
+                                                                       // triggerNewInstanceCreation
+                oldInstance.setMaximumNumberOfThreads(threadCount);
+                instance = oldInstance;
+            } else {
+                if (oldCloseable != null) {
+                    try {
+                        oldCloseable.close();
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+                instance = new TestingFixedThreadPool(threadCount,
+                        name.toString());
+            }
+        }
+        return instance;
+    }
+
+}