70200943cc0df63e67355a1d06b231deeb792cdc
[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         return Executors.newFixedThreadPool(threadCount, threadFactory);
133     }
134
135     /*
136      * (non-Javadoc)
137      * 
138      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getExecutorService(java.lang.String, int)
139      */
140     @Override
141     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
142         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
143         return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
144     }
145
146     /*
147      * (non-Javadoc)
148      * 
149      * @see
150      * org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor(org.onap.policy.apex.model.basicmodel
151      * .concepts.AxArtifactKey)
152      */
153     @Override
154     public Distributor getDistributor(final AxArtifactKey key) {
155         try {
156             return new DistributorFactory().getDistributor(key);
157         } catch (ContextException e) {
158             throw new ContextRuntimeException("Unable to create Distributor", e);
159         }
160     }
161
162     /*
163      * (non-Javadoc)
164      * 
165      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getDistributor()
166      */
167     @Override
168     public Distributor getDistributor() {
169         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
170         return getDistributor(distributorKey);
171     }
172
173     /*
174      * (non-Javadoc)
175      * 
176      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
177      * Distributor)
178      */
179     @Override
180     public ContextAlbum getContextAlbum(final Distributor distributor) {
181         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
182     }
183
184     /*
185      * (non-Javadoc)
186      * 
187      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbum(org.onap.policy.apex.context.
188      * Distributor, org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey,
189      * org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey[])
190      */
191     @Override
192     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
193                     final AxArtifactKey[] artifactKeys) {
194         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
195         try {
196             distributor.registerModel(axContextModel);
197             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
198             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
199             contextAlbum.setUserArtifactStack(artifactKeys);
200             return contextAlbum;
201         } catch (ContextException e) {
202             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
203         }
204     }
205
206     /*
207      * (non-Javadoc)
208      * 
209      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getContextAlbumInitValues()
210      */
211     @Override
212     public Map<String, Object> getContextAlbumInitValues() {
213         final Map<String, Object> values = new HashMap<>();
214         for (int i = 0; i < albumSize; i++) {
215             values.put(Integer.toString(i), new TestContextLongItem(0L));
216         }
217         return values;
218     }
219
220     /*
221      * (non-Javadoc)
222      * 
223      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getAlbumModifier()
224      */
225     @Override
226     public AlbumModifier getAlbumModifier() {
227         return lockType.getAlbumModifier();
228     }
229
230     /*
231      * (non-Javadoc)
232      * 
233      * @see org.onap.policy.apex.context.test.utils.ConfigrationProvider#getLockType()
234      */
235     @Override
236     public LockType getLockType() {
237         return lockType;
238     }
239
240     /**
241      * Gets the thread factory name.
242      *
243      * @param jvmCount the jvm count
244      * @param testType the test type
245      * @return the thread factory name
246      */
247     private String getThreadFactoryName(final int jvmCount, final String testType) {
248         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
249                         : testType + ":TestConcurrentContextJVMThread_";
250     }
251
252     /*
253      * (non-Javadoc)
254      * 
255      * @see java.lang.Object#toString()
256      */
257     @Override
258     public String toString() {
259         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
260                         + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType
261                         + "]";
262     }
263
264 }