Start DCAE App Simulator from command line 85/58585/1
authorfkrzywka <filip.krzywka@nokia.com>
Wed, 13 Jun 2018 13:02:40 +0000 (15:02 +0200)
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Thu, 2 Aug 2018 06:34:32 +0000 (08:34 +0200)
Closes ONAP-377

Change-Id: Iab959835dfafcfcfaf1322ead4ea83eff1e9284c
Signed-off-by: fkrzywka <filip.krzywka@nokia.com>
Issue-ID: DCAEGEN2-601

hv-collector-dcae-app-simulator/Dockerfile
hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfiguration.kt [new file with mode: 0644]
hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/DcaeAppSimConfiguration.kt [moved from hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/DummyTest.kt with 72% similarity]
hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/main.kt
hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/config/ArgBasedDcaeAppSimConfigurationTest.kt [new file with mode: 0644]
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt

index 8e1d7e8..a449078 100644 (file)
@@ -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 (file)
index 0000000..3f53930
--- /dev/null
@@ -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)
+    }
+
+}
  * 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>
+)
index 170806a..c037af3 100644 (file)
  */
 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 (file)
index 0000000..817df8e
--- /dev/null
@@ -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
index d36f194..9d1f7aa 100644 (file)
@@ -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()
index 314c9bf..19bfa12 100644 (file)
@@ -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)
+        }
+    }
 }
index be52197..b98131c 100644 (file)
@@ -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
+                }
+            }
+        }
+
 
     }
 })