2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.testingservices.parallelapsp;
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
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;
34 * Represents service that has dependency to thread pool.
37 public class TestingParallelAPSPModule implements Module, TestingParallelAPSPConfigMXBean {
38 private static final Logger LOG = LoggerFactory.getLogger(TestingParallelAPSPModule.class);
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;
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;
57 public ObjectName getThreadPool() {
61 @RequireInterface(TestingThreadPoolServiceInterface.class)
63 public void setThreadPool(final ObjectName threadPoolName) {
64 this.threadPoolON = threadPoolName;
68 public String getSomeParam() {
73 public void setSomeParam(final String someParam) {
74 this.someParam = someParam;
78 public Integer getMaxNumberOfThreads() {
79 if (instance == null) {
82 return instance.getMaxNumberOfThreads();
85 // this would be generated:
86 private final JmxAttribute threadPoolOnJMXAttribute = new JmxAttribute("threadPoolON");
89 public void validate() {
90 checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
91 dependencyResolver.validateDependency(TestingThreadPoolServiceInterface.class, threadPoolON,
92 threadPoolOnJMXAttribute);
94 checkState(Strings.isNullOrEmpty(someParam) == false, "Parameter 'SomeParam' is blank");
95 // check that calling resolveInstance fails
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());
103 // test retrieving dependent module's attribute
106 threadCount = (Integer) dependencyResolver.getAttribute(threadPoolON, "ThreadCount");
107 } catch (final ReflectionException | InstanceNotFoundException | AttributeNotFoundException
108 | MBeanException e) {
109 throw new IllegalStateException(e);
111 checkState(threadCount > 0);
112 TestingThreadPoolConfigMXBean proxy = dependencyResolver.newMXBeanProxy(threadPoolON,
113 TestingThreadPoolConfigMXBean.class);
114 checkState(threadCount == proxy.getThreadCount());
118 @SuppressWarnings("IllegalCatch")
119 public Closeable getInstance() {
120 if (instance == null) {
121 TestingThreadPoolIfc threadPoolInstance = dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
122 threadPoolON, threadPoolOnJMXAttribute);
124 if (oldInstance != null) {
125 // changing thread pool is not supported
126 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
128 LOG.debug("Reusing old instance");
129 instance = oldInstance;
130 instance.setSomeParam(someParam);
133 if (instance == null) {
134 LOG.debug("Creating new instance");
135 if (oldCloseable != null) {
137 oldCloseable.close();
138 } catch (final Exception e) {
139 throw new RuntimeException(e);
142 instance = new TestingParallelAPSPImpl(threadPoolInstance, someParam);
149 public boolean canReuse(final Module oldModule) {
154 public ModuleIdentifier getIdentifier() {