Prioritization expiry and clean scheduler service
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / message-prioritizaion / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / message / prioritization / utils / MessagePrioritizationSample.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.utils
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.CleanConfiguration
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.ExpiryConfiguration
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.KafkaConfiguration
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.PrioritizationConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.ShutDownConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.db.MessagePrioritization
25 import java.util.Calendar
26 import java.util.Date
27 import java.util.UUID
28
29 object MessagePrioritizationSample {
30
31     fun samplePrioritizationConfiguration(): PrioritizationConfiguration {
32         return PrioritizationConfiguration().apply {
33             kafkaConfiguration = KafkaConfiguration().apply {
34                 inputTopicSelector = "prioritize-input"
35                 outputTopic = "prioritize-output-topic"
36                 expiredTopic = "prioritize-expired-topic"
37             }
38             expiryConfiguration = ExpiryConfiguration().apply {
39                 frequencyMilli = 10000L
40                 maxPollRecord = 2000
41             }
42             shutDownConfiguration = ShutDownConfiguration().apply {
43                 waitMill = 2000L
44             }
45             cleanConfiguration = CleanConfiguration().apply {
46                 frequencyMilli = 10000L
47                 expiredRecordsHoldDays = 5
48             }
49         }
50     }
51
52     fun sampleSchedulerPrioritizationConfiguration(): PrioritizationConfiguration {
53         return PrioritizationConfiguration().apply {
54             expiryConfiguration = ExpiryConfiguration().apply {
55                 frequencyMilli = 10L
56                 maxPollRecord = 2000
57             }
58             shutDownConfiguration = ShutDownConfiguration().apply {
59                 waitMill = 20L
60             }
61             cleanConfiguration = CleanConfiguration().apply {
62                 frequencyMilli = 10L
63                 expiredRecordsHoldDays = 5
64             }
65         }
66     }
67
68     private fun currentDatePlusDays(days: Int): Date {
69         val calender = Calendar.getInstance()
70         calender.add(Calendar.DATE, days)
71         return calender.time
72     }
73
74     fun sampleMessages(messageState: String, count: Int): List<MessagePrioritization> {
75         return sampleMessages("sample-group", messageState, count)
76     }
77
78     fun sampleMessages(groupName: String, messageState: String, count: Int): List<MessagePrioritization> {
79         val messages: MutableList<MessagePrioritization> = arrayListOf()
80         repeat(count) {
81             val backPressureMessage = createMessage(
82                 groupName, messageState,
83                 "sample-type", null
84             )
85             messages.add(backPressureMessage)
86         }
87         return messages
88     }
89
90     fun sampleMessageWithSameCorrelation(
91         groupName: String,
92         messageState: String,
93         count: Int
94     ): List<MessagePrioritization> {
95         val messages: MutableList<MessagePrioritization> = arrayListOf()
96         repeat(count) {
97             val backPressureMessage = createMessage(
98                 groupName, messageState, "sample-type",
99                 "key1=value1,key2=value2"
100             )
101             messages.add(backPressureMessage)
102         }
103         return messages
104     }
105
106     fun sampleMessageWithDifferentTypeSameCorrelation(
107         groupName: String,
108         messageState: String,
109         count: Int
110     ): List<MessagePrioritization> {
111         val messages: MutableList<MessagePrioritization> = arrayListOf()
112         repeat(count) {
113             val backPressureMessage = createMessage(
114                 groupName, messageState, "type-$it",
115                 "key1=value1,key2=value2"
116             )
117             messages.add(backPressureMessage)
118         }
119         return messages
120     }
121
122     fun createMessage(
123         groupName: String,
124         messageState: String,
125         messageType: String,
126         messageCorrelationId: String?
127     ): MessagePrioritization {
128
129         return MessagePrioritization().apply {
130             id = UUID.randomUUID().toString()
131             group = groupName
132             type = messageType
133             state = messageState
134             priority = (1..10).shuffled().first()
135             correlationId = messageCorrelationId
136             message = "I am the Message"
137             createdDate = Date()
138             updatedDate = Date()
139             expiryDate = currentDatePlusDays(3)
140         }
141     }
142 }