3c55f4dcc05ffaead37a614904de3eed284de874
[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 java.io.Closeable;
14
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import javax.management.ObjectName;
18
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;
28
29 import com.google.common.base.Strings;
30
31 /**
32  * Represents service that has dependency to thread pool.
33  */
34 @NotThreadSafe
35 public class TestingParallelAPSPModule implements Module,
36         TestingParallelAPSPConfigMXBean {
37     private static final Logger logger = LoggerFactory
38             .getLogger(TestingParallelAPSPModule.class);
39
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;
47
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;
56     }
57
58     @Override
59     public ObjectName getThreadPool() {
60         return threadPoolON;
61     }
62
63     @RequireInterface(TestingThreadPoolServiceInterface.class)
64     @Override
65     public void setThreadPool(ObjectName threadPoolName) {
66         this.threadPoolON = threadPoolName;
67     }
68
69     @Override
70     public String getSomeParam() {
71         return someParam;
72     }
73
74     @Override
75     public void setSomeParam(String someParam) {
76         this.someParam = someParam;
77     }
78
79     @Override
80     public Integer getMaxNumberOfThreads() {
81         if (instance == null)
82             return null;
83         return instance.getMaxNumberOfThreads();
84     }
85
86     // this would be generated:
87     private final JmxAttribute threadPoolONJMXAttribute = new JmxAttribute("threadPoolON");
88
89     @Override
90     public void validate() {
91         checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
92         dependencyResolver.validateDependency(
93                 TestingThreadPoolServiceInterface.class, threadPoolON,
94                 threadPoolONJMXAttribute);
95
96         checkState(Strings.isNullOrEmpty(someParam) == false,
97                 "Parameter 'SomeParam' is blank");
98         // check that calling resolveInstance fails
99         try {
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()),
105                     e.getMessage());
106         }
107     }
108
109     @Override
110     public Closeable getInstance() {
111         if (instance == null) {
112             TestingThreadPoolIfc threadPoolInstance = dependencyResolver
113                     .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
114
115             if (oldInstance != null) {
116                 // changing thread pool is not supported
117                 boolean reuse = threadPoolInstance.equals(oldInstance
118                         .getThreadPool());
119                 if (reuse) {
120                     logger.debug("Reusing old instance");
121                     instance = oldInstance;
122                     instance.setSomeParam(someParam);
123                 }
124             }
125             if (instance == null) {
126                 logger.debug("Creating new instance");
127                 if (oldCloseable != null) {
128                     try {
129                         oldCloseable.close();
130                     } catch (Exception e) {
131                         throw new RuntimeException(e);
132                     }
133                 }
134                 instance = new TestingParallelAPSPImpl(threadPoolInstance,
135                         someParam);
136             }
137         }
138         return instance;
139     }
140
141     @Override
142     public ModuleIdentifier getIdentifier() {
143         return identifier;
144     }
145
146
147 }