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 java.io.Closeable;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import javax.management.ObjectName;
19 import org.opendaylight.controller.config.api.DependencyResolver;
20 import org.opendaylight.controller.config.api.JmxAttribute;
21 import org.opendaylight.controller.config.api.ModuleIdentifier;
22 import org.opendaylight.controller.config.api.annotations.RequireInterface;
23 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
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;
29 import com.google.common.base.Strings;
32 * Represents service that has dependency to thread pool.
35 public class TestingParallelAPSPModule implements Module,
36 TestingParallelAPSPConfigMXBean {
37 private static final Logger logger = LoggerFactory
38 .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(ModuleIdentifier identifier,
49 DependencyResolver dependencyResolver,
50 @Nullable AutoCloseable oldCloseable,
51 @Nullable TestingParallelAPSPImpl oldInstance) {
52 this.identifier = identifier;
53 this.dependencyResolver = dependencyResolver;
54 this.oldCloseable = oldCloseable;
55 this.oldInstance = oldInstance;
59 public ObjectName getThreadPool() {
63 @RequireInterface(TestingThreadPoolServiceInterface.class)
65 public void setThreadPool(ObjectName threadPoolName) {
66 this.threadPoolON = threadPoolName;
70 public String getSomeParam() {
75 public void setSomeParam(String someParam) {
76 this.someParam = someParam;
80 public Integer getMaxNumberOfThreads() {
83 return instance.getMaxNumberOfThreads();
86 // this would be generated:
87 private final JmxAttribute threadPoolONJMXAttribute = new JmxAttribute("threadPoolON");
90 public void validate() {
91 checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
92 dependencyResolver.validateDependency(
93 TestingThreadPoolServiceInterface.class, threadPoolON,
94 threadPoolONJMXAttribute);
96 checkState(Strings.isNullOrEmpty(someParam) == false,
97 "Parameter 'SomeParam' is blank");
98 // check that calling resolveInstance fails
100 dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
101 threadPoolON, threadPoolONJMXAttribute);
102 throw new RuntimeException("fail");
103 } catch (IllegalStateException e) {
104 checkState("Commit was not triggered".equals(e.getMessage()),
110 public Closeable getInstance() {
111 if (instance == null) {
112 TestingThreadPoolIfc threadPoolInstance = dependencyResolver
113 .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
115 if (oldInstance != null) {
116 // changing thread pool is not supported
117 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
119 logger.debug("Reusing old instance");
120 instance = oldInstance;
121 instance.setSomeParam(someParam);
124 if (instance == null) {
125 logger.debug("Creating new instance");
126 if (oldCloseable != null) {
128 oldCloseable.close();
129 } catch (Exception e) {
130 throw new RuntimeException(e);
133 instance = new TestingParallelAPSPImpl(threadPoolInstance,
141 public ModuleIdentifier getIdentifier() {