Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / service / BluePrintClusterService.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.core.service
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.BlueprintClusterTopic
20 import org.springframework.context.ApplicationEvent
21 import java.time.Duration
22 import java.util.Properties
23 import java.util.UUID
24
25 interface BluePrintClusterService {
26
27     /** Start the cluster with [clusterInfo], By default clustering service is disabled.
28      * Application module has to start cluster */
29     suspend fun <T> startCluster(configuration: T)
30
31     fun clusterJoined(): Boolean
32
33     fun isClient(): Boolean
34
35     fun isLiteMember(): Boolean
36
37     /** Returns [partitionGroup] master member */
38     suspend fun masterMember(partitionGroup: String): ClusterMember
39
40     /** Returns all the data cluster members */
41     suspend fun allMembers(): Set<ClusterMember>
42
43     /**
44      * Returns application cluster members for [appName] joined as server or lite member,
45      * Node joined as client won't be visible. Here the assumption is node-id is combination of
46      * application id and replica number, for an example Application cds-cluster then the node ids will be
47      * cds-cluster-1, cds-cluster-2, cds-cluster-3
48      */
49     suspend fun applicationMembers(appName: String): Set<ClusterMember>
50
51     /** Create and get or get the distributed data map store with [name] */
52     suspend fun <T> clusterMapStore(name: String): MutableMap<String, T>
53
54     /** Create and get the distributed lock with [name] */
55     suspend fun clusterLock(name: String): ClusterLock
56
57     /** Shut down the cluster with [duration] */
58     suspend fun shutDown(duration: Duration)
59
60     /** Send [message] to the listener(s) of a [topic] */
61     suspend fun <T> sendMessage(topic: BlueprintClusterTopic, message: T)
62
63     /** Register a [listener] to a [topic] and returns his UUID */
64     fun <T> addBlueprintClusterMessageListener(topic: BlueprintClusterTopic, listener: BlueprintClusterMessageListener<T>): UUID
65
66     /** Unregister a listener from a [topic] using his [uuid] and returns true if it succeeded */
67     fun removeBlueprintClusterMessageListener(topic: BlueprintClusterTopic, uuid: UUID): Boolean
68 }
69
70 data class ClusterInfo(
71     val id: String,
72     val nodeId: String,
73     var joinAsClient: Boolean = false,
74     var properties: Properties?,
75     var configFile: String
76 )
77
78 data class ClusterMember(
79     val id: String,
80     val name: String,
81     val memberAddress: String?,
82     val state: String? = null
83 )
84
85 interface ClusterLock {
86
87     fun name(): String
88     suspend fun lock()
89     suspend fun fenceLock(): String
90     suspend fun tryLock(timeout: Long): Boolean
91     suspend fun tryFenceLock(timeout: Long): String
92     suspend fun unLock()
93     fun isLocked(): Boolean
94     fun isLockedByCurrentThread(): Boolean
95     fun close()
96 }
97
98 class BluePrintClusterMessage<E>(val topic: BlueprintClusterTopic, val payload: E, publishTime: Long, clusterMember: ClusterMember)
99
100 interface BlueprintClusterMessageListener<E> {
101     fun onMessage(message: BluePrintClusterMessage<E>?)
102 }
103
104 class ClusterJoinedEvent(source: Any) : ApplicationEvent(source)
105
106 const val CDS_LOCK_GROUP = "cds-lock"