Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolModule.java
diff --git a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/testingservices/scheduledthreadpool/TestingScheduledThreadPoolModule.java b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/testingservices/scheduledthreadpool/TestingScheduledThreadPoolModule.java
new file mode 100644 (file)
index 0000000..4716d58
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * 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.scheduledthreadpool;
+
+import static com.google.common.base.Preconditions.checkState;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.Closeable;
+
+import javax.annotation.Nullable;
+
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
+import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
+import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
+import org.opendaylight.controller.config.spi.Module;
+
+/**
+ * This class has two exported interfaces and two runtime beans. Recreation is
+ * triggered by setting Recreate attribute to true.
+ */
+public class TestingScheduledThreadPoolModule implements Module,
+        TestingScheduledThreadPoolConfigBeanMXBean,
+        RuntimeBeanRegistratorAwareModule,
+        TestingScheduledThreadPoolServiceInterface {
+
+    private final ModuleIdentifier name;
+    @Nullable
+    private final AutoCloseable oldCloseable;
+    @Nullable
+    private final TestingScheduledThreadPoolImpl oldInstance;
+
+    private final int threadCount = 10;
+    private TestingScheduledThreadPoolImpl instance;
+    private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
+    private boolean recreate;
+
+    public TestingScheduledThreadPoolModule(ModuleIdentifier name,
+            @Nullable AutoCloseable oldCloseable,
+            @Nullable TestingScheduledThreadPoolImpl oldInstance) {
+        this.name = name;
+        this.oldCloseable = oldCloseable;
+        this.oldInstance = oldInstance;
+    }
+
+    @Override
+    public ModuleIdentifier getName() {
+        return name;
+    }
+
+    @Override
+    public void setRuntimeBeanRegistrator(
+            RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
+        this.runtimeBeanRegistrator = runtimeBeanRegistrator;
+    }
+
+    @Override
+    public void validate() {
+        assertNull(runtimeBeanRegistrator);
+        // check thread count
+        checkState(threadCount > 0,
+                "Parameter 'ThreadCount' must be greater than 0");
+    }
+
+    @Override
+    public int getThreadCount() {
+        return threadCount;
+    }
+
+    @Override
+    public Closeable getInstance() {
+        assertNotNull(runtimeBeanRegistrator);
+        if (instance == null) {
+            if (oldInstance != null && recreate == false) {
+                // reuse old instance
+                instance = oldInstance;
+            }
+            if (instance == null) {
+                if (oldCloseable != null) {
+                    try {
+                        oldCloseable.close();
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+                // close old threadpool and esp. unregister runtime beans
+                instance = new TestingScheduledThreadPoolImpl(
+                        runtimeBeanRegistrator, threadCount);
+            }
+        }
+        return instance;
+    }
+
+    // getters and setters
+    @Override
+    public boolean isRecreate() {
+        return recreate;
+    }
+
+    @Override
+    public void setRecreate(boolean recreate) {
+        this.recreate = recreate;
+    }
+
+}