Add metrics for active connections count
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ct / src / test / kotlin / org / onap / dcae / collectors / veshv / tests / fakes / metrics.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.tests.fakes
21
22 import org.onap.dcae.collectors.veshv.boundary.Metrics
23 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
24 import org.onap.dcae.collectors.veshv.model.ClientRejectionCause
25 import org.onap.dcae.collectors.veshv.model.MessageDropCause
26 import org.onap.dcae.collectors.veshv.model.RoutedMessage
27 import java.time.Duration
28 import java.time.Instant
29 import kotlin.test.fail
30
31 /**
32  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
33  * @since June 2018
34  */
35 class FakeMetrics : Metrics {
36
37     var bytesReceived: Int = 0; private set
38     var messageBytesReceived: Int = 0; private set
39     var messagesDroppedCount: Int = 0; private set
40     var lastProcessingTimeMicros: Double = -1.0; private set
41     var messagesSentCount: Int = 0; private set
42     var clientRejectionCause = mutableMapOf<ClientRejectionCause, Int>(); private set
43
44     private val messagesSentToTopic = mutableMapOf<String, Int>()
45     private val messagesDroppedCause = mutableMapOf<MessageDropCause, Int>()
46
47     override fun notifyBytesReceived(size: Int) {
48         bytesReceived += size
49     }
50
51     override fun notifyMessageReceived(msg: WireFrameMessage) {
52         messageBytesReceived += msg.payloadSize
53     }
54
55     override fun notifyMessageSent(msg: RoutedMessage) {
56         messagesSentCount++
57         messagesSentToTopic.compute(msg.topic) { k, _ ->
58             messagesSentToTopic[k]?.inc() ?: 1
59         }
60         lastProcessingTimeMicros = Duration.between(msg.message.wtpFrame.receivedAt, Instant.now()).toNanos() / 1000.0
61     }
62
63     override fun notifyMessageDropped(cause: MessageDropCause) {
64         messagesDroppedCount++
65         messagesDroppedCause.compute(cause) { k, _ -> messagesDroppedCause[k]?.inc() ?: 1 }
66     }
67
68     override fun notifyClientRejected(cause: ClientRejectionCause) {
69         clientRejectionCause.compute(cause) { k, _ -> clientRejectionCause[k]?.inc() ?: 1 }
70     }
71
72     override fun notifyClientDisconnected() {
73     }
74
75     override fun notifyClientConnected() {
76     }
77
78     fun messagesOnTopic(topic: String) =
79             messagesSentToTopic[topic] ?: fail("No messages were sent to topic $topic")
80
81     fun messagesDropped(cause: MessageDropCause) =
82             messagesDroppedCause[cause]
83                     ?: fail("No messages were dropped due to cause: ${cause.name}")
84 }