aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfkrzywka <filip.krzywka@nokia.com>2018-06-13 15:02:40 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 08:34:32 +0200
commitecf7cb09d4bce389b615257d3323ada0840100e8 (patch)
tree9e5157afe12daa501c60a7674fb44d5e38bb9e5d
parente93ba2b32d71844a7075a021631f40a0cc4888df (diff)
Start DCAE App Simulator from command line
Closes ONAP-377 Change-Id: Iab959835dfafcfcfaf1322ead4ea83eff1e9284c Signed-off-by: fkrzywka <filip.krzywka@nokia.com> Issue-ID: DCAEGEN2-601
-rw-r--r--hv-collector-dcae-app-simulator/Dockerfile2
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfiguration.kt52
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/DcaeAppSimConfiguration.kt (renamed from hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/DummyTest.kt)20
-rw-r--r--hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt26
-rw-r--r--hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt120
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt14
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt25
-rw-r--r--hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt44
8 files changed, 281 insertions, 22 deletions
diff --git a/hv-collector-dcae-app-simulator/Dockerfile b/hv-collector-dcae-app-simulator/Dockerfile
index 8e1d7e83..a449078e 100644
--- a/hv-collector-dcae-app-simulator/Dockerfile
+++ b/hv-collector-dcae-app-simulator/Dockerfile
@@ -9,7 +9,7 @@ EXPOSE 8080
WORKDIR /opt/ves-hv-dcae-app-simulator
ENTRYPOINT ["java", "-cp", "*:", "org.onap.dcae.collectors.veshv.simulators.dcaeapp.MainKt"]
-CMD ["--kafka-bootstrap-servers", "TODO", "--kafka-topics", "TODO", "--api-port", "TODO"]
+CMD ["--kafka-bootstrap-servers", "kafka:9092", "--kafka-topics", "ves_hvRanMeas", "--listen-port", "8080"]
COPY target/libs/external/* ./
COPY target/libs/internal/* ./
COPY target/hv-collector-dcae-app-simulator-*.jar ./
diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfiguration.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfiguration.kt
new file mode 100644
index 00000000..3f539302
--- /dev/null
+++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfiguration.kt
@@ -0,0 +1,52 @@
+/*
+* ============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.dcaeapp.config
+
+import org.apache.commons.cli.CommandLine
+import org.apache.commons.cli.DefaultParser
+import org.onap.dcae.collectors.veshv.simulators.dcaeapp.config.DefaultValues.API_PORT
+import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KAFKA_SERVERS
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KAFKA_TOPICS
+
+internal object DefaultValues {
+ const val API_PORT = 8080
+}
+
+class ArgBasedDcaeAppSimConfiguration : ArgBasedConfiguration<DcaeAppSimConfiguration>(DefaultParser()) {
+ override val cmdLineOptionsList: List<CommandLineOption> = listOf(
+ LISTEN_PORT,
+ KAFKA_SERVERS,
+ KAFKA_TOPICS
+ )
+
+ override fun getConfiguration(cmdLine: CommandLine): DcaeAppSimConfiguration {
+ val port = cmdLine.intValue(LISTEN_PORT, API_PORT)
+ val kafkaBootstrapServers = cmdLine.stringValue(KAFKA_SERVERS)
+ val kafkaTopics = cmdLine.stringValue(KAFKA_TOPICS).split(",").toSet()
+ return DcaeAppSimConfiguration(
+ port,
+ kafkaBootstrapServers,
+ kafkaTopics)
+ }
+
+}
diff --git a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/DummyTest.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/DcaeAppSimConfiguration.kt
index 0360fcc7..5bd2d155 100644
--- a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/DummyTest.kt
+++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/DcaeAppSimConfiguration.kt
@@ -17,18 +17,10 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.onap.dcae.collectors.veshv.simulators.dcaeapp
+package org.onap.dcae.collectors.veshv.simulators.dcaeapp.config
-import org.jetbrains.spek.api.Spek
-import org.jetbrains.spek.api.dsl.it
-import org.jetbrains.spek.api.dsl.on
-import kotlin.test.assertEquals
-
-object DummyTest : Spek({
- on("sum of 2 and 3") {
- val sum = 2 + 3
- it("outcome should be equals 5") {
- assertEquals(5, sum)
- }
- }
-}) \ No newline at end of file
+data class DcaeAppSimConfiguration(
+ val apiPort: Int,
+ val kafkaBootstrapServers: String,
+ val kafkaTopics: Set<String>
+)
diff --git a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt
index 170806a1..c037af35 100644
--- a/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt
+++ b/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt
@@ -19,20 +19,32 @@
*/
package org.onap.dcae.collectors.veshv.simulators.dcaeapp
+import org.onap.dcae.collectors.veshv.simulators.dcaeapp.config.ArgBasedDcaeAppSimConfiguration
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.kafka.KafkaSource
import org.onap.dcae.collectors.veshv.simulators.dcaeapp.remote.ApiServer
+import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentException
import org.onap.dcae.collectors.veshv.utils.logging.Logger
import org.slf4j.LoggerFactory
private val logger = Logger(LoggerFactory.getLogger("DCAE simulator :: main"))
fun main(args: Array<String>) {
- logger.info("Starting DCAE APP simulator")
- val port = 8080
- KafkaSource.create("kafka:9092", setOf("ves_hvRanMeas"))
- .start()
- .map(::ApiServer)
- .flatMap { it.start(port) }
- .block()
+ try {
+ logger.info("Starting DCAE APP simulator")
+ val simulatorConfig = ArgBasedDcaeAppSimConfiguration().parse(args)
+
+ KafkaSource.create(simulatorConfig.kafkaBootstrapServers, simulatorConfig.kafkaTopics)
+ .start()
+ .map(::ApiServer)
+ .flatMap { it.start(simulatorConfig.apiPort) }
+ .block()
+ } catch (e: WrongArgumentException) {
+ e.printHelp("java org.onap.dcae.collectors.veshv.simulators.dcaeapp.MainKt")
+ System.exit(1)
+ } catch (e: Exception) {
+ logger.error(e.localizedMessage)
+ logger.debug("An error occurred when starting ves dcea app simulator", e)
+ System.exit(2)
+ }
}
diff --git a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt
new file mode 100644
index 00000000..817df8e0
--- /dev/null
+++ b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt
@@ -0,0 +1,120 @@
+/*
+ * ============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.dcaeapp.config
+
+import org.assertj.core.api.Assertions.assertThat
+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.utils.commandline.WrongArgumentException
+import kotlin.test.assertFailsWith
+
+
+internal class ArgBasedDcaeAppSimConfigurationTest : Spek({
+
+ lateinit var cut: ArgBasedDcaeAppSimConfiguration
+ val kafkaBootstrapServers = "localhosting:123,localhostinger:12345"
+ val kafkaTopics = "top1,top2"
+
+ beforeEachTest {
+ cut = ArgBasedDcaeAppSimConfiguration()
+ }
+
+ fun parse(vararg cmdLine: String) = cut.parse(cmdLine)
+
+ describe("parsing arguments") {
+ lateinit var result: DcaeAppSimConfiguration
+
+ given("all parameters are present in the long form") {
+
+ beforeEachTest {
+ result = parse("--listen-port", "6969",
+ "--kafka-bootstrap-servers", kafkaBootstrapServers,
+ "--kafka-topics", kafkaTopics
+ )
+ }
+
+ it("should set proper port") {
+ assertThat(result.apiPort).isEqualTo(6969)
+ }
+
+
+ it("should set proper kafka boostrap servers") {
+ assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
+ }
+
+ it("should set proper kafka topics") {
+ assertThat(result.kafkaTopics).isEqualTo(
+ setOf("top1", "top2")
+ )
+ }
+ }
+
+ given("some parameters are present in the short form") {
+
+ beforeEachTest {
+ result = parse("-p", "666", "--kafka-bootstrap-servers", kafkaBootstrapServers, "-f", kafkaTopics)
+ }
+
+ it("should set proper port") {
+ assertThat(result.apiPort).isEqualTo(666)
+ }
+
+ it("should set proper kafka boostrap servers") {
+ assertThat(result.kafkaBootstrapServers).isEqualTo(kafkaBootstrapServers)
+ }
+
+ it("should set proper kafka topics") {
+ assertThat(result.kafkaTopics).isEqualTo(
+ setOf("top1", "top2")
+ )
+ }
+ }
+
+ given("all optional parameters are absent") {
+
+ beforeEachTest {
+ result = parse("-s", kafkaBootstrapServers, "-f", kafkaTopics)
+ }
+
+ it("should set default port") {
+ assertThat(result.apiPort).isEqualTo(DefaultValues.API_PORT)
+ }
+ }
+
+
+ describe("required parameter is absent") {
+ given("kafka topics are missing") {
+ it("should throw exception") {
+ assertFailsWith<WrongArgumentException> { parse("-s", kafkaBootstrapServers) }
+ }
+ }
+
+ given("kafka bootstrap servers are missing") {
+ it("should throw exception") {
+ assertFailsWith<WrongArgumentException> { parse("-f", kafkaTopics) }
+ }
+ }
+ }
+ }
+
+
+}) \ No newline at end of file
diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt
index d36f194d..9d1f7aa8 100644
--- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt
@@ -55,6 +55,20 @@ enum class CommandLineOption(val option: Option) {
.desc("Amount of messages to send")
.build()
),
+ KAFKA_SERVERS(Option.builder("s")
+ .longOpt("kafka-bootstrap-servers")
+ .required()
+ .hasArg()
+ .desc("Comma-separated Kafka bootstrap servers in <host>:<port> format")
+ .build()
+ ),
+ KAFKA_TOPICS(Option.builder("f")
+ .longOpt("kafka-topics")
+ .required()
+ .hasArg()
+ .desc("Comma-separated Kafka topics")
+ .build()
+ ),
PRIVATE_KEY_FILE(Option.builder("k")
.longOpt("private-key-file")
.hasArg()
diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
index 314c9bf9..19bfa126 100644
--- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
@@ -107,4 +107,29 @@ class Logger(val logger: org.slf4j.Logger) {
logger.warn(messageProvider(), ex)
}
}
+
+
+ //
+ // ERROR
+ //
+
+ fun error(message: String) {
+ logger.error(message)
+ }
+
+ fun error(message: String, ex: Exception) {
+ logger.error(message, ex)
+ }
+
+ fun error(messageProvider: () -> String) {
+ if (logger.isErrorEnabled) {
+ logger.error(messageProvider())
+ }
+ }
+
+ fun error(ex: Exception, messageProvider: () -> String) {
+ if (logger.isErrorEnabled) {
+ logger.error(messageProvider(), ex)
+ }
+ }
}
diff --git a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
index be521970..b98131cc 100644
--- a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
+++ b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
@@ -181,6 +181,50 @@ object LoggerTest : Spek({
}
}
+ describe("error levels") {
+ it("should log message") {
+ cut.error(message)
+ verify(slf4jLogger).error(message)
+ }
+
+ it("should log message with exception") {
+ cut.error(message, exception)
+ verify(slf4jLogger).error(message, exception)
+ }
+
+ describe("lazy logging message") {
+
+ it("should log when debug is ON") {
+ whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
+ cut.error { message }
+ verify(slf4jLogger).isErrorEnabled
+ verify(slf4jLogger).error(message)
+ }
+
+ it("should not log when debug is OFF") {
+ whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
+ cut.error { message }
+ verify(slf4jLogger).isErrorEnabled
+ }
+ }
+
+ describe("lazy logging message with exception") {
+
+ it("should log when debug is ON") {
+ whenever(slf4jLogger.isErrorEnabled).thenReturn(true)
+ cut.error(exception) { message }
+ verify(slf4jLogger).isErrorEnabled
+ verify(slf4jLogger).error(message, exception)
+ }
+
+ it("should not log when debug is OFF") {
+ whenever(slf4jLogger.isErrorEnabled).thenReturn(false)
+ cut.error(exception) { message }
+ verify(slf4jLogger).isErrorEnabled
+ }
+ }
+ }
+
}
})