Add metric for total latency without routing
[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-2020 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.domain.RoutedMessage
27 import org.onap.dcae.collectors.veshv.domain.VesMessage
28 import org.onap.dcae.collectors.veshv.utils.TimeUtils
29 import java.time.Duration
30 import java.time.Instant
31 import kotlin.test.fail
32
33 /**
34  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
35  * @since June 2018
36  */
37 class FakeMetrics : Metrics {
38
39     var bytesReceived: Int = 0; private set
40     var messageBytesReceived: Int = 0; private set
41     var messagesDroppedCount: Int = 0; private set
42     var lastProcessingTimeMicros: Double = -1.0; private set
43     var lastProcessingTimeWithoutRoutingMicros: Double = -1.0; private set
44     var lastToCollectorTravelTime: Double = -1.0; private set
45     var lastTotalLatencyWithoutRouting: Double = -1.0; private set
46     var messagesSentCount: Int = 0; private set
47     var clientRejectionCause = mutableMapOf<ClientRejectionCause, Int>(); private set
48
49     private val messagesSentToTopic = mutableMapOf<String, Int>()
50     private val messagesDroppedCause = mutableMapOf<MessageDropCause, Int>()
51
52     override fun notifyBytesReceived(size: Int) {
53         bytesReceived += size
54     }
55
56     override fun notifyMessageReceived(msg: WireFrameMessage) {
57         messageBytesReceived += msg.payloadSize
58     }
59
60     override fun notifyMessageReceived(msg: VesMessage) {
61         lastToCollectorTravelTime = Duration.between(TimeUtils.epochMicroToInstant(msg.header.lastEpochMicrosec),
62                 Instant.now()).toNanos() / 1000.0
63     }
64
65     override fun notifyMessageReadyForRouting(msg: VesMessage) {
66         lastProcessingTimeWithoutRoutingMicros = Duration.between(msg.wtpFrame.receivedAt, Instant.now()).toNanos() / 1000.0
67         lastTotalLatencyWithoutRouting = Duration.between(TimeUtils.epochMicroToInstant(msg.header.lastEpochMicrosec), Instant.now()).toNanos() / 1000.0
68     }
69
70     override fun notifyMessageSent(msg: RoutedMessage) {
71         messagesSentCount++
72         messagesSentToTopic.compute(msg.targetTopic) { k, _ ->
73             messagesSentToTopic[k]?.inc() ?: 1
74         }
75         lastProcessingTimeMicros = Duration.between(msg.message.wtpFrame.receivedAt, Instant.now()).toNanos() / 1000.0
76     }
77
78     override fun notifyMessageDropped(cause: MessageDropCause) {
79         messagesDroppedCount++
80         messagesDroppedCause.compute(cause) { k, _ -> messagesDroppedCause[k]?.inc() ?: 1 }
81     }
82
83     override fun notifyClientRejected(cause: ClientRejectionCause) {
84         clientRejectionCause.compute(cause) { k, _ -> clientRejectionCause[k]?.inc() ?: 1 }
85     }
86
87     override fun notifyClientDisconnected() {
88     }
89
90     override fun notifyClientConnected() {
91     }
92
93     fun messagesOnTopic(topic: String) =
94             messagesSentToTopic[topic] ?: fail("No messages were sent to topic $topic")
95
96     fun messagesDropped(cause: MessageDropCause) =
97             messagesDroppedCause[cause]
98                     ?: fail("No messages were dropped due to cause: ${cause.name}")
99 }