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.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.TestingThreadPoolIfc;
24 import org.opendaylight.controller.config.spi.Module;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Strings;
31 * Represents service that has dependency to thread pool.
34 public class TestingParallelAPSPModule implements Module,
35 TestingParallelAPSPConfigMXBean {
36 private static final Logger logger = LoggerFactory
37 .getLogger(TestingParallelAPSPModule.class);
39 private final DependencyResolver dependencyResolver;
40 private final AutoCloseable oldCloseable;
41 private final TestingParallelAPSPImpl oldInstance;
42 private final ModuleIdentifier name;
43 private ObjectName threadPoolON;
44 private TestingParallelAPSPImpl instance;
45 private String someParam;
47 public TestingParallelAPSPModule(ModuleIdentifier name,
48 DependencyResolver dependencyResolver,
49 @Nullable AutoCloseable oldCloseable,
50 @Nullable TestingParallelAPSPImpl oldInstance) {
52 this.dependencyResolver = dependencyResolver;
53 this.oldCloseable = oldCloseable;
54 this.oldInstance = oldInstance;
58 public ModuleIdentifier getName() {
63 public ObjectName getThreadPool() {
67 @RequireInterface(TestingThreadPoolServiceInterface.class)
69 public void setThreadPool(ObjectName threadPoolName) {
70 this.threadPoolON = threadPoolName;
74 public String getSomeParam() {
79 public void setSomeParam(String someParam) {
80 this.someParam = someParam;
84 public Integer getMaxNumberOfThreads() {
87 return instance.getMaxNumberOfThreads();
91 public void validate() {
92 checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
93 dependencyResolver.validateDependency(
94 TestingThreadPoolServiceInterface.class, threadPoolON,
97 checkState(Strings.isNullOrEmpty(someParam) == false,
98 "Parameter 'SomeParam' is blank");
99 // check that calling resolveInstance fails
101 dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
103 throw new RuntimeException("fail");
104 } catch (IllegalStateException e) {
105 checkState("Commit was not triggered".equals(e.getMessage()),
111 public Closeable getInstance() {
112 if (instance == null) {
113 TestingThreadPoolIfc threadPoolInstance = dependencyResolver
114 .resolveInstance(TestingThreadPoolIfc.class, threadPoolON);
116 if (oldInstance != null) {
117 // changing thread pool is not supported
118 boolean reuse = threadPoolInstance.equals(oldInstance
121 logger.debug("Reusing old instance");
122 instance = oldInstance;
123 instance.setSomeParam(someParam);
126 if (instance == null) {
127 logger.debug("Creating new instance");
128 if (oldCloseable != null) {
130 oldCloseable.close();
131 } catch (Exception e) {
132 throw new RuntimeException(e);
135 instance = new TestingParallelAPSPImpl(threadPoolInstance,