From 767d0464a19e0949d2919e6df15c9653dec50503 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Fri, 8 Jun 2018 14:55:25 +0200 Subject: Cleanup simulator dependencies and packages Closes ONAP-307 Change-Id: Ifdaa9f054470386c01865d44c78a3aecf02ce956 Signed-off-by: Piotr Jaszczyk Issue-ID: DCAEGEN2-601 --- hv-collector-client-simulator/pom.xml | 5 - .../config/ArgBasedClientConfiguration.kt | 144 --------------------- .../config/ClientConfiguration.kt | 30 ----- .../config/ClientSecurityConfiguration.kt | 31 ----- .../impl/MessageFactory.kt | 78 ----------- .../impl/VesHvClient.kt | 86 ------------ .../org.onap.dcae.collectors.veshv.main/main.kt | 47 ------- .../xnf/config/ArgBasedClientConfiguration.kt | 143 ++++++++++++++++++++ .../simulators/xnf/config/ClientConfiguration.kt | 30 +++++ .../xnf/config/ClientSecurityConfiguration.kt | 31 +++++ .../veshv/simulators/xnf/impl/MessageFactory.kt | 78 +++++++++++ .../veshv/simulators/xnf/impl/VesHvClient.kt | 86 ++++++++++++ .../dcae/collectors/veshv/simulators/xnf/main.kt | 47 +++++++ .../MessageFactoryTest.kt | 51 -------- .../simulators/xnf/impl/MessageFactoryTest.kt | 51 ++++++++ 15 files changed, 466 insertions(+), 472 deletions(-) delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ArgBasedClientConfiguration.kt delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientConfiguration.kt delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientSecurityConfiguration.kt delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/MessageFactory.kt delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/VesHvClient.kt delete mode 100644 hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/main.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt create mode 100644 hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt delete mode 100644 hv-collector-client-simulator/src/test/kotlin/org.onap.dcae.collectors.veshv.main/MessageFactoryTest.kt create mode 100644 hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt diff --git a/hv-collector-client-simulator/pom.xml b/hv-collector-client-simulator/pom.xml index 0d0db3fb..e7a25855 100644 --- a/hv-collector-client-simulator/pom.xml +++ b/hv-collector-client-simulator/pom.xml @@ -119,11 +119,6 @@ hv-collector-utils ${project.parent.version} - - ${project.parent.groupId} - hv-collector-core - ${project.parent.version} - commons-cli commons-cli diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ArgBasedClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ArgBasedClientConfiguration.kt deleted file mode 100644 index 628afdad..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ArgBasedClientConfiguration.kt +++ /dev/null @@ -1,144 +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.main.config - -import org.apache.commons.cli.Option -import org.apache.commons.cli.Options -import org.apache.commons.cli.DefaultParser -import org.apache.commons.cli.CommandLine -import org.apache.commons.cli.HelpFormatter -import java.io.File -import java.nio.file.Paths - - -internal object DefaultValues { - const val MESSAGES_AMOUNT = 1 - const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key" - const val CERT_FILE = "/etc/ves-hv/client.crt" - const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" -} - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -internal object ArgBasedClientConfiguration { - - private val OPT_VES_PORT = Option.builder("p") - .longOpt("ves-port") - .required() - .hasArg() - .desc("VesHvCollector port") - .build() - - private val OPT_VES_HOST = Option.builder("h") - .longOpt("ves-host") - .required() - .hasArg() - .desc("VesHvCollector host") - .build() - - private val OPT_MESSAGES_AMOUNT = Option.builder("m") - .longOpt("messages") - .hasArg() - .desc("Amount of messages to send") - .build() - - private val OPT_PK_FILE = Option.builder("k") - .longOpt("private-key-file") - .hasArg() - .desc("File with client private key in PEM format") - .build() - - private val OPT_CERT_FILE = Option.builder("e") - .longOpt("cert-file") - .hasArg() - .desc("File with client certificate bundle") - .build() - - private val OPT_TRUST_CERT_FILE = Option.builder("t") - .longOpt("trust-cert-file") - .hasArg() - .desc("File with trusted certificate bundle for trusting servers") - .build() - - private val options by lazy { - val options = Options() - options.addOption(OPT_VES_PORT) - options.addOption(OPT_VES_HOST) - options.addOption(OPT_MESSAGES_AMOUNT) - options.addOption(OPT_PK_FILE) - options.addOption(OPT_CERT_FILE) - options.addOption(OPT_TRUST_CERT_FILE) - options - } - - fun parse(args: Array): ClientConfiguration { - - - val parser = DefaultParser() - - try { - val cmdLine = parser.parse(options, args) - val host = cmdLine.stringValue(OPT_VES_HOST) - val port = cmdLine.intValue(OPT_VES_PORT) - val msgsAmount = cmdLine.intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT) - return ClientConfiguration( - host, - port, - parseSecurityConfig(cmdLine), - msgsAmount) - } catch (ex: Exception) { - throw WrongArgumentException(ex) - } - } - - private fun parseSecurityConfig(cmdLine: CommandLine): ClientSecurityConfiguration { - val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE) - val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE) - val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE) - return ClientSecurityConfiguration( - privateKey = stringPathToPath(pkFile), - cert = stringPathToPath(certFile), - trustedCert = stringPathToPath(trustCertFile)) - } - - private fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) - - private fun CommandLine.intValueOrDefault(option: Option, default: Int) = - getOptionValue(option.opt)?.toInt() ?: default - - private fun CommandLine.intValue(option: Option) = - getOptionValue(option.opt).toInt() - - private fun CommandLine.stringValue(option: Option) = - getOptionValue(option.opt) - - private fun CommandLine.stringValue(option: Option, default: String) = - getOptionValue(option.opt) ?: default - - - class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) { - fun printHelp(programName: String) { - val formatter = HelpFormatter() - formatter.printHelp(programName, options) - } - } -} diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientConfiguration.kt deleted file mode 100644 index e3cba57b..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientConfiguration.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.main.config - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -data class ClientConfiguration( - val vesHost: String, - val vesPort: Int, - val security: ClientSecurityConfiguration, - val messagesAmount: Int) diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientSecurityConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientSecurityConfiguration.kt deleted file mode 100644 index fc7cf665..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/config/ClientSecurityConfiguration.kt +++ /dev/null @@ -1,31 +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.main.config - -import java.nio.file.Path - -/** - * @author Piotr Jaszczyk - * @since June 2018 - */ -data class ClientSecurityConfiguration( - val privateKey: Path, - val cert: Path, - val trustedCert: Path) diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/MessageFactory.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/MessageFactory.kt deleted file mode 100644 index ac0c0867..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/MessageFactory.kt +++ /dev/null @@ -1,78 +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.main.impl - -import com.google.protobuf.ByteString -import io.netty.buffer.ByteBuf -import io.netty.buffer.Unpooled -import org.onap.dcae.collectors.veshv.domain.WireFrame -import org.onap.ves.VesEventV5 -import reactor.core.publisher.Flux -import reactor.core.publisher.Mono - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -class MessageFactory { - - companion object { - const val DEFAULT_START_EPOCH: Long = 120034455 - const val DEFAULT_LAST_EPOCH: Long = 120034455 - } - - fun createMessageFlux(amount: Int = 1): Flux = - Mono.just(createMessage()).repeat(amount.toLong()) - - - private fun createMessage(): WireFrame { - val commonHeader = VesEventV5.VesEvent.CommonEventHeader.newBuilder() - .setVersion("1.9") - .setEventName("Sample event name") - .setDomain(VesEventV5.VesEvent.CommonEventHeader.Domain.HVRANMEAS) - .setEventId("Sample event Id") - .setSourceName("Sample Source") - .setReportingEntityName(ByteString.copyFromUtf8("Sample byte String")) - .setPriority(VesEventV5.VesEvent.CommonEventHeader.Priority.MEDIUM) - .setStartEpochMicrosec(DEFAULT_START_EPOCH) - .setLastEpochMicrosec(DEFAULT_LAST_EPOCH) - .setSequence(2) - .build() - - val payload = vesMessageBytes(commonHeader) - return WireFrame( - payload = payload, - mark = 0xFF, - majorVersion = 1, - minorVersion = 2, - payloadSize = payload.readableBytes()) - - - } - - private fun vesMessageBytes(commonHeader: VesEventV5.VesEvent.CommonEventHeader): ByteBuf { - val msg = VesEventV5.VesEvent.newBuilder() - .setCommonEventHeader(commonHeader) - .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data")) - .build() - - return Unpooled.wrappedBuffer(msg.toByteArray()) - } -} diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/VesHvClient.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/VesHvClient.kt deleted file mode 100644 index 4553ab20..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/impl/VesHvClient.kt +++ /dev/null @@ -1,86 +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.main.impl - -import io.netty.handler.ssl.ClientAuth -import io.netty.handler.ssl.SslContext -import io.netty.handler.ssl.SslContextBuilder -import io.netty.handler.ssl.SslProvider -import org.onap.dcae.collectors.veshv.domain.WireFrame -import org.onap.dcae.collectors.veshv.main.config.ClientConfiguration -import org.onap.dcae.collectors.veshv.main.config.ClientSecurityConfiguration -import org.onap.dcae.collectors.veshv.utils.logging.Logger -import org.reactivestreams.Publisher -import reactor.core.publisher.Flux -import reactor.ipc.netty.NettyInbound -import reactor.ipc.netty.NettyOutbound -import reactor.ipc.netty.tcp.TcpClient -import java.util.function.BiFunction - - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -class VesHvClient(configuration: ClientConfiguration) { - - private val client: TcpClient = TcpClient.builder() - .options { opts -> - opts.host(configuration.vesHost) - .port(configuration.vesPort) - .sslContext(createSslContext(configuration.security)) - } - .build() - - fun send(messages: Flux) { - client.startAndAwait(BiFunction { i, o -> handler(i, o, messages) }) - } - - // sending flux with multiple WireFrames not supported yet - private fun handler(nettyInbound: NettyInbound, - nettyOutbound: NettyOutbound, - messages: Flux): Publisher { - - nettyInbound - .receive() - .asString(Charsets.UTF_8) - .subscribe { str -> logger.info("Server response: $str") } - - val frames = messages - .doOnNext { logger.info { "About to send message with ${it.payloadSize} B of payload" } } - .map { it.encode(nettyOutbound.alloc()) } - - return nettyOutbound - .options { it.flushOnEach() } - .send(frames) - } - - private fun createSslContext(config: ClientSecurityConfiguration): SslContext = - SslContextBuilder.forClient() - .keyManager(config.cert.toFile(), config.privateKey.toFile()) - .trustManager(config.trustedCert.toFile()) - .sslProvider(SslProvider.OPENSSL) - .clientAuth(ClientAuth.REQUIRE) - .build() - - companion object { - private val logger = Logger(VesHvClient::class) - } -} diff --git a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/main.kt b/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/main.kt deleted file mode 100644 index a41035da..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org.onap.dcae.collectors.veshv.main/main.kt +++ /dev/null @@ -1,47 +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.main - -import org.onap.dcae.collectors.veshv.main.config.ArgBasedClientConfiguration -import org.onap.dcae.collectors.veshv.main.impl.MessageFactory -import org.onap.dcae.collectors.veshv.main.impl.VesHvClient -import org.slf4j.LoggerFactory.getLogger - - -private val logger = getLogger("Simulator :: main") - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -fun main(args: Array) { - try { - val clientConfig = ArgBasedClientConfiguration.parse(args) - val messageFactory = MessageFactory() - val client = VesHvClient(clientConfig) - client.send(messageFactory.createMessageFlux(clientConfig.messagesAmount)) - } catch (e: ArgBasedClientConfiguration.WrongArgumentException) { - e.printHelp("java org.onap.dcae.collectors.veshv.main.MainKt") - } catch (e: Exception) { - logger.error(e.localizedMessage) - logger.debug("An error occurred when starting ves client", e) - } -} - diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt new file mode 100644 index 00000000..49653b57 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt @@ -0,0 +1,143 @@ +/* + * ============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.apache.commons.cli.CommandLine +import org.apache.commons.cli.DefaultParser +import org.apache.commons.cli.HelpFormatter +import org.apache.commons.cli.Option +import org.apache.commons.cli.Options +import java.io.File +import java.nio.file.Paths + +internal object DefaultValues { + const val MESSAGES_AMOUNT = 1 + const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key" + const val CERT_FILE = "/etc/ves-hv/client.crt" + const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" +} + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +internal object ArgBasedClientConfiguration { + + private val OPT_VES_PORT = Option.builder("p") + .longOpt("ves-port") + .required() + .hasArg() + .desc("VesHvCollector port") + .build() + + private val OPT_VES_HOST = Option.builder("h") + .longOpt("ves-host") + .required() + .hasArg() + .desc("VesHvCollector host") + .build() + + private val OPT_MESSAGES_AMOUNT = Option.builder("m") + .longOpt("messages") + .hasArg() + .desc("Amount of messages to send") + .build() + + private val OPT_PK_FILE = Option.builder("k") + .longOpt("private-key-file") + .hasArg() + .desc("File with client private key in PEM format") + .build() + + private val OPT_CERT_FILE = Option.builder("e") + .longOpt("cert-file") + .hasArg() + .desc("File with client certificate bundle") + .build() + + private val OPT_TRUST_CERT_FILE = Option.builder("t") + .longOpt("trust-cert-file") + .hasArg() + .desc("File with trusted certificate bundle for trusting servers") + .build() + + private val options by lazy { + val options = Options() + options.addOption(OPT_VES_PORT) + options.addOption(OPT_VES_HOST) + options.addOption(OPT_MESSAGES_AMOUNT) + options.addOption(OPT_PK_FILE) + options.addOption(OPT_CERT_FILE) + options.addOption(OPT_TRUST_CERT_FILE) + options + } + + fun parse(args: Array): ClientConfiguration { + + + val parser = DefaultParser() + + try { + val cmdLine = parser.parse(options, args) + val host = cmdLine.stringValue(OPT_VES_HOST) + val port = cmdLine.intValue(OPT_VES_PORT) + val msgsAmount = cmdLine.intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT) + return ClientConfiguration( + host, + port, + parseSecurityConfig(cmdLine), + msgsAmount) + } catch (ex: Exception) { + throw WrongArgumentException(ex) + } + } + + private fun parseSecurityConfig(cmdLine: CommandLine): ClientSecurityConfiguration { + val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE) + val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE) + val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE) + return ClientSecurityConfiguration( + privateKey = stringPathToPath(pkFile), + cert = stringPathToPath(certFile), + trustedCert = stringPathToPath(trustCertFile)) + } + + private fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) + + private fun CommandLine.intValueOrDefault(option: Option, default: Int) = + getOptionValue(option.opt)?.toInt() ?: default + + private fun CommandLine.intValue(option: Option) = + getOptionValue(option.opt).toInt() + + private fun CommandLine.stringValue(option: Option) = + getOptionValue(option.opt) + + private fun CommandLine.stringValue(option: Option, default: String) = + getOptionValue(option.opt) ?: default + + + class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) { + fun printHelp(programName: String) { + val formatter = HelpFormatter() + formatter.printHelp(programName, options) + } + } +} diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt new file mode 100644 index 00000000..e835ee95 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt @@ -0,0 +1,30 @@ +/* + * ============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 + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +data class ClientConfiguration( + val vesHost: String, + val vesPort: Int, + val security: ClientSecurityConfiguration, + val messagesAmount: Int) diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt new file mode 100644 index 00000000..bb4f306a --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt @@ -0,0 +1,31 @@ +/* + * ============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 java.nio.file.Path + +/** + * @author Piotr Jaszczyk + * @since June 2018 + */ +data class ClientSecurityConfiguration( + val privateKey: Path, + val cert: Path, + val trustedCert: Path) 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 new file mode 100644 index 00000000..0c578b38 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactory.kt @@ -0,0 +1,78 @@ +/* + * ============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 io.netty.buffer.ByteBuf +import io.netty.buffer.Unpooled +import org.onap.dcae.collectors.veshv.domain.WireFrame +import org.onap.ves.VesEventV5 +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +class MessageFactory { + + companion object { + const val DEFAULT_START_EPOCH: Long = 120034455 + const val DEFAULT_LAST_EPOCH: Long = 120034455 + } + + fun createMessageFlux(amount: Int = 1): Flux = + Mono.just(createMessage()).repeat(amount.toLong()) + + + private fun createMessage(): WireFrame { + val commonHeader = VesEventV5.VesEvent.CommonEventHeader.newBuilder() + .setVersion("1.9") + .setEventName("Sample event name") + .setDomain(VesEventV5.VesEvent.CommonEventHeader.Domain.HVRANMEAS) + .setEventId("Sample event Id") + .setSourceName("Sample Source") + .setReportingEntityName(ByteString.copyFromUtf8("Sample byte String")) + .setPriority(VesEventV5.VesEvent.CommonEventHeader.Priority.MEDIUM) + .setStartEpochMicrosec(DEFAULT_START_EPOCH) + .setLastEpochMicrosec(DEFAULT_LAST_EPOCH) + .setSequence(2) + .build() + + val payload = vesMessageBytes(commonHeader) + return WireFrame( + payload = payload, + mark = 0xFF, + majorVersion = 1, + minorVersion = 2, + payloadSize = payload.readableBytes()) + + + } + + private fun vesMessageBytes(commonHeader: VesEventV5.VesEvent.CommonEventHeader): ByteBuf { + val msg = VesEventV5.VesEvent.newBuilder() + .setCommonEventHeader(commonHeader) + .setHvRanMeasFields(ByteString.copyFromUtf8("high volume data")) + .build() + + return Unpooled.wrappedBuffer(msg.toByteArray()) + } +} diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt new file mode 100644 index 00000000..c911c533 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt @@ -0,0 +1,86 @@ +/* + * ============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 io.netty.handler.ssl.ClientAuth +import io.netty.handler.ssl.SslContext +import io.netty.handler.ssl.SslContextBuilder +import io.netty.handler.ssl.SslProvider +import org.onap.dcae.collectors.veshv.domain.WireFrame +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientSecurityConfiguration +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import org.reactivestreams.Publisher +import reactor.core.publisher.Flux +import reactor.ipc.netty.NettyInbound +import reactor.ipc.netty.NettyOutbound +import reactor.ipc.netty.tcp.TcpClient +import java.util.function.BiFunction + + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +class VesHvClient(configuration: ClientConfiguration) { + + private val client: TcpClient = TcpClient.builder() + .options { opts -> + opts.host(configuration.vesHost) + .port(configuration.vesPort) + .sslContext(createSslContext(configuration.security)) + } + .build() + + fun send(messages: Flux) { + client.startAndAwait(BiFunction { i, o -> handler(i, o, messages) }) + } + + // sending flux with multiple WireFrames not supported yet + private fun handler(nettyInbound: NettyInbound, + nettyOutbound: NettyOutbound, + messages: Flux): Publisher { + + nettyInbound + .receive() + .asString(Charsets.UTF_8) + .subscribe { str -> logger.info("Server response: $str") } + + val frames = messages + .doOnNext { logger.info { "About to send message with ${it.payloadSize} B of payload" } } + .map { it.encode(nettyOutbound.alloc()) } + + return nettyOutbound + .options { it.flushOnEach() } + .send(frames) + } + + private fun createSslContext(config: ClientSecurityConfiguration): SslContext = + SslContextBuilder.forClient() + .keyManager(config.cert.toFile(), config.privateKey.toFile()) + .trustManager(config.trustedCert.toFile()) + .sslProvider(SslProvider.OPENSSL) + .clientAuth(ClientAuth.REQUIRE) + .build() + + companion object { + private val logger = Logger(VesHvClient::class) + } +} diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt new file mode 100644 index 00000000..ee7f49a6 --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt @@ -0,0 +1,47 @@ +/* + * ============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.config.ArgBasedClientConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.impl.MessageFactory +import org.onap.dcae.collectors.veshv.simulators.xnf.impl.VesHvClient +import org.slf4j.LoggerFactory.getLogger + + +private val logger = getLogger("Simulator :: main") + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +fun main(args: Array) { + try { + val clientConfig = ArgBasedClientConfiguration.parse(args) + val messageFactory = MessageFactory() + val client = VesHvClient(clientConfig) + client.send(messageFactory.createMessageFlux(clientConfig.messagesAmount)) + } catch (e: ArgBasedClientConfiguration.WrongArgumentException) { + e.printHelp("java org.onap.dcae.collectors.veshv.main.MainKt") + } catch (e: Exception) { + logger.error(e.localizedMessage) + logger.debug("An error occurred when starting ves client", e) + } +} + diff --git a/hv-collector-client-simulator/src/test/kotlin/org.onap.dcae.collectors.veshv.main/MessageFactoryTest.kt b/hv-collector-client-simulator/src/test/kotlin/org.onap.dcae.collectors.veshv.main/MessageFactoryTest.kt deleted file mode 100644 index af00ae8a..00000000 --- a/hv-collector-client-simulator/src/test/kotlin/org.onap.dcae.collectors.veshv.main/MessageFactoryTest.kt +++ /dev/null @@ -1,51 +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.main - -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.main.impl.MessageFactory -import kotlin.test.assertEquals - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -object MessageFactoryTest : Spek({ - describe("message factory") { - val factory = MessageFactory() - - given("no parameters") { - it("should return flux with one message") { - val result = factory.createMessageFlux() - - assertEquals(1, result.count().block()) - } - } - given("messages amount") { - it("should return message flux of specified size") { - val result = factory.createMessageFlux(5) - assertEquals(5, result.count().block()) - } - } - } -}) \ No newline at end of file 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 new file mode 100644 index 00000000..edcec65f --- /dev/null +++ b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/MessageFactoryTest.kt @@ -0,0 +1,51 @@ +/* + * ============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.describe +import org.jetbrains.spek.api.dsl.given +import org.jetbrains.spek.api.dsl.it +import org.onap.dcae.collectors.veshv.simulators.xnf.impl.MessageFactory +import kotlin.test.assertEquals + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +object MessageFactoryTest : Spek({ + describe("message factory") { + val factory = MessageFactory() + + given("no parameters") { + it("should return flux with one message") { + val result = factory.createMessageFlux() + + assertEquals(1, result.count().block()) + } + } + given("messages amount") { + it("should return message flux of specified size") { + val result = factory.createMessageFlux(5) + assertEquals(5, result.count().block()) + } + } + } +}) -- cgit 1.2.3-korg