Merge "Exception for URI /restconf/operations/module_name:rpc ended with slash"
[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 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;
20
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.NotThreadSafe;
23 import javax.management.ObjectName;
24 import java.io.Closeable;
25
26 import static com.google.common.base.Preconditions.checkNotNull;
27 import static com.google.common.base.Preconditions.checkState;
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
107     @Override
108     public Closeable getInstance() {
109         if (instance == null) {
110             TestingThreadPoolIfc threadPoolInstance = dependencyResolver
111                     .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
112
113             if (oldInstance != null) {
114                 // changing thread pool is not supported
115                 boolean reuse = threadPoolInstance == oldInstance.getThreadPool();
116                 if (reuse) {
117                     logger.debug("Reusing old instance");
118                     instance = oldInstance;
119                     instance.setSomeParam(someParam);
120                 }
121             }
122             if (instance == null) {
123                 logger.debug("Creating new instance");
124                 if (oldCloseable != null) {
125                     try {
126                         oldCloseable.close();
127                     } catch (Exception e) {
128                         throw new RuntimeException(e);
129                     }
130                 }
131                 instance = new TestingParallelAPSPImpl(threadPoolInstance,
132                         someParam);
133             }
134         }
135         return instance;
136     }
137
138     @Override
139     public ModuleIdentifier getIdentifier() {
140         return identifier;
141     }
142
143
144 }