50429099e61ca1773953573a7f37cd90d32b166d
[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 package org.onap.policy.apex.context.test.utils;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26
27 import org.onap.policy.apex.context.ContextAlbum;
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.context.ContextRuntimeException;
30 import org.onap.policy.apex.context.Distributor;
31 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
32 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
33 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
34 import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier;
35 import org.onap.policy.apex.context.test.lock.modifier.LockType;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
38 import org.onap.policy.apex.model.utilities.Assertions;
39
40 public class ConfigrationProviderImpl implements ConfigrationProvider {
41
42     private final String testType;
43     private final int jvmCount;
44     private final int threadCount;
45     private final int loopSize;
46     private final int albumSize;
47     private final LockType lockType;
48
49     public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
50             final int loopSize, final int albumSize, final int lockType) {
51         this.testType = testType;
52         this.jvmCount = jvmCount;
53         this.threadCount = threadCount;
54         this.loopSize = loopSize;
55         this.albumSize = albumSize;
56         this.lockType = LockType.getLockType(lockType);
57     }
58
59     @Override
60     public String getTestName() {
61         return testType;
62     }
63
64     @Override
65     public int getLoopSize() {
66         return loopSize;
67     }
68
69     @Override
70     public int getThreadCount() {
71         return threadCount;
72     }
73
74     @Override
75     public int getJvmCount() {
76         return jvmCount;
77     }
78
79     @Override
80     public int getAlbumSize() {
81         return albumSize;
82     }
83
84     @Override
85     public ExecutorService getExecutorService() {
86         final String name = getThreadFactoryName(jvmCount, testType);
87         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
88         final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
89         return executorService;
90     }
91
92     @Override
93     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
94         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
95         final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
96         return executorService;
97     }
98
99     @Override
100     public Distributor getDistributor(final AxArtifactKey key) {
101         try {
102             return new DistributorFactory().getDistributor(key);
103         } catch (ContextException e) {
104             throw new ContextRuntimeException("Unable to create Distributor", e);
105         }
106     }
107
108     @Override
109     public Distributor getDistributor() {
110         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
111         return getDistributor(distributorKey);
112     }
113
114     @Override
115     public ContextAlbum getContextAlbum(final Distributor distributor) {
116         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
117     }
118
119     @Override
120     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
121             final AxArtifactKey[] artifactKeys) {
122         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
123         try {
124             distributor.registerModel(axContextModel);
125             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
126             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
127             contextAlbum.setUserArtifactStack(artifactKeys);
128             return contextAlbum;
129         } catch (ContextException e) {
130             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
131         }
132     }
133
134     @Override
135     public Map<String, Object> getContextAlbumInitValues() {
136         final Map<String, Object> values = new HashMap<>();
137         for (int i = 0; i < albumSize; i++) {
138             values.put(Integer.toString(i), new TestContextLongItem(0l));
139         }
140         return values;
141     }
142
143     @Override
144     public AlbumModifier getAlbumModifier() {
145         return lockType.getAlbumModifier();
146     }
147
148     @Override
149     public LockType getLockType() {
150         return lockType;
151     }
152
153
154     private String getThreadFactoryName(final int jvmCount, final String testType) {
155         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
156                 : testType + ":TestConcurrentContextJVMThread_";
157     }
158
159     @Override
160     public String toString() {
161         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
162                 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType + "]";
163     }
164
165
166 }