2 * Copyright (c) 2013 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.ObjectName;
18 import org.opendaylight.controller.config.api.DependencyResolver;
19 import org.opendaylight.controller.config.api.JmxAttribute;
20 import org.opendaylight.controller.config.api.ModuleIdentifier;
21 import org.opendaylight.controller.config.api.annotations.RequireInterface;
22 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
23 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolConfigMXBean;
24 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
25 import org.opendaylight.controller.config.spi.Module;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Represents service that has dependency to thread pool.
33 public class TestingParallelAPSPModule implements Module,
34 TestingParallelAPSPConfigMXBean {
35 private static final Logger LOG = LoggerFactory
36 .getLogger(TestingParallelAPSPModule.class);
38 private final DependencyResolver dependencyResolver;
39 private final AutoCloseable oldCloseable;
40 private final TestingParallelAPSPImpl oldInstance;
41 private final ModuleIdentifier identifier;
42 private ObjectName threadPoolON;
43 private TestingParallelAPSPImpl instance;
44 private String someParam;
46 public TestingParallelAPSPModule(ModuleIdentifier identifier,
47 DependencyResolver dependencyResolver,
48 @Nullable AutoCloseable oldCloseable,
49 @Nullable 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(ObjectName threadPoolName) {
64 this.threadPoolON = threadPoolName;
68 public String getSomeParam() {
73 public void setSomeParam(String someParam) {
74 this.someParam = someParam;
78 public Integer getMaxNumberOfThreads() {
81 return instance.getMaxNumberOfThreads();
84 // this would be generated:
85 private final JmxAttribute threadPoolONJMXAttribute = new JmxAttribute("threadPoolON");
88 public void validate() {
89 checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
90 dependencyResolver.validateDependency(
91 TestingThreadPoolServiceInterface.class, threadPoolON,
92 threadPoolONJMXAttribute);
94 checkState(Strings.isNullOrEmpty(someParam) == false,
95 "Parameter 'SomeParam' is blank");
96 // check that calling resolveInstance fails
98 dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
99 threadPoolON, threadPoolONJMXAttribute);
100 throw new RuntimeException("fail");
101 } catch (IllegalStateException e) {
102 checkState("Commit was not triggered".equals(e.getMessage()),
106 // test retrieving dependent module's attribute
109 threadCount = (Integer)dependencyResolver.getAttribute(threadPoolON, "ThreadCount");
110 } catch (Exception e) {
111 throw new IllegalStateException(e);
113 checkState(threadCount > 0);
114 TestingThreadPoolConfigMXBean proxy = dependencyResolver.newMXBeanProxy(threadPoolON, TestingThreadPoolConfigMXBean.class);
115 checkState(threadCount == proxy.getThreadCount());
119 public Closeable getInstance() {
120 if (instance == null) {
121 TestingThreadPoolIfc threadPoolInstance = dependencyResolver
122 .resolveInstance(TestingThreadPoolIfc.class, 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 (Exception e) {
139 throw new RuntimeException(e);
142 instance = new TestingParallelAPSPImpl(threadPoolInstance,
150 public ModuleIdentifier getIdentifier() {