81ba49e1aaad9ac6ef53d82404cfe8f9b481f027
[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 name;
44     private ObjectName threadPoolON;
45     private TestingParallelAPSPImpl instance;
46     private String someParam;
47
48     public TestingParallelAPSPModule(ModuleIdentifier name,
49             DependencyResolver dependencyResolver,
50             @Nullable AutoCloseable oldCloseable,
51             @Nullable TestingParallelAPSPImpl oldInstance) {
52         this.name = name;
53         this.dependencyResolver = dependencyResolver;
54         this.oldCloseable = oldCloseable;
55         this.oldInstance = oldInstance;
56     }
57
58     @Override
59     public ModuleIdentifier getName() {
60         return name;
61     }
62
63     @Override
64     public ObjectName getThreadPool() {
65         return threadPoolON;
66     }
67
68     @RequireInterface(TestingThreadPoolServiceInterface.class)
69     @Override
70     public void setThreadPool(ObjectName threadPoolName) {
71         this.threadPoolON = threadPoolName;
72     }
73
74     @Override
75     public String getSomeParam() {
76         return someParam;
77     }
78
79     @Override
80     public void setSomeParam(String someParam) {
81         this.someParam = someParam;
82     }
83
84     @Override
85     public Integer getMaxNumberOfThreads() {
86         if (instance == null)
87             return null;
88         return instance.getMaxNumberOfThreads();
89     }
90
91     // this would be generated:
92     private final JmxAttribute threadPoolONJMXAttribute = new JmxAttribute("threadPoolON");
93
94     @Override
95     public void validate() {
96         checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
97         dependencyResolver.validateDependency(
98                 TestingThreadPoolServiceInterface.class, threadPoolON,
99                 threadPoolONJMXAttribute);
100
101         checkState(Strings.isNullOrEmpty(someParam) == false,
102                 "Parameter 'SomeParam' is blank");
103         // check that calling resolveInstance fails
104         try {
105             dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
106                     threadPoolON, threadPoolONJMXAttribute);
107             throw new RuntimeException("fail");
108         } catch (IllegalStateException e) {
109             checkState("Commit was not triggered".equals(e.getMessage()),
110                     e.getMessage());
111         }
112     }
113
114     @Override
115     public Closeable getInstance() {
116         if (instance == null) {
117             TestingThreadPoolIfc threadPoolInstance = dependencyResolver
118                     .resolveInstance(TestingThreadPoolIfc.class, threadPoolON, threadPoolONJMXAttribute);
119
120             if (oldInstance != null) {
121                 // changing thread pool is not supported
122                 boolean reuse = threadPoolInstance.equals(oldInstance
123                         .getThreadPool());
124                 if (reuse) {
125                     logger.debug("Reusing old instance");
126                     instance = oldInstance;
127                     instance.setSomeParam(someParam);
128                 }
129             }
130             if (instance == null) {
131                 logger.debug("Creating new instance");
132                 if (oldCloseable != null) {
133                     try {
134                         oldCloseable.close();
135                     } catch (Exception e) {
136                         throw new RuntimeException(e);
137                     }
138                 }
139                 instance = new TestingParallelAPSPImpl(threadPoolInstance,
140                         someParam);
141             }
142         }
143         return instance;
144     }
145 }