Initial code drop of yang model driven configuration system
[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.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;
27
28 import com.google.common.base.Strings;
29
30 /**
31  * Represents service that has dependency to thread pool.
32  */
33 @NotThreadSafe
34 public class TestingParallelAPSPModule implements Module,
35         TestingParallelAPSPConfigMXBean {
36     private static final Logger logger = LoggerFactory
37             .getLogger(TestingParallelAPSPModule.class);
38
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;
46
47     public TestingParallelAPSPModule(ModuleIdentifier name,
48             DependencyResolver dependencyResolver,
49             @Nullable AutoCloseable oldCloseable,
50             @Nullable TestingParallelAPSPImpl oldInstance) {
51         this.name = name;
52         this.dependencyResolver = dependencyResolver;
53         this.oldCloseable = oldCloseable;
54         this.oldInstance = oldInstance;
55     }
56
57     @Override
58     public ModuleIdentifier getName() {
59         return name;
60     }
61
62     @Override
63     public ObjectName getThreadPool() {
64         return threadPoolON;
65     }
66
67     @RequireInterface(TestingThreadPoolServiceInterface.class)
68     @Override
69     public void setThreadPool(ObjectName threadPoolName) {
70         this.threadPoolON = threadPoolName;
71     }
72
73     @Override
74     public String getSomeParam() {
75         return someParam;
76     }
77
78     @Override
79     public void setSomeParam(String someParam) {
80         this.someParam = someParam;
81     }
82
83     @Override
84     public Integer getMaxNumberOfThreads() {
85         if (instance == null)
86             return null;
87         return instance.getMaxNumberOfThreads();
88     }
89
90     @Override
91     public void validate() {
92         checkNotNull(threadPoolON, "Parameter 'threadPool' must be set");
93         dependencyResolver.validateDependency(
94                 TestingThreadPoolServiceInterface.class, threadPoolON,
95                 "threadPoolON");
96
97         checkState(Strings.isNullOrEmpty(someParam) == false,
98                 "Parameter 'SomeParam' is blank");
99         // check that calling resolveInstance fails
100         try {
101             dependencyResolver.resolveInstance(TestingThreadPoolIfc.class,
102                     threadPoolON);
103             throw new RuntimeException("fail");
104         } catch (IllegalStateException e) {
105             checkState("Commit was not triggered".equals(e.getMessage()),
106                     e.getMessage());
107         }
108     }
109
110     @Override
111     public Closeable getInstance() {
112         if (instance == null) {
113             TestingThreadPoolIfc threadPoolInstance = dependencyResolver
114                     .resolveInstance(TestingThreadPoolIfc.class, threadPoolON);
115
116             if (oldInstance != null) {
117                 // changing thread pool is not supported
118                 boolean reuse = threadPoolInstance.equals(oldInstance
119                         .getThreadPool());
120                 if (reuse) {
121                     logger.debug("Reusing old instance");
122                     instance = oldInstance;
123                     instance.setSomeParam(someParam);
124                 }
125             }
126             if (instance == null) {
127                 logger.debug("Creating new instance");
128                 if (oldCloseable != null) {
129                     try {
130                         oldCloseable.close();
131                     } catch (Exception e) {
132                         throw new RuntimeException(e);
133                     }
134                 }
135                 instance = new TestingParallelAPSPImpl(threadPoolInstance,
136                         someParam);
137             }
138         }
139         return instance;
140     }
141 }