554c3d0058e40c4289fafd8ae81980b31960c681
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.locking;
22
23 import org.onap.policy.apex.context.ContextAlbum;
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.context.Distributor;
26 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
27 import org.onap.policy.apex.context.parameters.ContextParameters;
28 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
29 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * The Class TestConcurrentContextThread tests concurrent use of context.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class ConcurrentContextThread implements Runnable {
42     // Logger for this class
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class);
44     private final Distributor distributor;
45     private final int jvm;
46     private final int instance;
47     private final int threadLoops;
48
49     /**
50      * The Constructor.
51      *
52      * @param jvm the jvm
53      * @param instance the instance
54      * @param threadLoops the thread loops
55      * @throws ApexException the apex exception
56      */
57     public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException {
58         this.jvm = jvm;
59         this.instance = instance;
60         this.threadLoops = threadLoops;
61
62         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
63
64         new ContextParameters();
65         distributor = new DistributorFactory().getDistributor(distributorKey);
66         final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
67         distributor.registerModel(albumsModel);
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see java.lang.Runnable#run()
74      */
75     @Override
76     public void run() {
77         LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
78
79         ContextAlbum lTypeAlbum = null;
80
81         try {
82             lTypeAlbum = distributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
83         } catch (final Exception e) {
84             LOGGER.error("could not get the test context album", e);
85             LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
86             return;
87         }
88
89         if (lTypeAlbum == null) {
90             LOGGER.error("could not find the test context album");
91             LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
92             return;
93         }
94
95         // @formatter:off
96         final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
97                 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
98         // @formatter:on
99
100         lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
101
102         try {
103             updateAlbum(lTypeAlbum);
104         } catch (final Exception exception) {
105             LOGGER.error("could not set the value in the test context album", exception);
106             LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
107             return;
108         }
109
110         try {
111             lTypeAlbum.lockForWriting("testValue");
112             final TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get("testValue");
113             final long value = item.getLongValue();
114             LOGGER.info("completed TestConcurrentContextThread_" + jvm + "_" + instance + ", value=" + value);
115         } catch (final Exception e) {
116             LOGGER.error("could not read the value in the test context album", e);
117             LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
118         } finally {
119             try {
120                 lTypeAlbum.unlockForWriting("testValue");
121                 distributor.shutdown();
122             } catch (final ContextException e) {
123                 LOGGER.error("could not unlock test context album item", e);
124                 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
125             }
126         }
127     }
128
129     private void updateAlbum(final ContextAlbum lTypeAlbum) throws Exception {
130         for (int i = 0; i < threadLoops; i++) {
131             try {
132                 lTypeAlbum.lockForWriting("testValue");
133                 TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get("testValue");
134                 if (item != null) {
135                     long value = item.getLongValue();
136                     item.setLongValue(++value);
137                 } else {
138                     item = new TestContextLongItem(0L);
139                 }
140                 lTypeAlbum.put("testValue", item);
141             } finally {
142                 lTypeAlbum.unlockForWriting("testValue");
143             }
144         }
145     }
146 }