0b672663754b5bff157a6f6ccb77796836613aea
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / config / embeddedcache / SynchronizationCacheConfig.java
1 /*
2  * ============LICENSE_START========================================================
3  *  Copyright (C) 2022-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.ncmp.api.impl.config.embeddedcache;
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 com.hazelcast.map.IMap;
30 import java.util.concurrent.BlockingQueue;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.spi.model.DataNode;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.context.annotation.Bean;
35 import org.springframework.context.annotation.Configuration;
36
37 /**
38  * Core infrastructure of the hazelcast distributed caches for Module Sync and Data Sync use cases.
39  */
40 @Slf4j
41 @Configuration
42 public class SynchronizationCacheConfig {
43
44     public static final int MODULE_SYNC_STARTED_TTL_SECS = 600;
45     public static final int DATA_SYNC_SEMAPHORE_TTL_SECS = 1800;
46
47     @Value("${hazelcast.mode.kubernetes.enabled}")
48     private boolean cacheKubernetesEnabled;
49
50     @Value("${hazelcast.mode.kubernetes.service-name}")
51     private String cacheKubernetesServiceName;
52
53     private static final QueueConfig commonQueueConfig = createQueueConfig();
54     private static final MapConfig moduleSyncStartedConfig = createMapConfig("moduleSyncStartedConfig");
55     private static final MapConfig dataSyncSemaphoresConfig = createMapConfig("dataSyncSemaphoresConfig");
56
57     /**
58      * Module Sync Distributed Queue Instance.
59      *
60      * @return queue of cm handles (data nodes) that need module sync
61      */
62     @Bean
63     public BlockingQueue<DataNode> moduleSyncWorkQueue() {
64         return createHazelcastInstance("moduleSyncWorkQueue", commonQueueConfig)
65             .getQueue("moduleSyncWorkQueue");
66     }
67
68     /**
69      * Module Sync started (and maybe finished) on cm handles (ids).
70      *
71      * @return Map of cm handles (ids) and objects (not used really) for which module sync has started or been completed
72      */
73     @Bean
74     public IMap<String, Object> moduleSyncStartedOnCmHandles() {
75         return createHazelcastInstance("moduleSyncStartedOnCmHandles", moduleSyncStartedConfig)
76             .getMap("moduleSyncStartedOnCmHandles");
77     }
78
79     /**
80      * Data Sync Distributed Map Instance.
81      *
82      * @return configured map of data sync semaphores
83      */
84     @Bean
85     public IMap<String, Boolean> dataSyncSemaphores() {
86         return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig)
87             .getMap("dataSyncSemaphores");
88     }
89
90     private HazelcastInstance createHazelcastInstance(
91         final String hazelcastInstanceName, final NamedConfig namedConfig) {
92         return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig));
93     }
94
95     private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) {
96         final Config config = new Config(instanceName);
97         if (namedConfig instanceof MapConfig) {
98             config.addMapConfig((MapConfig) namedConfig);
99         }
100         if (namedConfig instanceof QueueConfig) {
101             config.addQueueConfig((QueueConfig) namedConfig);
102         }
103         config.setClusterName("synchronization-caches");
104         updateDiscoveryMode(config);
105         return config;
106     }
107
108     private static QueueConfig createQueueConfig() {
109         final QueueConfig commonQueueConfig = new QueueConfig("defaultQueueConfig");
110         commonQueueConfig.setBackupCount(3);
111         commonQueueConfig.setAsyncBackupCount(3);
112         return commonQueueConfig;
113     }
114
115     private static MapConfig createMapConfig(final String configName) {
116         final MapConfig mapConfig = new MapConfig(configName);
117         mapConfig.setBackupCount(3);
118         mapConfig.setAsyncBackupCount(3);
119         return mapConfig;
120     }
121
122     private void updateDiscoveryMode(final Config config) {
123         if (cacheKubernetesEnabled) {
124             log.info("Enabling kubernetes mode with service-name : {}", cacheKubernetesServiceName);
125             config.getNetworkConfig().getJoin().getKubernetesConfig().setEnabled(true)
126                     .setProperty("service-name", cacheKubernetesServiceName);
127         }
128     }
129
130 }