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 com.google.common.base.Strings;
11 import org.opendaylight.controller.config.api.DependencyResolver;
12 import org.opendaylight.controller.config.api.JmxAttribute;
13 import org.opendaylight.controller.config.api.ModuleIdentifier;
14 import org.opendaylight.controller.config.api.annotations.RequireInterface;
15 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
16 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
17 import org.opendaylight.controller.config.spi.Module;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.NotThreadSafe;
23 import javax.management.ObjectName;
24 import java.io.Closeable;
26 import static com.google.common.base.Preconditions.checkNotNull;
27 import static com.google.common.base.Preconditions.checkState;
30 * Represents service that has dependency to thread pool.
33 public class TestingParallelAPSPModule implements Module,
34 TestingParallelAPSPConfigMXBean {
35 private static final Logger logger = 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()),
108 public Closeable getInstance() {
109 if (instance == null) {
110 TestingThreadPoolIfc threadPoolInstance = dependencyResolver
111 .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
113 if (oldInstance != null) {
114 // changing thread pool is not supported
115 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
117 logger.debug("Reusing old instance");
118 instance = oldInstance;
119 instance.setSomeParam(someParam);
122 if (instance == null) {
123 logger.debug("Creating new instance");
124 if (oldCloseable != null) {
126 oldCloseable.close();
127 } catch (Exception e) {
128 throw new RuntimeException(e);
131 instance = new TestingParallelAPSPImpl(threadPoolInstance,
139 public ModuleIdentifier getIdentifier() {