c632809668426e90fd21918b4a3e08221361cc7b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.utils;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27
28 import org.onap.policy.apex.context.ContextAlbum;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.Distributor;
32 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
33 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
34 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
35 import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier;
36 import org.onap.policy.apex.context.test.lock.modifier.LockType;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
39 import org.onap.policy.apex.model.utilities.Assertions;
40
41 /**
42  * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
43  */
44 public class ConfigrationProviderImpl implements ConfigrationProvider {
45
46     private final String testType;
47     private final int jvmCount;
48     private final int threadCount;
49     private final int loopSize;
50     private final int albumSize;
51     private final LockType lockType;
52
53     /**
54      * The parameterized ConfigrationProviderImpl constructor.
55      * 
56      * @param testType the test type
57      * @param jvmCount the JVM count
58      * @param threadCount the thread count
59      * @param loopSize the size of loop
60      * @param albumSize the size of album
61      * @param lockType the lock type
62      */
63     public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
64                     final int loopSize, final int albumSize, final int lockType) {
65         this.testType = testType;
66         this.jvmCount = jvmCount;
67         this.threadCount = threadCount;
68         this.loopSize = loopSize;
69         this.albumSize = albumSize;
70         this.lockType = LockType.getLockType(lockType);
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getTestName()
77      */
78     @Override
79     public String getTestName() {
80         return testType;
81     }
82
83     /*
84      * (non-Javadoc)
85      * 
86      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLoopSize()
87      */
88     @Override
89     public int getLoopSize() {
90         return loopSize;
91     }
92
93     /*
94      * (non-Javadoc)
95      * 
96      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getThreadCount()
97      */
98     @Override
99     public int getThreadCount() {
100         return threadCount;
101     }
102
103     /*
104      * (non-Javadoc)
105      * 
106      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getJvmCount()
107      */
108     @Override
109     public int getJvmCount() {
110         return jvmCount;
111     }
112
113     /*
114      * (non-Javadoc)
115      * 
116      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumSize()
117      */
118     @Override
119     public int getAlbumSize() {
120         return albumSize;
121     }
122
123     /*
124      * (non-Javadoc)
125      * 
126      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService()
127      */
128     @Override
129     public ExecutorService getExecutorService() {
130         final String name = getThreadFactoryName(jvmCount, testType);
131         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
132         final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
133         return executorService;
134     }
135
136     /*
137      * (non-Javadoc)
138      * 
139      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService(java.lang.String, int)
140      */
141     @Override
142     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
143         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
144         final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
145         return executorService;
146     }
147
148     /*
149      * (non-Javadoc)
150      * 
151      * @see
152      * org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor(org.onap.policy.apex.model.basicmodel
153      * .concepts.AxArtifactKey)
154      */
155     @Override
156     public Distributor getDistributor(final AxArtifactKey key) {
157         try {
158             return new DistributorFactory().getDistributor(key);
159         } catch (ContextException e) {
160             throw new ContextRuntimeException("Unable to create Distributor", e);
161         }
162     }
163
164     /*
165      * (non-Javadoc)
166      * 
167      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor()
168      */
169     @Override
170     public Distributor getDistributor() {
171         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
172         return getDistributor(distributorKey);
173     }
174
175     /*
176      * (non-Javadoc)
177      * 
178      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
179      * Distributor)
180      */
181     @Override
182     public ContextAlbum getContextAlbum(final Distributor distributor) {
183         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
184     }
185
186     /*
187      * (non-Javadoc)
188      * 
189      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
190      * Distributor, org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey,
191      * org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey[])
192      */
193     @Override
194     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
195                     final AxArtifactKey[] artifactKeys) {
196         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
197         try {
198             distributor.registerModel(axContextModel);
199             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
200             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
201             contextAlbum.setUserArtifactStack(artifactKeys);
202             return contextAlbum;
203         } catch (ContextException e) {
204             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
205         }
206     }
207
208     /*
209      * (non-Javadoc)
210      * 
211      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbumInitValues()
212      */
213     @Override
214     public Map<String, Object> getContextAlbumInitValues() {
215         final Map<String, Object> values = new HashMap<>();
216         for (int i = 0; i < albumSize; i++) {
217             values.put(Integer.toString(i), new TestContextLongItem(0L));
218         }
219         return values;
220     }
221
222     /*
223      * (non-Javadoc)
224      * 
225      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumModifier()
226      */
227     @Override
228     public AlbumModifier getAlbumModifier() {
229         return lockType.getAlbumModifier();
230     }
231
232     /*
233      * (non-Javadoc)
234      * 
235      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLockType()
236      */
237     @Override
238     public LockType getLockType() {
239         return lockType;
240     }
241
242     /**
243      * Gets the thread factory name.
244      *
245      * @param jvmCount the jvm count
246      * @param testType the test type
247      * @return the thread factory name
248      */
249     private String getThreadFactoryName(final int jvmCount, final String testType) {
250         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
251                         : testType + ":TestConcurrentContextJVMThread_";
252     }
253
254     /*
255      * (non-Javadoc)
256      * 
257      * @see java.lang.Object#toString()
258      */
259     @Override
260     public String toString() {
261         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
262                         + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType
263                         + "]";
264     }
265
266 }