b78ebf68bbd63d0b2f4fea8d6c56baa44c77247e
[ccsdk/cds.git] /
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.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.logger
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 javax.annotation.PreDestroy
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_NODE_ADDRESS,
46  * CLUSTER_MEMBERS, CLUSTER_STORAGE_PATH
47  *     Example values :
48  *      CLUSTER_ID: cds-cluster
49  *      CLUSTER_NODE_ID: cds-controller-2
50  *      CLUSTER_NODE_ADDRESS: cds-controller-2
51  *      CLUSTER_MEMBERS: cds-controller-1,cds-controller-2,cds-controller-3,resource-resolution-1,resource-resolution-2,resource-resolution-3
52  *      CLUSTER_STORAGE_PATH: /opt/app/onap/config/cluster
53  *      CLUSTER_CONFIG_FILE:  /opt/app/onap/config/atomix/atomix-multicast.conf
54  * 4. Cluster will be enabled only all the above properties present in the environments.
55  * if CLUSTER_ID is present, then it will try to create cluster.
56  */
57 @Component
58 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
59
60     private val log = logger(BluePrintProcessorCluster::class)
61
62     @EventListener(ApplicationReadyEvent::class)
63     fun startAndJoinCluster() = runBlocking {
64         val clusterId = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_ID)
65
66         if (!clusterId.isNullOrEmpty()) {
67
68             val nodeId = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_NODE_ID)
69                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_NODE_ID}")
70
71             val nodeAddress = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_NODE_ADDRESS)
72                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_NODE_ADDRESS}")
73
74             val clusterMembers = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_MEMBERS)
75                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_MEMBERS}")
76
77             val clusterMemberList = clusterMembers.split(",").map { it.trim() }.toList()
78
79             val clusterStorage = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_STORAGE_PATH)
80                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_STORAGE_PATH}")
81
82             val clusterConfigFile = System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
83
84             val clusterInfo = ClusterInfo(
85                 id = clusterId, nodeId = nodeId,
86                 clusterMembers = clusterMemberList, nodeAddress = nodeAddress,
87                 storagePath = clusterStorage,
88                 configFile = clusterConfigFile
89             )
90             bluePrintClusterService.startCluster(clusterInfo)
91         } else {
92             log.info(
93                 "Cluster is disabled, to enable cluster set the environment " +
94                     "properties[CLUSTER_ID,CLUSTER_NODE_ID, CLUSTER_NODE_ADDRESS, CLUSTER_MEMBERS,CLUSTER_CONFIG_FILE]"
95             )
96         }
97     }
98
99     @PreDestroy
100     fun shutDown() = runBlocking {
101         bluePrintClusterService.shutDown(Duration.ofSeconds(1))
102     }
103 }