Fixing Blueprint Typo's and docs
[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.GlobalScope
20 import kotlinx.coroutines.launch
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BluePrintClusterService
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterInfo
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.logger
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils
26 import org.springframework.boot.context.event.ApplicationReadyEvent
27 import org.springframework.context.event.EventListener
28 import org.springframework.stereotype.Component
29 import java.util.Properties
30
31 /**
32  * To Start the cluster, minimum 2 Instances/ Replicas od CDS needed.
33  * All instance such as Blueprintprocessor, ResourceResolution, MessagePrioritization should be in
34  * same cluster and should have same cluster name.
35  *
36  * Data can be shared only between the clusters, outside the cluster data can't be shared.
37  * If cds-controller-x instance wants to share data with resource-resolution-x instance, then it should be in the
38  * same cluster.(cds-cluster) and same network (cds-network)
39  *
40  * Assumptions:
41  * 1. Container, Pod and Host names are same.
42  * 2. Container names should end with sequence number.
43  *      Blueprintprocessor example be : cds-controller-1, cds-controller-2, cds-controller-3
44  *      ResourceResolution example be : resource-resolution-1, resource-resolution-2,  resource-resolution-3
45  * 3. Each contained, should have environment properties CLUSTER_ID, CLUSTER_NODE_ID, CLUSTER_JOIN_AS_CLIENT,
46  * CLUSTER_CONFIG_FILE
47  *     Example values :
48  *      CLUSTER_ID: cds-cluster
49  *      CLUSTER_NODE_ID: cds-controller-2
50  *      CLUSTER_JOIN_AS_CLIENT: "true" or "false"
51  *      CLUSTER_CONFIG_FILE:  <Config location>
52  * 4. Cluster will be enabled only all the above properties present in the environments.
53  * if CLUSTER_ENABLED is present, then it will try to create cluster.
54  */
55 @Component
56 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
57
58     private val log = logger(BluePrintProcessorCluster::class)
59
60     @EventListener(ApplicationReadyEvent::class)
61     fun startAndJoinCluster() = GlobalScope.launch {
62
63         if (BluePrintConstants.CLUSTER_ENABLED) {
64
65             val clusterId = ClusterUtils.clusterId()
66             val nodeId = ClusterUtils.clusterNodeId()
67
68             val joinAsClient =
69                 (System.getenv(BluePrintConstants.PROPERTY_CLUSTER_JOIN_AS_CLIENT) ?: "false").toBoolean()
70
71             val clusterConfigFile = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
72
73             val properties = Properties()
74             properties["hazelcast.logging.type"] = "slf4j"
75
76             val clusterInfo = ClusterInfo(
77                 id = clusterId, nodeId = nodeId,
78                 joinAsClient = joinAsClient,
79                 configFile = clusterConfigFile,
80                 properties = properties
81             )
82             bluePrintClusterService.startCluster(clusterInfo)
83         } else {
84             log.info("Cluster is disabled, to enable cluster set the environment CLUSTER_* properties.")
85         }
86     }
87 }