Adding apex plugins/plugins-context modules
[policy/apex-pdp.git] / plugins / plugins-context / context-distribution / context-distribution-hazelcast / src / main / java / org / onap / policy / apex / plugins / context / distribution / hazelcast / HazelcastContextDistributor.java
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.distribution.hazelcast;
22
23 import java.util.Map;
24
25 import org.onap.policy.apex.context.ContextException;
26 import org.onap.policy.apex.context.impl.distribution.AbstractDistributor;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 import com.hazelcast.core.Hazelcast;
32 import com.hazelcast.core.HazelcastInstance;
33
34 /**
35  * This context distributor distributes context across threads in multiple JVMs on multiple hosts.
36  * It uses hazelcast to distribute maps.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class HazelcastContextDistributor extends AbstractDistributor {
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(HazelcastContextDistributor.class);
43
44     // The hazelcast instance for distributing context for this JVM
45     private static HazelcastInstance hazelcastInstance = null;
46
47     /**
48      * Create an instance of a Hazelcast Context Distributor.
49      *
50      * @throws ContextException On errors creating the context distributor
51      */
52     public HazelcastContextDistributor() throws ContextException {
53         super();
54         LOGGER.entry("HazelcastContextDistributor()");
55
56         LOGGER.exit("HazelcastContextDistributor()");
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.onap.policy.apex.context.impl.distribution.AbstractContextDistributor#init(org.onap.
63      * policy.apex.model.basicmodel.concepts.AxArtifactKey)
64      */
65     @Override
66     public void init(final AxArtifactKey key) throws ContextException {
67         LOGGER.entry("init(" + key + ")");
68
69         super.init(key);
70
71         // Create the hazelcast instance if it does not already exist
72         if (hazelcastInstance == null) {
73             hazelcastInstance = Hazelcast.newHazelcastInstance();
74         }
75
76         LOGGER.exit("init(" + key + ")");
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.onap.policy.apex.core.context.impl.distribution.AbstractContextDistributor#
83      * getContextAlbumMap(org.onap.policy.apex.core.model.concepts.AxArtifactKey)
84      */
85     @Override
86     public Map<String, Object> getContextAlbumMap(final AxArtifactKey contextAlbumKey) {
87         // Get the map from Hazelcast
88         LOGGER.info("HazelcastContextDistributor: create album: " + contextAlbumKey.getID());
89         return hazelcastInstance.getMap(contextAlbumKey.getID());
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see
96      * org.onap.policy.apex.core.context.impl.distribution.AbstractContextDistributor#shutdown()
97      */
98     @Override
99     public void shutdown() {
100         // Shut down the hazelcast instance
101         if (hazelcastInstance != null) {
102             hazelcastInstance.shutdown();
103         }
104         hazelcastInstance = null;
105     }
106 }