4c21103c3cd7d56bb2040813de5edc58ef43031c
[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.plugins.context.test;
22
23 import java.util.Map.Entry;
24 import java.util.Random;
25
26 import org.onap.policy.apex.context.ContextAlbum;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.context.Distributor;
29 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
30 import org.onap.policy.apex.context.parameters.ContextParameters;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
33 import org.onap.policy.apex.model.basicmodel.service.ModelService;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39
40 /**
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public final class HazelcastContextInjection {
44     private static final int ONE_SECOND = 1000;
45     private static final int SIXTY_SECONDS = 60;
46     private static final int MAX_INT_10000 = 10000;
47
48     /**
49      * Default constructor is private to avoid subclassing.
50      */
51     private HazelcastContextInjection() {}
52
53     /**
54      * The main method.
55      * 
56      * @param args the arguments to the method
57      * @throws ContextException exceptions thrown on context injection
58      */
59     public static void main(final String[] args) throws ContextException {
60         // For convenience, I created model programmatically, it can of course be read in from a
61         // policy model
62         final AxContextModel contextModel = createContextModel();
63
64         // The model must be registered in the model service.
65         ModelService.registerModel(AxContextSchemas.class, contextModel.getSchemas());
66         ModelService.registerModel(AxContextAlbums.class, contextModel.getAlbums());
67
68         // Configure APex to use Hazelcast distribution and locking
69         final ContextParameters contextParameters = new ContextParameters();
70         contextParameters.getDistributorParameters()
71                 .setPluginClass("com.ericsson.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
72         contextParameters.getLockManagerParameters()
73                 .setPluginClass("com.ericsson.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
74
75         // Fire up our distribution
76         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
77         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
78         contextDistributor.init(distributorKey);
79
80         // Now, get a handle on the album
81         final ContextAlbum myContextAlbum =
82                 contextDistributor.createContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"));
83
84         final int jvmID = new Random().nextInt(MAX_INT_10000);
85         final String myLongKey = "MyLong_" + jvmID;
86         final String commonLongKey = "CommonLong";
87
88         // Put the long value for this JVM into the map
89         myContextAlbum.put(myLongKey, new Long(0L));
90
91         // Put the common long value to be used across JVMS in the map if its not htere already
92         myContextAlbum.lockForWriting(commonLongKey);
93         if (myContextAlbum.get(commonLongKey) == null) {
94             myContextAlbum.put(commonLongKey, new Long(0L));
95         }
96         myContextAlbum.unlockForWriting(commonLongKey);
97
98         // Run for 60 seconds to show multi JVM
99         for (int i = 0; i < SIXTY_SECONDS; i++) {
100             System.out.println("JVM " + jvmID + " iteration " + i);
101
102             for (final Entry<String, Object> albumEntry : myContextAlbum.entrySet()) {
103                 myContextAlbum.lockForReading(albumEntry.getKey());
104                 System.out.println(albumEntry.getKey() + "-->" + albumEntry.getValue());
105                 myContextAlbum.unlockForReading(albumEntry.getKey());
106             }
107             System.out.println("old " + myLongKey + ": " + myContextAlbum.get(myLongKey));
108
109             myContextAlbum.lockForReading(commonLongKey);
110             System.out.println("old CommonLong: " + myContextAlbum.get(commonLongKey));
111             myContextAlbum.unlockForReading(commonLongKey);
112
113             Long myLong = (Long) myContextAlbum.get(myLongKey);
114             myLong++;
115             myContextAlbum.put(myLongKey, myLong);
116
117             myContextAlbum.lockForWriting(commonLongKey);
118             Long commonLong = (Long) myContextAlbum.get(commonLongKey);
119             commonLong++;
120             myContextAlbum.put(commonLongKey, commonLong);
121             myContextAlbum.unlockForWriting(commonLongKey);
122
123             System.out.println("new myLong: " + myContextAlbum.get(myLongKey));
124             System.out.println("new commonLong: " + myContextAlbum.get(commonLongKey));
125
126             try {
127                 Thread.sleep(ONE_SECOND);
128             } catch (final Exception e) {
129                 e.printStackTrace();
130             }
131         }
132
133         contextDistributor.clear();
134     }
135
136     /**
137      * This method just creates a simple context model programatically.
138      * 
139      * @return a context model
140      */
141     public static AxContextModel createContextModel() {
142         final AxContextSchema longSchema =
143                 new AxContextSchema(new AxArtifactKey("LongSchema", "0.0.1"), "Java", "java.lang.Long");
144
145         final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1"));
146         schemas.getSchemasMap().put(longSchema.getKey(), longSchema);
147
148         final AxContextAlbum longAlbumDefinition = new AxContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"),
149                 "APPLICATION", true, longSchema.getKey());
150
151         final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1"));
152         albums.getAlbumsMap().put(longAlbumDefinition.getKey(), longAlbumDefinition);
153
154         final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"));
155         final AxContextModel contextModel =
156                 new AxContextModel(new AxArtifactKey("LongContextModel", "0.0.1"), schemas, albums, keyInformation);
157         contextModel.setKeyInformation(keyInformation);
158         keyInformation.generateKeyInfo(contextModel);
159
160         return contextModel;
161     }
162 }