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.service
19 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationService
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationStateService
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessageState
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.db.MessagePrioritization
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.ids
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.kafka.AbstractKafkaMessagePrioritizationService
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.nats.AbstractNatsMessagePrioritizationService
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.orderByHighestPriority
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
29 /** Sample Prioritization Service, Define spring service injector to register in application*/
30 open class SampleMessagePrioritizationService(private val messagePrioritizationStateService: MessagePrioritizationStateService) :
31 AbstractMessagePrioritizationService(messagePrioritizationStateService) {
33 /** Child overriding this implementation , if necessary */
34 override suspend fun handleAggregation(messages: List<MessagePrioritization>) {
35 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
36 this, messagePrioritizationStateService
38 sampleMessagePrioritizationHandler.handleAggregation(messages)
41 /** If consumer wants specific correlation with respect to group and types, then populate the specific types,
42 * otherwise correlation happens with group and correlationId */
43 override fun getGroupCorrelationTypes(messagePrioritization: MessagePrioritization): List<String>? {
44 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
45 this, messagePrioritizationStateService
47 return sampleMessagePrioritizationHandler.getGroupCorrelationTypes(messagePrioritization)
51 open class SampleKafkaMessagePrioritizationService(private val messagePrioritizationStateService: MessagePrioritizationStateService) :
52 AbstractKafkaMessagePrioritizationService(messagePrioritizationStateService) {
54 /** Child overriding this implementation , if necessary */
55 override suspend fun handleAggregation(messages: List<MessagePrioritization>) {
56 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
57 this, messagePrioritizationStateService
59 sampleMessagePrioritizationHandler.handleAggregation(messages)
62 /** If consumer wants specific correlation with respect to group and types, then populate the specific types,
63 * otherwise correlation happens with group and correlationId */
64 override fun getGroupCorrelationTypes(messagePrioritization: MessagePrioritization): List<String>? {
65 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
66 this, messagePrioritizationStateService
68 return sampleMessagePrioritizationHandler.getGroupCorrelationTypes(messagePrioritization)
72 open class SampleNatsMessagePrioritizationService(private val messagePrioritizationStateService: MessagePrioritizationStateService) :
73 AbstractNatsMessagePrioritizationService(messagePrioritizationStateService) {
75 /** Child overriding this implementation , if necessary */
76 override suspend fun handleAggregation(messages: List<MessagePrioritization>) {
77 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
78 this, messagePrioritizationStateService
80 sampleMessagePrioritizationHandler.handleAggregation(messages)
83 /** If consumer wants specific correlation with respect to group and types, then populate the specific types,
84 * otherwise correlation happens with group and correlationId */
85 override fun getGroupCorrelationTypes(messagePrioritization: MessagePrioritization): List<String>? {
86 val sampleMessagePrioritizationHandler = SampleMessagePrioritizationHandler(
87 this, messagePrioritizationStateService
89 return sampleMessagePrioritizationHandler.getGroupCorrelationTypes(messagePrioritization)
93 class SampleMessagePrioritizationHandler(
94 private val messagePrioritizationService: MessagePrioritizationService,
95 private val messagePrioritizationStateService: MessagePrioritizationStateService
98 private val log = logger(SampleMessagePrioritizationHandler::class)
100 suspend fun handleAggregation(messages: List<MessagePrioritization>) {
101 log.info("messages(${messages.ids()}) aggregated")
102 /** Sequence based on Priority and Updated Date */
103 val sequencedMessage = messages.orderByHighestPriority()
104 /** Update all messages to aggregated state */
105 messagePrioritizationStateService.setMessagesState(
106 sequencedMessage.ids(),
107 MessageState.AGGREGATED.name
109 messagePrioritizationService.output(sequencedMessage)
112 fun getGroupCorrelationTypes(messagePrioritization: MessagePrioritization): List<String>? {
113 return when (messagePrioritization.group) {
114 /** Dummy Implementation, This can also be read from file and stored as cached map **/
115 "group-typed" -> arrayListOf("type-0", "type-1", "type-2")
116 "pass-typed" -> arrayListOf(messagePrioritization.type)