2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.context.utils;
24 import java.util.HashMap;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
29 import org.onap.policy.apex.context.ContextAlbum;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.context.Distributor;
33 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
36 import org.onap.policy.apex.testsuites.integration.context.concepts.TestContextLongItem;
37 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
38 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
39 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
40 import org.onap.policy.common.utils.validation.Assertions;
43 * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
45 public class ConfigrationProviderImpl implements ConfigrationProvider {
47 private final String testType;
48 private final int jvmCount;
49 private final int threadCount;
50 private final int loopSize;
51 private final int albumSize;
52 private final LockType lockType;
55 * The parameterized ConfigrationProviderImpl constructor.
57 * @param testType the test type
58 * @param jvmCount the JVM count
59 * @param threadCount the thread count
60 * @param loopSize the size of loop
61 * @param albumSize the size of album
62 * @param lockType the lock type
64 public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
65 final int loopSize, final int albumSize, final int lockType) {
66 this.testType = testType;
67 this.jvmCount = jvmCount;
68 this.threadCount = threadCount;
69 this.loopSize = loopSize;
70 this.albumSize = albumSize;
71 this.lockType = LockType.getLockType(lockType);
77 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getTestName()
80 public String getTestName() {
87 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLoopSize()
90 public int getLoopSize() {
97 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getThreadCount()
100 public int getThreadCount() {
107 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getJvmCount()
110 public int getJvmCount() {
117 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumSize()
120 public int getAlbumSize() {
127 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService()
130 public ExecutorService getExecutorService() {
131 final String name = getThreadFactoryName(jvmCount, testType);
132 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
133 return Executors.newFixedThreadPool(threadCount, threadFactory);
139 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService(java.lang.String, int)
142 public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
143 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
144 return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
151 * org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor(org.onap.policy.apex.model.basicmodel
152 * .concepts.AxArtifactKey)
155 public Distributor getDistributor(final AxArtifactKey key) {
157 return new DistributorFactory().getDistributor(key);
158 } catch (ContextException e) {
159 throw new ContextRuntimeException("Unable to create Distributor", e);
166 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor()
169 public Distributor getDistributor() {
170 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
171 return getDistributor(distributorKey);
177 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
181 public ContextAlbum getContextAlbum(final Distributor distributor) {
182 return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
188 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
189 * Distributor, org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey,
190 * org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey[])
193 public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
194 final AxArtifactKey[] artifactKeys) {
195 final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
197 distributor.registerModel(axContextModel);
198 final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
199 Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
200 contextAlbum.setUserArtifactStack(artifactKeys);
202 } catch (ContextException e) {
203 throw new ContextRuntimeException("Unable to create ContextAlbum", e);
210 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbumInitValues()
213 public Map<String, Object> getContextAlbumInitValues() {
214 final Map<String, Object> values = new HashMap<>();
215 for (int i = 0; i < albumSize; i++) {
216 values.put(Integer.toString(i), new TestContextLongItem(0L));
224 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumModifier()
227 public AlbumModifier getAlbumModifier() {
228 return lockType.getAlbumModifier();
234 * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLockType()
237 public LockType getLockType() {
242 * Gets the thread factory name.
244 * @param jvmCount the jvm count
245 * @param testType the test type
246 * @return the thread factory name
248 private String getThreadFactoryName(final int jvmCount, final String testType) {
249 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
250 : testType + ":TestConcurrentContextJVMThread_";
256 * @see java.lang.Object#toString()
259 public String toString() {
260 return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
261 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType