VESEvent payload generation introduction 97/58597/1
authorkjaniak <kornel.janiak@nokia.com>
Thu, 14 Jun 2018 11:10:53 +0000 (13:10 +0200)
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Thu, 2 Aug 2018 07:19:48 +0000 (09:19 +0200)
Change-Id: Ida15b9739d26b5db2f69a5296876f20ea83de761
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Issue-ID: DCAEGEN2-601

hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt
hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt
hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt [new file with mode: 0644]
hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt
hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt [new file with mode: 0644]

index bc1cff7..bc7db86 100644 (file)
@@ -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)
index 6011760..f731e11 100644 (file)
@@ -32,8 +32,7 @@ import javax.json.JsonObject
  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
  * @since June 2018
  */
-object MessageFactory {
-
+class MessageFactory(private val payloadGenerator: PayloadGenerator) {
 
     fun createMessageFlux(messageParameters: MessageParameters): Flux<WireFrame> =
             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 (file)
index 0000000..17dbbf4
--- /dev/null
@@ -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
+    }
+
+}
index ee1d1cf..2f59264 100644 (file)
@@ -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 (file)
index 0000000..73129a7
--- /dev/null
@@ -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)
+            }
+
+        }
+    }
+})