Add message prioritization module
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / message-prioritizaion / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / message / prioritization / topology / MessagePrioritizationPunctuators.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.functions.message.prioritization.topology
18
19 import org.apache.kafka.streams.processor.To
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationConstants
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessageState
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.PrioritizationConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.service.MessagePrioritizationStateService
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.kafka.AbstractBluePrintMessagePunctuator
25 import org.onap.ccsdk.cds.controllerblueprints.core.logger
26
27
28 class MessagePriorityExpiryPunctuator(private val messagePrioritizationStateService: MessagePrioritizationStateService)
29     : AbstractBluePrintMessagePunctuator() {
30
31     private val log = logger(MessagePriorityExpiryPunctuator::class)
32     lateinit var configuration: PrioritizationConfiguration
33
34     override suspend fun punctuateNB(timestamp: Long) {
35
36         log.info("**** executing expiry punctuator applicationId(${processorContext.applicationId()}), " +
37                 "taskId(${processorContext.taskId()})")
38         val expiryConfiguration = configuration.expiryConfiguration
39         val fetchMessages = messagePrioritizationStateService
40                 .getExpiryEligibleMessages(expiryConfiguration.maxPollRecord)
41
42         val expiredIds = fetchMessages?.map { it.id }
43         if (expiredIds != null && expiredIds.isNotEmpty()) {
44             messagePrioritizationStateService.updateMessagesState(expiredIds, MessageState.EXPIRED.name)
45             fetchMessages.forEach { expired ->
46                 processorContext.forward(expired.id, expired,
47                         To.child(MessagePrioritizationConstants.SINK_EXPIRED))
48             }
49         }
50     }
51 }
52
53 class MessagePriorityCleanPunctuator(private val messagePrioritizationStateService: MessagePrioritizationStateService)
54     : AbstractBluePrintMessagePunctuator() {
55
56     private val log = logger(MessagePriorityCleanPunctuator::class)
57     lateinit var configuration: PrioritizationConfiguration
58
59     override suspend fun punctuateNB(timestamp: Long) {
60         log.info("**** executing clean punctuator applicationId(${processorContext.applicationId()}), " +
61                 "taskId(${processorContext.taskId()})")
62         //TODO
63     }
64 }