87b81643a0fa62a13ab3c0bef4da9c552b4ba185
[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 public class ConfigrationProviderImpl implements ConfigrationProvider {
42
43     private final String testType;
44     private final int jvmCount;
45     private final int threadCount;
46     private final int loopSize;
47     private final int albumSize;
48     private final LockType lockType;
49
50     /**
51      * The parameterized ConfigrationProviderImpl constructor.
52      * @param testType the test type
53      * @param jvmCount the JVM count
54      * @param threadCount the thread count
55      * @param loopSize the size of loop
56      * @param albumSize the size of album
57      * @param lockType the lock type
58      */
59     public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
60             final int loopSize, final int albumSize, final int lockType) {
61         this.testType = testType;
62         this.jvmCount = jvmCount;
63         this.threadCount = threadCount;
64         this.loopSize = loopSize;
65         this.albumSize = albumSize;
66         this.lockType = LockType.getLockType(lockType);
67     }
68
69     @Override
70     public String getTestName() {
71         return testType;
72     }
73
74     @Override
75     public int getLoopSize() {
76         return loopSize;
77     }
78
79     @Override
80     public int getThreadCount() {
81         return threadCount;
82     }
83
84     @Override
85     public int getJvmCount() {
86         return jvmCount;
87     }
88
89     @Override
90     public int getAlbumSize() {
91         return albumSize;
92     }
93
94     @Override
95     public ExecutorService getExecutorService() {
96         final String name = getThreadFactoryName(jvmCount, testType);
97         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
98         final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
99         return executorService;
100     }
101
102     @Override
103     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
104         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
105         final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
106         return executorService;
107     }
108
109     @Override
110     public Distributor getDistributor(final AxArtifactKey key) {
111         try {
112             return new DistributorFactory().getDistributor(key);
113         } catch (ContextException e) {
114             throw new ContextRuntimeException("Unable to create Distributor", e);
115         }
116     }
117
118     @Override
119     public Distributor getDistributor() {
120         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
121         return getDistributor(distributorKey);
122     }
123
124     @Override
125     public ContextAlbum getContextAlbum(final Distributor distributor) {
126         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
127     }
128
129     @Override
130     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
131             final AxArtifactKey[] artifactKeys) {
132         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
133         try {
134             distributor.registerModel(axContextModel);
135             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
136             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
137             contextAlbum.setUserArtifactStack(artifactKeys);
138             return contextAlbum;
139         } catch (ContextException e) {
140             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
141         }
142     }
143
144     @Override
145     public Map<String, Object> getContextAlbumInitValues() {
146         final Map<String, Object> values = new HashMap<>();
147         for (int i = 0; i < albumSize; i++) {
148             values.put(Integer.toString(i), new TestContextLongItem(0L));
149         }
150         return values;
151     }
152
153     @Override
154     public AlbumModifier getAlbumModifier() {
155         return lockType.getAlbumModifier();
156     }
157
158     @Override
159     public LockType getLockType() {
160         return lockType;
161     }
162
163
164     private String getThreadFactoryName(final int jvmCount, final String testType) {
165         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
166                 : testType + ":TestConcurrentContextJVMThread_";
167     }
168
169     @Override
170     public String toString() {
171         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
172                 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType + "]";
173     }
174
175
176 }