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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.test;
23 import java.util.Map.Entry;
24 import java.util.Random;
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;
41 * @author Liam Fallon (liam.fallon@ericsson.com)
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;
49 * Default constructor is private to avoid subclassing.
51 private HazelcastContextInjection() {}
56 * @param args the arguments to the method
57 * @throws ContextException exceptions thrown on context injection
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
62 final AxContextModel contextModel = createContextModel();
64 // The model must be registered in the model service.
65 ModelService.registerModel(AxContextSchemas.class, contextModel.getSchemas());
66 ModelService.registerModel(AxContextAlbums.class, contextModel.getAlbums());
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");
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);
80 // Now, get a handle on the album
81 final ContextAlbum myContextAlbum =
82 contextDistributor.createContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"));
84 final int jvmID = new Random().nextInt(MAX_INT_10000);
85 final String myLongKey = "MyLong_" + jvmID;
86 final String commonLongKey = "CommonLong";
88 // Put the long value for this JVM into the map
89 myContextAlbum.put(myLongKey, new Long(0L));
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));
96 myContextAlbum.unlockForWriting(commonLongKey);
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);
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());
107 System.out.println("old " + myLongKey + ": " + myContextAlbum.get(myLongKey));
109 myContextAlbum.lockForReading(commonLongKey);
110 System.out.println("old CommonLong: " + myContextAlbum.get(commonLongKey));
111 myContextAlbum.unlockForReading(commonLongKey);
113 Long myLong = (Long) myContextAlbum.get(myLongKey);
115 myContextAlbum.put(myLongKey, myLong);
117 myContextAlbum.lockForWriting(commonLongKey);
118 Long commonLong = (Long) myContextAlbum.get(commonLongKey);
120 myContextAlbum.put(commonLongKey, commonLong);
121 myContextAlbum.unlockForWriting(commonLongKey);
123 System.out.println("new myLong: " + myContextAlbum.get(myLongKey));
124 System.out.println("new commonLong: " + myContextAlbum.get(commonLongKey));
127 Thread.sleep(ONE_SECOND);
128 } catch (final Exception e) {
133 contextDistributor.clear();
137 * This method just creates a simple context model programatically.
139 * @return a context model
141 public static AxContextModel createContextModel() {
142 final AxContextSchema longSchema =
143 new AxContextSchema(new AxArtifactKey("LongSchema", "0.0.1"), "Java", "java.lang.Long");
145 final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1"));
146 schemas.getSchemasMap().put(longSchema.getKey(), longSchema);
148 final AxContextAlbum longAlbumDefinition = new AxContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"),
149 "APPLICATION", true, longSchema.getKey());
151 final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1"));
152 albums.getAlbumsMap().put(longAlbumDefinition.getKey(), longAlbumDefinition);
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);