Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolModule.java
1 /*
2  * Copyright (c) 2013, 2017 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.scheduledthreadpool;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.io.Closeable;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
18 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.spi.Module;
21
22 /**
23  * This class has two exported interfaces and two runtime beans. Recreation is
24  * triggered by setting Recreate attribute to true.
25  */
26 public class TestingScheduledThreadPoolModule implements Module, TestingScheduledThreadPoolConfigBeanMXBean,
27         RuntimeBeanRegistratorAwareModule, TestingScheduledThreadPoolServiceInterface {
28
29     private final ModuleIdentifier identifier;
30     @Nullable
31     private final AutoCloseable oldCloseable;
32     @Nullable
33     private final TestingScheduledThreadPoolImpl oldInstance;
34
35     private int threadCount = 10;
36     private TestingScheduledThreadPoolImpl instance;
37     private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
38     private boolean recreate;
39
40     public TestingScheduledThreadPoolModule(final ModuleIdentifier identifier,
41             @Nullable final AutoCloseable oldCloseable, @Nullable final TestingScheduledThreadPoolImpl oldInstance) {
42         this.identifier = identifier;
43         this.oldCloseable = oldCloseable;
44         this.oldInstance = oldInstance;
45     }
46
47     @Override
48     public void setRuntimeBeanRegistrator(final RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
49         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
50     }
51
52     @Override
53     public void validate() {
54         assertNull(runtimeBeanRegistrator);
55         // check thread count
56         checkState(threadCount > 0, "Parameter 'ThreadCount' must be greater than 0");
57     }
58
59     @Override
60     public boolean canReuse(final Module oldModule) {
61         return getClass().isInstance(oldModule)
62                 && getThreadCount() == ((TestingScheduledThreadPoolModule) oldModule).getThreadCount();
63     }
64
65     @Override
66     public int getThreadCount() {
67         return threadCount;
68     }
69
70     @Override
71     public void setThreadCount(final int threadCount) {
72         this.threadCount = threadCount;
73     }
74
75     @Override
76     @SuppressWarnings("IllegalCatch")
77     public Closeable getInstance() {
78         assertNotNull(runtimeBeanRegistrator);
79         if (instance == null) {
80             if (oldInstance != null && recreate == false) {
81                 // reuse old instance
82                 instance = oldInstance;
83             }
84             if (instance == null) {
85                 if (oldCloseable != null) {
86                     try {
87                         oldCloseable.close();
88                     } catch (final Exception e) {
89                         throw new RuntimeException(e);
90                     }
91                 }
92                 // close old threadpool and esp. unregister runtime beans
93                 instance = new TestingScheduledThreadPoolImpl(runtimeBeanRegistrator, threadCount);
94             }
95         }
96         return instance;
97     }
98
99     // getters and setters
100     @Override
101     public boolean isRecreate() {
102         return recreate;
103     }
104
105     @Override
106     public void setRecreate(final boolean recreate) {
107         this.recreate = recreate;
108     }
109
110     @Override
111     public ModuleIdentifier getIdentifier() {
112         return identifier;
113     }
114 }