16cb5d6e2686065c96d445604416272d7ac2a88e
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / BluePrintProcessorCluster.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor
18
19 import kotlinx.coroutines.runBlocking
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BluePrintClusterService
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterInfo
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils
25 import org.springframework.boot.context.event.ApplicationReadyEvent
26 import org.springframework.context.event.EventListener
27 import org.springframework.stereotype.Component
28 import java.time.Duration
29 import java.util.Properties
30 import javax.annotation.PreDestroy
31
32 /**
33  * To Start the cluster, minimum 2 Instances/ Replicas od CDS needed.
34  * All instance such as Blueprintprocessor, ResourceResolution, MessagePrioritization should be in
35  * same cluster and should have same cluster name.
36  *
37  * Data can be shared only between the clusters, outside the cluster data can't be shared.
38  * If cds-controller-x instance wants to share data with resource-resolution-x instance, then it should be in the
39  * same cluster.(cds-cluster) and same network (cds-network)
40  *
41  * Assumptions:
42  * 1. Container, Pod and Host names are same.
43  * 2. Container names should end with sequence number.
44  *      Blueprintprocessor example be : cds-controller-1, cds-controller-2, cds-controller-3
45  *      ResourceResolution example be : resource-resolution-1, resource-resolution-2,  resource-resolution-3
46  * 3. Each contained, should have environment properties CLUSTER_ID, CLUSTER_NODE_ID, CLUSTER_JOIN_AS_CLIENT,
47  * CLUSTER_CONFIG_FILE
48  *     Example values :
49  *      CLUSTER_ID: cds-cluster
50  *      CLUSTER_NODE_ID: cds-controller-2
51  *      CLUSTER_JOIN_AS_CLIENT: "true" or "false"
52  *      CLUSTER_CONFIG_FILE:  <Config location>
53  * 4. Cluster will be enabled only all the above properties present in the environments.
54  * if CLUSTER_ENABLED is present, then it will try to create cluster.
55  */
56 @Component
57 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
58
59     private val log = logger(BluePrintProcessorCluster::class)
60
61     @EventListener(ApplicationReadyEvent::class)
62     fun startAndJoinCluster() = runBlocking {
63
64         if (BluePrintConstants.CLUSTER_ENABLED) {
65
66             val clusterId = ClusterUtils.clusterId()
67             val nodeId = ClusterUtils.clusterNodeId()
68
69             val joinAsClient =
70                 (System.getenv(BluePrintConstants.PROPERTY_CLUSTER_JOIN_AS_CLIENT) ?: "false").toBoolean()
71
72             val clusterConfigFile = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
73
74             val properties = Properties()
75             properties["hazelcast.logging.type"] = "slf4j"
76
77             val clusterInfo = ClusterInfo(
78                 id = clusterId, nodeId = nodeId,
79                 joinAsClient = joinAsClient,
80                 configFile = clusterConfigFile,
81                 properties = properties
82             )
83             bluePrintClusterService.startCluster(clusterInfo)
84         } else {
85             log.info("Cluster is disabled, to enable cluster set the environment CLUSTER_* properties.")
86         }
87     }
88
89     @PreDestroy
90     fun shutDown() = runBlocking {
91         bluePrintClusterService.shutDown(Duration.ofSeconds(1))
92     }
93 }