From e7987b7a660060746d5f49e1ec90b1ff90fcf55a Mon Sep 17 00:00:00 2001 From: kjaniak Date: Thu, 14 Jun 2018 13:10:53 +0200 Subject: VESEvent payload generation introduction Change-Id: Ida15b9739d26b5db2f69a5296876f20ea83de761 Signed-off-by: kjaniak Issue-ID: DCAEGEN2-601 --- .../veshv/simulators/xnf/impl/HttpServer.kt | 4 +- .../veshv/simulators/xnf/impl/MessageFactory.kt | 9 ++- .../veshv/simulators/xnf/impl/PayloadGenerator.kt | 62 ++++++++++++++++++ .../simulators/xnf/impl/MessageFactoryTest.kt | 2 +- .../simulators/xnf/impl/PayloadGeneratorTest.kt | 74 ++++++++++++++++++++++ 5 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt create mode 100644 hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt index bc1cff7c..bc7db869 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt @@ -47,7 +47,7 @@ class HttpServer(private val vesClient: VesHvClient) { ctx.request.body .map { Json.createReader(it.inputStream).readObject() } .map { extractMessageParameters(it) } - .map { MessageFactory.createMessageFlux(it) } + .map { MessageFactory.INSTANCE.createMessageFlux(it) } .onError { handleException(it, ctx) } .then { vesClient.send(it) @@ -75,7 +75,7 @@ class HttpServer(private val vesClient: VesHvClient) { private fun extractMessageParameters(request: JsonObject): MessageParameters = try { - val commonEventHeader = MessageFactory + val commonEventHeader = MessageFactory.INSTANCE .parseCommonHeader(request.getJsonObject("commonEventHeader")) val messagesAmount = request.getJsonNumber("messagesAmount").longValue() MessageParameters(commonEventHeader, messagesAmount) diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt index 60117603..f731e11c 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt @@ -32,8 +32,7 @@ import javax.json.JsonObject * @author Jakub Dudycz * @since June 2018 */ -object MessageFactory { - +class MessageFactory(private val payloadGenerator: PayloadGenerator) { fun createMessageFlux(messageParameters: MessageParameters): Flux = Mono.fromCallable { createMessage(messageParameters.commonEventHeader) }.let { @@ -69,9 +68,13 @@ object MessageFactory { private fun vesMessageBytes(commonHeader: CommonEventHeader): ByteArray { val msg = VesEvent.newBuilder() .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data")) + .setHvRanMeasFields(PayloadGenerator().generatePayload().toByteString()) .build() return msg.toByteArray() } + + companion object { + val INSTANCE = MessageFactory(PayloadGenerator()) + } } diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt new file mode 100644 index 00000000..17dbbf41 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt @@ -0,0 +1,62 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.simulators.xnf.impl + +import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload +import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload.PMObject +import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload.PMObject.HVRanMeas +import java.util.Random + +class PayloadGenerator { + + private val randomGenerator = Random() + + fun generatePayload(numOfCountPerMeas: Long = 2, numOfMeasPerObject: Int = 2): HVRanMeasPayload { + val pmObject = generatePmObject(numOfCountPerMeas, numOfMeasPerObject) + return HVRanMeasPayload.newBuilder() + .addPmObject(pmObject) + .build() + } + + private fun generatePmObject(numOfCountPerMeas: Long, numOfMeasPerObject: Int): PMObject { + val hvRanMeasList = MutableList(numOfMeasPerObject) { generateHvRanMeas(numOfCountPerMeas) } + val finalUriName = URI_BASE_NAME + randomGenerator.nextInt(UPPER_URI_NUMBER_BOUND) + return HVRanMeasPayload.PMObject.newBuilder() + .setUri(finalUriName) + .addAllHvRanMeas(hvRanMeasList.asIterable()) + .build() + } + + private fun generateHvRanMeas(numOfCountPerMeas: Long): HVRanMeas { + return HVRanMeasPayload.PMObject.HVRanMeas.newBuilder() + .setMeasurementId(randomGenerator.nextInt()) + .addAllCounterSubid(Iterable { randomGenerator.ints(numOfCountPerMeas).iterator() }) + .addAllCounterValue(Iterable { randomGenerator.longs(numOfCountPerMeas).iterator() }) + .setSuspectFlagIncomplete(false) + .setSuspectFlagOutOfSync(false) + .build() + } + + companion object { + private const val URI_BASE_NAME = "sample/uri" + private const val UPPER_URI_NUMBER_BOUND = 10_000 + } + +} diff --git a/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt index ee1d1cf2..2f592641 100644 --- a/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt +++ b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt @@ -40,7 +40,7 @@ const val SAMPLE_LAST_EPOCH: Long = 120034455 object MessageFactoryTest : Spek({ describe("message factory") { - val factory = MessageFactory + val factory = MessageFactory.INSTANCE given("only common header") { it("should return infinite flux") { diff --git a/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt new file mode 100644 index 00000000..73129a7f --- /dev/null +++ b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.simulators.xnf.impl + +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.given +import org.jetbrains.spek.api.dsl.it +import org.assertj.core.api.Assertions.assertThat +import org.jetbrains.spek.api.dsl.on + +private const val DEFAULT_MEASUREMENTS_NUMBER = 2 +private const val DEFAULT_COUNTERS_NUMBER = 2 + +private val uriRegex = """sample/uri(\d+)""".toRegex() + +object PayloadGeneratorTest : Spek({ + + given("payload factory object") { + val payloadGenerator = PayloadGenerator() + + on("two generated payloads") { + val generatedPayload0 = payloadGenerator.generatePayload() + val generatedPayload1 = payloadGenerator.generatePayload() + it("URIs should have different names") { + val matchResult0 = uriRegex.find(generatedPayload0.getPmObject(0).uri)!!.value + val matchResult1 = uriRegex.find(generatedPayload1.getPmObject(0).uri)!!.value + assertThat(matchResult0 != matchResult1).isTrue() + } + } + + on("call with default parameters") { + val generatedPayload = payloadGenerator.generatePayload() + it("should contain default numbers of measurements") { + assertThat(generatedPayload.getPmObject(0).hvRanMeasCount).isEqualTo(DEFAULT_MEASUREMENTS_NUMBER) + } + it("should contain default numbers of counters in measurement") { + assertThat(generatedPayload.getPmObject(0).getHvRanMeas(0).counterSubidCount).isEqualTo(DEFAULT_COUNTERS_NUMBER) + } + } + + on("call with specified parameters") { + val numOfCountPerMeas: Long = 5 + val numOfMeasPerObject: Int = 10 + val generatedPayload = payloadGenerator.generatePayload(numOfCountPerMeas, numOfMeasPerObject) + it("should contain specified number of measurements") { + assertThat(generatedPayload.getPmObject(0).hvRanMeasCount).isEqualTo(numOfMeasPerObject) + } + it("measurement should contain specified number of counters") { + assertThat(generatedPayload.getPmObject(0).hvRanMeasList + .filter { numOfCountPerMeas.toInt() == it.counterSubidCount } + .size) + .isEqualTo(numOfMeasPerObject) + } + + } + } +}) -- cgit 1.2.3-korg