Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / parallelapsp / TestingParallelAPSPModule.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.parallelapsp;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.base.Strings;
14 import java.io.Closeable;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import javax.management.AttributeNotFoundException;
18 import javax.management.InstanceNotFoundException;
19 import javax.management.MBeanException;
20 import javax.management.ObjectName;
21 import javax.management.ReflectionException;
22 import org.opendaylight.controller.config.api.DependencyResolver;
23 import org.opendaylight.controller.config.api.JmxAttribute;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.api.annotations.RequireInterface;
26 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
27 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolConfigMXBean;
28 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
29 import org.opendaylight.controller.config.spi.Module;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Represents service that has dependency to thread pool.
35  */
36 @NotThreadSafe
37 public class TestingParallelAPSPModule implements Module, TestingParallelAPSPConfigMXBean {
38     private static final Logger LOG = LoggerFactory.getLogger(TestingParallelAPSPModule.class);
39
40     private final DependencyResolver dependencyResolver;
41     private final AutoCloseable oldCloseable;
42     private final TestingParallelAPSPImpl oldInstance;
43     private final ModuleIdentifier identifier;
44     private ObjectName threadPoolON;
45     private TestingParallelAPSPImpl instance;
46     private String someParam;
47
48     public TestingParallelAPSPModule(final ModuleIdentifier identifier, final DependencyResolver dependencyResolver,
49             @Nullable final AutoCloseable oldCloseable, @Nullable final TestingParallelAPSPImpl oldInstance) {
50         this.identifier = identifier;
51         this.dependencyResolver = dependencyResolver;
52         this.oldCloseable = oldCloseable;
53         this.oldInstance = oldInstance;
54     }
55
56     @Override
57     public ObjectName getThreadPool() {
58         return threadPoolON;
59     }
60
61     @RequireInterface(TestingThreadPoolServiceInterface.class)
62     @Override
63     public void setThreadPool(final ObjectName threadPoolName) {
64         this.threadPoolON = threadPoolName;
65     }
66
67     @Override
68     public String getSomeParam() {
69         return someParam;
70     }
71
72     @Override
73     public void setSomeParam(final String someParam) {
74         this.someParam = someParam;
75     }
76
77     @Override
78     public Integer getMaxNumberOfThreads() {
79         if (instance == null) {
80             return null;
81         }
82         return instance.getMaxNumberOfThreads();
83     }
84
85     // this would be generated:
86     private final JmxAttribute threadPoolOnJMXAttribute = new JmxAttribute("threadPoolON");
87
88     @Override
89     public void validate() {
90         checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
91         dependencyResolver.validateDependency(TestingThreadPoolServiceInterface.class, threadPoolON,
92                 threadPoolOnJMXAttribute);
93
94         checkState(Strings.isNullOrEmpty(someParam) == false, "Parameter 'SomeParam' is blank");
95         // check that calling resolveInstance fails
96         try {
97             dependencyResolver.resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolOnJMXAttribute);
98             throw new RuntimeException("fail");
99         } catch (final IllegalStateException e) {
100             checkState("Commit was not triggered".equals(e.getMessage()), e.getMessage());
101         }
102
103         // test retrieving dependent module's attribute
104         int threadCount;
105         try {
106             threadCount = (Integer) dependencyResolver.getAttribute(threadPoolON, "ThreadCount");
107         } catch (final ReflectionException | InstanceNotFoundException | AttributeNotFoundException
108                 | MBeanException e) {
109             throw new IllegalStateException(e);
110         }
111         checkState(threadCount > 0);
112         TestingThreadPoolConfigMXBean proxy = dependencyResolver.newMXBeanProxy(threadPoolON,
113                 TestingThreadPoolConfigMXBean.class);
114         checkState(threadCount == proxy.getThreadCount());
115     }
116
117     @Override
118     @SuppressWarnings("IllegalCatch")
119     public Closeable getInstance() {
120         if (instance == null) {
121             TestingThreadPoolIfc threadPoolInstance = dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
122                     threadPoolON, threadPoolOnJMXAttribute);
123
124             if (oldInstance != null) {
125                 // changing thread pool is not supported
126                 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
127                 if (reuse) {
128                     LOG.debug("Reusing old instance");
129                     instance = oldInstance;
130                     instance.setSomeParam(someParam);
131                 }
132             }
133             if (instance == null) {
134                 LOG.debug("Creating new instance");
135                 if (oldCloseable != null) {
136                     try {
137                         oldCloseable.close();
138                     } catch (final Exception e) {
139                         throw new RuntimeException(e);
140                     }
141                 }
142                 instance = new TestingParallelAPSPImpl(threadPoolInstance, someParam);
143             }
144         }
145         return instance;
146     }
147
148     @Override
149     public boolean canReuse(final Module oldModule) {
150         return false;
151     }
152
153     @Override
154     public ModuleIdentifier getIdentifier() {
155         return identifier;
156     }
157 }