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