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