Timeout for Subscription Create Partial Response
[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.mode.kubernetes.enabled}")
39     protected boolean cacheKubernetesEnabled;
40
41     @Value("${hazelcast.mode.kubernetes.service-name}")
42     protected String cacheKubernetesServiceName;
43
44     protected HazelcastInstance createHazelcastInstance(final String hazelcastInstanceName,
45                                                         final NamedConfig namedConfig, final String clusterName) {
46         return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig, clusterName));
47     }
48
49     private Config initializeConfig(final String instanceName, final NamedConfig namedConfig,
50                                     final String clusterName) {
51         final Config config = new Config(instanceName);
52         if (namedConfig instanceof MapConfig) {
53             config.addMapConfig((MapConfig) namedConfig);
54         }
55         if (namedConfig instanceof QueueConfig) {
56             config.addQueueConfig((QueueConfig) namedConfig);
57         }
58         config.setClusterName(clusterName);
59         updateDiscoveryMode(config);
60         return config;
61     }
62
63     protected static MapConfig createMapConfig(final String configName) {
64         final MapConfig mapConfig = new MapConfig(configName);
65         mapConfig.setBackupCount(3);
66         mapConfig.setAsyncBackupCount(3);
67         return mapConfig;
68     }
69
70     protected static QueueConfig createQueueConfig(final String configName) {
71         final QueueConfig commonQueueConfig = new QueueConfig(configName);
72         commonQueueConfig.setBackupCount(3);
73         commonQueueConfig.setAsyncBackupCount(3);
74         return commonQueueConfig;
75     }
76
77     protected void updateDiscoveryMode(final Config config) {
78         if (cacheKubernetesEnabled) {
79             log.info("Enabling kubernetes mode with service-name : {}", cacheKubernetesServiceName);
80             config.getNetworkConfig().getJoin().getKubernetesConfig().setEnabled(true)
81                 .setProperty("service-name", cacheKubernetesServiceName);
82         }
83     }
84
85 }