Bug 1022 - Add ability to lookup dependent Module's attribute.
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / parallelapsp / TestingParallelAPSPModule.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.testingservices.parallelapsp;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12
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;
28
29 /**
30  * Represents service that has dependency to thread pool.
31  */
32 @NotThreadSafe
33 public class TestingParallelAPSPModule implements Module,
34         TestingParallelAPSPConfigMXBean {
35     private static final Logger logger = LoggerFactory
36             .getLogger(TestingParallelAPSPModule.class);
37
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;
45
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;
54     }
55
56     @Override
57     public ObjectName getThreadPool() {
58         return threadPoolON;
59     }
60
61     @RequireInterface(TestingThreadPoolServiceInterface.class)
62     @Override
63     public void setThreadPool(ObjectName threadPoolName) {
64         this.threadPoolON = threadPoolName;
65     }
66
67     @Override
68     public String getSomeParam() {
69         return someParam;
70     }
71
72     @Override
73     public void setSomeParam(String someParam) {
74         this.someParam = someParam;
75     }
76
77     @Override
78     public Integer getMaxNumberOfThreads() {
79         if (instance == null)
80             return null;
81         return instance.getMaxNumberOfThreads();
82     }
83
84     // this would be generated:
85     private final JmxAttribute threadPoolONJMXAttribute = new JmxAttribute("threadPoolON");
86
87     @Override
88     public void validate() {
89         checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
90         dependencyResolver.validateDependency(
91                 TestingThreadPoolServiceInterface.class, threadPoolON,
92                 threadPoolONJMXAttribute);
93
94         checkState(Strings.isNullOrEmpty(someParam) == false,
95                 "Parameter 'SomeParam' is blank");
96         // check that calling resolveInstance fails
97         try {
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()),
103                     e.getMessage());
104         }
105
106         // test retrieving dependent module's attribute
107         int threadCount;
108         try {
109             threadCount = (Integer)dependencyResolver.getAttribute(threadPoolON, "ThreadCount");
110         } catch (Exception e) {
111             throw new IllegalStateException(e);
112         }
113         checkState(threadCount > 0);
114         TestingThreadPoolConfigMXBean proxy = dependencyResolver.newMXBeanProxy(threadPoolON, TestingThreadPoolConfigMXBean.class);
115         checkState(threadCount == proxy.getThreadCount());
116     }
117
118     @Override
119     public Closeable getInstance() {
120         if (instance == null) {
121             TestingThreadPoolIfc threadPoolInstance = dependencyResolver
122                     .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
123
124             if (oldInstance != null) {
125                 // changing thread pool is not supported
126                 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
127                 if (reuse) {
128                     logger.debug("Reusing old instance");
129                     instance = oldInstance;
130                     instance.setSomeParam(someParam);
131                 }
132             }
133             if (instance == null) {
134                 logger.debug("Creating new instance");
135                 if (oldCloseable != null) {
136                     try {
137                         oldCloseable.close();
138                     } catch (Exception e) {
139                         throw new RuntimeException(e);
140                     }
141                 }
142                 instance = new TestingParallelAPSPImpl(threadPoolInstance,
143                         someParam);
144             }
145         }
146         return instance;
147     }
148
149     @Override
150     public ModuleIdentifier getIdentifier() {
151         return identifier;
152     }
153
154
155 }