2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.utils
19 import org.apache.kafka.streams.processor.ProcessorSupplier
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.cluster.optionalClusterService
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterLock
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.db.MessagePrioritization
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.kafka.AbstractMessagePrioritizeProcessor
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.toFormatedCorrelation
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
28 object MessageProcessorUtils {
30 /** Utility to create the cluster lock for message [messagePrioritization] prioritization procssing.*/
31 suspend fun prioritizationGrouplock(messagePrioritization: MessagePrioritization): ClusterLock? {
32 val clusterService = BluePrintDependencyService.optionalClusterService()
34 return if (clusterService != null && clusterService.clusterJoined() &&
35 !messagePrioritization.correlationId.isNullOrBlank()
37 // Get the correlation key in ascending order, even it it is misplaced
38 val correlationId = messagePrioritization.toFormatedCorrelation()
39 val lockName = "prioritize::${messagePrioritization.group}::$correlationId"
40 val clusterLock = clusterService.clusterLock(lockName)
42 if (!clusterLock.isLocked()) throw BluePrintProcessorException("failed to lock($lockName)")
47 /** Utility to create the cluster lock for expiry scheduler*/
48 suspend fun prioritizationExpiryLock(): ClusterLock? {
49 val clusterService = BluePrintDependencyService.optionalClusterService()
50 return if (clusterService != null && clusterService.clusterJoined()) {
51 val lockName = "prioritize-expiry"
52 val clusterLock = clusterService.clusterLock(lockName)
54 if (!clusterLock.isLocked()) throw BluePrintProcessorException("failed to lock($lockName)")
59 /** Utility to create the cluster lock for expiry scheduler*/
60 suspend fun prioritizationCleanLock(): ClusterLock? {
61 val clusterService = BluePrintDependencyService.optionalClusterService()
62 return if (clusterService != null && clusterService.clusterJoined()) {
63 val lockName = "prioritize-clean"
64 val clusterLock = clusterService.clusterLock(lockName)
66 if (!clusterLock.isLocked()) throw BluePrintProcessorException("failed to lock($lockName)")
71 /** Utility used to cluster unlock for message [clusterLock] */
72 suspend fun prioritizationUnLock(clusterLock: ClusterLock?) {
73 if (clusterLock != null) {
79 /** Get the Kafka Supplier for processor lookup [name] **/
80 fun <K, V> bluePrintProcessorSupplier(name: String): ProcessorSupplier<K, V> {
81 return ProcessorSupplier<K, V> {
82 // Dynamically resolve the Prioritization Processor
83 BluePrintDependencyService.instance<AbstractMessagePrioritizeProcessor<K, V>>(name)