diff options
author | kjaniak <kornel.janiak@nokia.com> | 2018-07-12 10:57:58 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-02 14:47:46 +0200 |
commit | 3bdd41724d031c5d787239ce1b4186ac7680d03c (patch) | |
tree | 1276e35c4402b9c0d417a7bd7588f180ea688716 /hv-collector-xnf-simulator | |
parent | ece457cd2470f65275b695991bf0bee1e144c24d (diff) |
Extract message generator module
Closes ONAP-501
Change-Id: Ia5dc5f67e18c302abbeac7e0373cef479f4befb8
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-xnf-simulator')
9 files changed, 9 insertions, 391 deletions
diff --git a/hv-collector-xnf-simulator/pom.xml b/hv-collector-xnf-simulator/pom.xml index 9af2246d..2a02fae1 100644 --- a/hv-collector-xnf-simulator/pom.xml +++ b/hv-collector-xnf-simulator/pom.xml @@ -94,6 +94,11 @@ <version>${project.parent.version}</version> </dependency> <dependency> + <groupId>${project.parent.groupId}</groupId> + <artifactId>hv-collector-ves-message-generator</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> <groupId>io.arrow-kt</groupId> <artifactId>arrow-effects</artifactId> </dependency> diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/api/MessageGenerator.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/api/MessageGenerator.kt deleted file mode 100644 index a6d6af84..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/api/MessageGenerator.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * ============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.api - -import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage -import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters -import reactor.core.publisher.Flux - -/** - * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> - * @since June 2018 - */ -interface MessageGenerator { - fun createMessageFlux(messageParameters: MessageParameters): Flux<PayloadWireFrameMessage> -} diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/MessageParameters.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/MessageParameters.kt deleted file mode 100644 index f993f45a..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/MessageParameters.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * ============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.config - -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader - -/** - * @author Jakub Dudycz <jakub.dudycz@nokia.com> - * @since June 2018 - */ -data class MessageParameters(val commonEventHeader: CommonEventHeader, val amount: Long = -1) diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/factory.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/factory.kt deleted file mode 100644 index dce386b1..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/factory.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * ============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 - -import org.onap.dcae.collectors.veshv.simulators.xnf.api.MessageGenerator -import org.onap.dcae.collectors.veshv.simulators.xnf.impl.MessageGeneratorImpl -import org.onap.dcae.collectors.veshv.simulators.xnf.impl.PayloadGenerator - -/** - * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> - * @since June 2018 - */ -fun createMessageGenerator(): MessageGenerator = MessageGeneratorImpl(PayloadGenerator()) diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt index 6346b648..08a35d42 100644 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt @@ -21,8 +21,9 @@ package org.onap.dcae.collectors.veshv.simulators.xnf.impl import arrow.effects.IO import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage -import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters +import org.onap.dcae.collectors.veshv.ves.message.generator.config.MessageParameters import org.onap.dcae.collectors.veshv.utils.logging.Logger +import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator import ratpack.exec.Promise import ratpack.handling.Chain import ratpack.handling.Context @@ -69,7 +70,7 @@ internal class HttpServer(private val vesClient: XnfSimulator) { return ctx.request.body .map { Json.createReader(it.inputStream).readObject() } .map { extractMessageParameters(it) } - .map { MessageGeneratorImpl.INSTANCE.createMessageFlux(it) } + .map { MessageGenerator.INSTANCE.createMessageFlux(it) } } private fun sendAcceptedResponse(ctx: Context) { @@ -95,7 +96,7 @@ internal class HttpServer(private val vesClient: XnfSimulator) { private fun extractMessageParameters(request: JsonObject): MessageParameters = try { - val commonEventHeader = MessageGeneratorImpl.INSTANCE + val commonEventHeader = MessageGenerator.INSTANCE .parseCommonHeader(request.getJsonObject("commonEventHeader")) val messagesAmount = request.getJsonNumber("messagesAmount").longValue() MessageParameters(commonEventHeader, messagesAmount) diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageGeneratorImpl.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageGeneratorImpl.kt deleted file mode 100644 index baff967a..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageGeneratorImpl.kt +++ /dev/null @@ -1,79 +0,0 @@ -/* - * ============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 com.google.protobuf.ByteString -import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage -import org.onap.dcae.collectors.veshv.simulators.xnf.api.MessageGenerator -import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters -import org.onap.ves.VesEventV5.VesEvent -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader -import reactor.core.publisher.Flux -import reactor.core.publisher.Mono -import javax.json.JsonObject - -/** - * @author Jakub Dudycz <jakub.dudycz@nokia.com> - * @since June 2018 - */ -internal class MessageGeneratorImpl(private val payloadGenerator: PayloadGenerator) : MessageGenerator { - - override fun createMessageFlux(messageParameters: MessageParameters): Flux<PayloadWireFrameMessage> = - Mono.fromCallable { createMessage(messageParameters.commonEventHeader) }.let { - if (messageParameters.amount < 0) - it.repeat() - else - it.repeat(messageParameters.amount) - } - - fun parseCommonHeader(json: JsonObject): CommonEventHeader = CommonEventHeader.newBuilder() - .setVersion(json.getString("version")) - .setDomain(CommonEventHeader.Domain.forNumber(json.getInt("domain"))) - .setSequence(json.getInt("sequence")) - .setPriority(CommonEventHeader.Priority.forNumber(json.getInt("priority"))) - .setEventId(json.getString("eventId")) - .setEventName(json.getString("eventName")) - .setEventType(json.getString("eventType")) - .setStartEpochMicrosec(json.getJsonNumber("startEpochMicrosec").longValue()) - .setLastEpochMicrosec(json.getJsonNumber("lastEpochMicrosec").longValue()) - .setNfNamingCode(json.getString("nfNamingCode")) - .setNfcNamingCode(json.getString("nfcNamingCode")) - .setReportingEntityId(json.getString("reportingEntityId")) - .setReportingEntityName(ByteString.copyFromUtf8(json.getString("reportingEntityName"))) - .setSourceId(ByteString.copyFromUtf8(json.getString("sourceId"))) - .setSourceName(json.getString("sourceName")) - .build() - - - private fun createMessage(commonHeader: CommonEventHeader): PayloadWireFrameMessage = - PayloadWireFrameMessage(vesMessageBytes(commonHeader)) - - - private fun vesMessageBytes(commonHeader: CommonEventHeader): ByteArray = - VesEvent.newBuilder() - .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(payloadGenerator.generatePayload().toByteString()) - .build() - .toByteArray() - - companion object { - val INSTANCE = MessageGeneratorImpl(PayloadGenerator()) - } -} diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt deleted file mode 100644 index c8b97639..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGenerator.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * ============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.* - -internal 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-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt deleted file mode 100644 index 6f8a95a4..00000000 --- a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ============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 com.google.protobuf.ByteString -import org.jetbrains.spek.api.Spek -import org.jetbrains.spek.api.dsl.describe -import org.jetbrains.spek.api.dsl.given -import org.jetbrains.spek.api.dsl.it -import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters -import org.onap.ves.VesEventV5 -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain.HVRANMEAS -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Priority.MEDIUM -import reactor.test.test - -const val SAMPLE_START_EPOCH: Long = 120034455 -const val SAMPLE_LAST_EPOCH: Long = 120034455 - -/** - * @author Jakub Dudycz <jakub.dudycz@nokia.com> - * @since June 2018 - */ -object MessageFactoryTest : Spek({ - describe("message factory") { - - val factory = MessageGeneratorImpl.INSTANCE - - given("only common header") { - it("should return infinite flux") { - val limit = 1000L - factory.createMessageFlux(getSampleMessageParameters()).take(limit).test() - .expectNextCount(limit) - .verifyComplete() - } - } - given("common header and messages amount") { - it("should return message flux of specified size") { - factory.createMessageFlux((getSampleMessageParameters(5))).test() - .expectNextCount(5) - .verifyComplete() - } - } - } -}) - -fun getSampleMessageParameters(amount: Long = -1): MessageParameters{ - val commonHeader = VesEventV5.VesEvent.CommonEventHeader.newBuilder() - .setVersion("sample-version") - .setDomain(HVRANMEAS) - .setSequence(1) - .setPriority(MEDIUM) - .setEventId("sample-event-id") - .setEventName("sample-event-name") - .setEventType("sample-event-type") - .setStartEpochMicrosec(SAMPLE_START_EPOCH) - .setLastEpochMicrosec(SAMPLE_LAST_EPOCH) - .setNfNamingCode("sample-nf-naming-code") - .setNfcNamingCode("sample-nfc-naming-code") - .setReportingEntityId("sample-reporting-entity-id") - .setReportingEntityName(ByteString.copyFromUtf8("sample-reporting-entity-name")) - .setSourceId(ByteString.copyFromUtf8("sample-source-id")) - .setSourceName("sample-source-name") - .build() - - return MessageParameters(commonHeader, amount) -} diff --git a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt deleted file mode 100644 index 73129a7f..00000000 --- a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/PayloadGeneratorTest.kt +++ /dev/null @@ -1,74 +0,0 @@ -/* - * ============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) - } - - } - } -}) |