0a9a195e9ba6f2c0febaac754c6beadcc86fcadb
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.utils;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28
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.context.test.concepts.TestContextLongItem;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
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;
41
42 /**
43  * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
44  */
45 public class ConfigrationProviderImpl implements ConfigrationProvider {
46
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;
53
54     /**
55      * The parameterized ConfigrationProviderImpl constructor.
56      *
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
63      */
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);
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getTestName()
78      */
79     @Override
80     public String getTestName() {
81         return testType;
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLoopSize()
88      */
89     @Override
90     public int getLoopSize() {
91         return loopSize;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getThreadCount()
98      */
99     @Override
100     public int getThreadCount() {
101         return threadCount;
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getJvmCount()
108      */
109     @Override
110     public int getJvmCount() {
111         return jvmCount;
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumSize()
118      */
119     @Override
120     public int getAlbumSize() {
121         return albumSize;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService()
128      */
129     @Override
130     public ExecutorService getExecutorService() {
131         final String name = getThreadFactoryName(jvmCount, testType);
132         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
133         return Executors.newFixedThreadPool(threadCount, threadFactory);
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         return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see
151      * org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor(org.onap.policy.apex.model.basicmodel
152      * .concepts.AxArtifactKey)
153      */
154     @Override
155     public Distributor getDistributor(final AxArtifactKey key) {
156         try {
157             return new DistributorFactory().getDistributor(key);
158         } catch (ContextException e) {
159             throw new ContextRuntimeException("Unable to create Distributor", e);
160         }
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor()
167      */
168     @Override
169     public Distributor getDistributor() {
170         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
171         return getDistributor(distributorKey);
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
178      * Distributor)
179      */
180     @Override
181     public ContextAlbum getContextAlbum(final Distributor distributor) {
182         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
183     }
184
185     /*
186      * (non-Javadoc)
187      *
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[])
191      */
192     @Override
193     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
194                     final AxArtifactKey[] artifactKeys) {
195         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
196         try {
197             distributor.registerModel(axContextModel);
198             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
199             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
200             contextAlbum.setUserArtifactStack(artifactKeys);
201             return contextAlbum;
202         } catch (ContextException e) {
203             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
204         }
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbumInitValues()
211      */
212     @Override
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));
217         }
218         return values;
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumModifier()
225      */
226     @Override
227     public AlbumModifier getAlbumModifier() {
228         return lockType.getAlbumModifier();
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLockType()
235      */
236     @Override
237     public LockType getLockType() {
238         return lockType;
239     }
240
241     /**
242      * Gets the thread factory name.
243      *
244      * @param jvmCount the jvm count
245      * @param testType the test type
246      * @return the thread factory name
247      */
248     private String getThreadFactoryName(final int jvmCount, final String testType) {
249         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
250                         : testType + ":TestConcurrentContextJVMThread_";
251     }
252
253     /*
254      * (non-Javadoc)
255      *
256      * @see java.lang.Object#toString()
257      */
258     @Override
259     public String toString() {
260         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
261                         + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType
262                         + "]";
263     }
264
265 }