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