405e6e2a885801292aed1e005655eaf6545ca763
[cps.git] / cps-service / src / main / java / org / onap / cps / cache / HazelcastCacheConfig.java
1 /*
2  * ============LICENSE_START========================================================
3  *  Copyright (C) 2023 Nordix Foundation
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.cps.cache;
22
23 import com.hazelcast.config.Config;
24 import com.hazelcast.config.MapConfig;
25 import com.hazelcast.config.NamedConfig;
26 import com.hazelcast.config.QueueConfig;
27 import com.hazelcast.core.Hazelcast;
28 import com.hazelcast.core.HazelcastInstance;
29 import lombok.extern.slf4j.Slf4j;
30 import org.springframework.beans.factory.annotation.Value;
31
32 /**
33  * Core infrastructure of the hazelcast distributed cache.
34  */
35 @Slf4j
36 public class HazelcastCacheConfig {
37
38     @Value("${hazelcast.cluster-name}")
39     protected String clusterName;
40
41     @Value("${hazelcast.mode.kubernetes.enabled}")
42     protected boolean cacheKubernetesEnabled;
43
44     @Value("${hazelcast.mode.kubernetes.service-name}")
45     protected String cacheKubernetesServiceName;
46
47     protected HazelcastInstance createHazelcastInstance(final String hazelcastInstanceName,
48                                                         final NamedConfig namedConfig) {
49         return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig));
50     }
51
52     private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) {
53         final Config config = new Config(instanceName);
54         if (namedConfig instanceof MapConfig) {
55             config.addMapConfig((MapConfig) namedConfig);
56         }
57         if (namedConfig instanceof QueueConfig) {
58             config.addQueueConfig((QueueConfig) namedConfig);
59         }
60         config.setClusterName(clusterName);
61         updateDiscoveryMode(config);
62         return config;
63     }
64
65     protected static MapConfig createMapConfig(final String configName) {
66         final MapConfig mapConfig = new MapConfig(configName);
67         mapConfig.setBackupCount(3);
68         mapConfig.setAsyncBackupCount(3);
69         return mapConfig;
70     }
71
72     protected static QueueConfig createQueueConfig(final String configName) {
73         final QueueConfig commonQueueConfig = new QueueConfig(configName);
74         commonQueueConfig.setBackupCount(3);
75         commonQueueConfig.setAsyncBackupCount(3);
76         return commonQueueConfig;
77     }
78
79     protected void updateDiscoveryMode(final Config config) {
80         if (cacheKubernetesEnabled) {
81             log.info("Enabling kubernetes mode with service-name : {}", cacheKubernetesServiceName);
82             config.getNetworkConfig().getJoin().getKubernetesConfig().setEnabled(true)
83                 .setProperty("service-name", cacheKubernetesServiceName);
84         }
85     }
86
87 }