0b3ac82c29fa4233cfacd6eec01e357d58769af6
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.context.utils;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import lombok.Getter;
30 import lombok.ToString;
31 import org.onap.policy.apex.context.ContextAlbum;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.ContextRuntimeException;
34 import org.onap.policy.apex.context.Distributor;
35 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
36 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
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.testsuites.integration.context.factory.TestContextAlbumFactory;
40 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
41 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
42 import org.onap.policy.common.utils.validation.Assertions;
43
44 /**
45  * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
46  */
47 @Getter
48 @ToString
49 public class ConfigrationProviderImpl implements ConfigrationProvider {
50
51     private final String testName;
52     private final int jvmCount;
53     private final int threadCount;
54     private final int loopSize;
55     private final int albumSize;
56     private final LockType lockType;
57
58     /**
59      * The parameterized ConfigrationProviderImpl constructor.
60      *
61      * @param testType the test type
62      * @param jvmCount the JVM count
63      * @param threadCount the thread count
64      * @param loopSize the size of loop
65      * @param albumSize the size of album
66      * @param lockType the lock type
67      */
68     public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
69                     final int loopSize, final int albumSize, final int lockType) {
70         this.testName = testType;
71         this.jvmCount = jvmCount;
72         this.threadCount = threadCount;
73         this.loopSize = loopSize;
74         this.albumSize = albumSize;
75         this.lockType = LockType.getLockType(lockType);
76     }
77
78     /**
79      * {@inheritDoc}.
80      */
81     @Override
82     public ExecutorService getExecutorService() {
83         final String name = getThreadFactoryName(jvmCount, testName);
84         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
85         return Executors.newFixedThreadPool(threadCount, threadFactory);
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
93         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
94         return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
95     }
96
97     /**
98      * {@inheritDoc}.
99      */
100     @Override
101     public Distributor getDistributor(final AxArtifactKey key) {
102         try {
103             return new DistributorFactory().getDistributor(key);
104         } catch (ContextException e) {
105             throw new ContextRuntimeException("Unable to create Distributor", e);
106         }
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public Distributor getDistributor() {
114         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
115         return getDistributor(distributorKey);
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public ContextAlbum getContextAlbum(final Distributor distributor) {
123         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
124     }
125
126     /**
127      * {@inheritDoc}.
128      *[])
129      */
130     @Override
131     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
132                     final AxArtifactKey[] artifactKeys) {
133         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
134         try {
135             distributor.registerModel(axContextModel);
136             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
137             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
138             contextAlbum.setUserArtifactStack(artifactKeys);
139             return contextAlbum;
140         } catch (ContextException e) {
141             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
142         }
143     }
144
145     /**
146      * {@inheritDoc}.
147      */
148     @Override
149     public Map<String, Object> getContextAlbumInitValues() {
150         final Map<String, Object> values = new HashMap<>();
151         for (int i = 0; i < albumSize; i++) {
152             values.put(Integer.toString(i), new TestContextLongItem(0L));
153         }
154         return values;
155     }
156
157     /**
158      * {@inheritDoc}.
159      */
160     @Override
161     public AlbumModifier getAlbumModifier() {
162         return lockType.getAlbumModifier();
163     }
164
165     /**
166      * Gets the thread factory name.
167      *
168      * @param jvmCount the jvm count
169      * @param testType the test type
170      * @return the thread factory name
171      */
172     private String getThreadFactoryName(final int jvmCount, final String testType) {
173         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
174                         : testType + ":TestConcurrentContextJVMThread_";
175     }
176 }