c85ce035d9a1ee3e0ee0c96adece33f0ed93fc2a
[dcaegen2/collectors/hv-ves.git] /
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.ves.message.generator.impl
21
22 import com.google.protobuf.ByteString
23 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload
24 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload.PMObject
25 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload.PMObject.HVRanMeas
26 import java.util.*
27
28 internal class PayloadGenerator {
29
30     private val randomGenerator = Random()
31
32     fun generateRawPayload(size: Int): ByteString =
33             ByteString.copyFrom(ByteArray(size))
34
35     fun generatePayload(numOfCountPerMeas: Long = 2, numOfMeasPerObject: Int = 2): HVRanMeasPayload {
36         val pmObject = generatePmObject(numOfCountPerMeas, numOfMeasPerObject)
37         return HVRanMeasPayload.newBuilder()
38                 .addPmObject(pmObject)
39                 .build()
40     }
41
42     private fun generatePmObject(numOfCountPerMeas: Long, numOfMeasPerObject: Int): PMObject {
43         val hvRanMeasList = MutableList(numOfMeasPerObject) { generateHvRanMeas(numOfCountPerMeas) }
44         val finalUriName = URI_BASE_NAME + randomGenerator.nextInt(UPPER_URI_NUMBER_BOUND)
45         return HVRanMeasPayload.PMObject.newBuilder()
46                 .setUri(finalUriName)
47                 .addAllHvRanMeas(hvRanMeasList.asIterable())
48                 .build()
49     }
50
51     private fun generateHvRanMeas(numOfCountPerMeas: Long): HVRanMeas {
52         return HVRanMeasPayload.PMObject.HVRanMeas.newBuilder()
53                 .setMeasurementId(randomGenerator.nextInt())
54                 .addAllCounterSubid(Iterable { randomGenerator.ints(numOfCountPerMeas).iterator() })
55                 .addAllCounterValue(Iterable { randomGenerator.longs(numOfCountPerMeas).iterator() })
56                 .setSuspectFlagIncomplete(false)
57                 .setSuspectFlagOutOfSync(false)
58                 .build()
59     }
60
61     companion object {
62         private const val URI_BASE_NAME = "sample/uri"
63         private const val UPPER_URI_NUMBER_BOUND = 10_000
64     }
65
66 }