BUG-2283 Fix close order when reconfiguring config modules.
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolModule.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.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,
27         TestingScheduledThreadPoolConfigBeanMXBean,
28         RuntimeBeanRegistratorAwareModule,
29         TestingScheduledThreadPoolServiceInterface {
30
31     private final ModuleIdentifier identifier;
32     @Nullable
33     private final AutoCloseable oldCloseable;
34     @Nullable
35     private final TestingScheduledThreadPoolImpl oldInstance;
36
37     private int threadCount = 10;
38     private TestingScheduledThreadPoolImpl instance;
39     private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
40     private boolean recreate;
41
42     public TestingScheduledThreadPoolModule(ModuleIdentifier identifier,
43             @Nullable AutoCloseable oldCloseable,
44             @Nullable TestingScheduledThreadPoolImpl oldInstance) {
45         this.identifier = identifier;
46         this.oldCloseable = oldCloseable;
47         this.oldInstance = oldInstance;
48     }
49
50     @Override
51     public void setRuntimeBeanRegistrator(
52             RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
53         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
54     }
55
56     @Override
57     public void validate() {
58         assertNull(runtimeBeanRegistrator);
59         // check thread count
60         checkState(threadCount > 0,
61                 "Parameter 'ThreadCount' must be greater than 0");
62     }
63
64     @Override
65     public boolean canReuse(final Module oldModule) {
66         return false;
67     }
68
69     @Override
70     public int getThreadCount() {
71         return threadCount;
72     }
73
74     @Override
75     public void setThreadCount(int threadCount) {
76         this.threadCount = threadCount;
77     }
78
79     @Override
80     public Closeable getInstance() {
81         assertNotNull(runtimeBeanRegistrator);
82         if (instance == null) {
83             if (oldInstance != null && recreate == false) {
84                 // reuse old instance
85                 instance = oldInstance;
86             }
87             if (instance == null) {
88                 if (oldCloseable != null) {
89                     try {
90                         oldCloseable.close();
91                     } catch (Exception e) {
92                         throw new RuntimeException(e);
93                     }
94                 }
95                 // close old threadpool and esp. unregister runtime beans
96                 instance = new TestingScheduledThreadPoolImpl(
97                         runtimeBeanRegistrator, threadCount);
98             }
99         }
100         return instance;
101     }
102
103     // getters and setters
104     @Override
105     public boolean isRecreate() {
106         return recreate;
107     }
108
109     @Override
110     public void setRecreate(boolean recreate) {
111         this.recreate = recreate;
112     }
113
114     @Override
115     public ModuleIdentifier getIdentifier() {
116         return identifier;
117     }
118
119
120 }