aboutsummaryrefslogtreecommitdiffstats
path: root/hv-collector-main
diff options
context:
space:
mode:
authorfkrzywka <filip.krzywka@nokia.com>2018-06-13 10:02:03 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 08:13:46 +0200
commit32aeb329986b3d6b9671c0a9e555cbff43964b3a (patch)
tree355bb884e579b27719f3b98e7aa61f7cb7131193 /hv-collector-main
parent1e77afdda9c9e89a313dec034c51f6cd0e407814 (diff)
Refactor ArgBasedConfiguration
Extracted duplicate code from Client/Server ArgBasedConfiguration, as it probably would be used third time in future work Change-Id: I581abbcd5f1dd4a1a049e1d28e68a7e4d82a84f8 Signed-off-by: fkrzywka <filip.krzywka@nokia.com> Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-main')
-rw-r--r--hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt109
-rw-r--r--hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt8
-rw-r--r--hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt11
3 files changed, 35 insertions, 93 deletions
diff --git a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt
index 91d29106..59b91d7f 100644
--- a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt
+++ b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt
@@ -19,11 +19,16 @@
*/
package org.onap.dcae.collectors.veshv.main
-import org.apache.commons.cli.*
+import org.apache.commons.cli.DefaultParser
+import org.apache.commons.cli.CommandLine
+import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.PRIVATE_KEY_FILE
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CERT_FILE
+import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_CERT_FILE
import org.onap.dcae.collectors.veshv.model.ServerConfiguration
-import org.onap.dcae.collectors.veshv.model.SecurityConfiguration
-import java.io.File
-import java.nio.file.Paths
+import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
internal object DefaultValues {
const val PORT = 6061
@@ -33,27 +38,26 @@ internal object DefaultValues {
const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
}
-internal class ArgBasedServerConfiguration {
-
- fun parse(args: Array<out String>): ServerConfiguration {
- val parser = DefaultParser()
-
- try {
- val cmdLine = parser.parse(options, args)
- val port = cmdLine.intValue(OPT_PORT, DefaultValues.PORT)
- val configUrl = cmdLine.stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL)
- val secConf = createSecurityConfiguration(cmdLine)
- return ServerConfiguration(port, configUrl, secConf)
- } catch (ex: Exception) {
- throw WrongArgumentException(ex)
- }
+internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
+ override val cmdLineOptionsList = listOf(
+ LISTEN_PORT,
+ CONSUL_CONFIG_URL,
+ PRIVATE_KEY_FILE,
+ CERT_FILE,
+ TRUST_CERT_FILE
+ )
+
+ override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration {
+ val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT)
+ val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL)
+ val security = createSecurityConfiguration(cmdLine)
+ return ServerConfiguration(port, configUrl, security)
}
private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
-
- 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)
+ val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
+ val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
+ val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
return SecurityConfiguration(
privateKey = stringPathToPath(pkFile),
@@ -61,65 +65,4 @@ internal class ArgBasedServerConfiguration {
trustedCert = stringPathToPath(trustCertFile)
)
}
-
- private fun CommandLine.intValue(option: Option, default: Int) =
- getOptionValue(option.opt)?.toInt() ?: default
-
- private fun CommandLine.stringValue(option: Option, default: String) =
- getOptionValue(option.opt) ?: default
-
- private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
-
- class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
- fun printMessage() {
- println(message)
- }
-
- fun printHelp(programName: String) {
- val formatter = HelpFormatter()
- formatter.printHelp(programName, options)
- }
- }
-
- companion object {
- private val OPT_PORT = Option.builder("p")
- .longOpt("listen-port")
- .hasArg()
- .desc("Listen port")
- .build()
-
- private val OPT_CONFIG_URL = Option.builder("c")
- .longOpt("config-url")
- .hasArg()
- .desc("URL of ves configuration on consul")
- .build()
-
- private val OPT_PK_FILE = Option.builder("k")
- .longOpt("private-key-file")
- .hasArg()
- .desc("File with private key in PEM format")
- .build()
-
- private val OPT_CERT_FILE = Option.builder("e")
- .longOpt("cert-file")
- .hasArg()
- .desc("File with server certificate bundle")
- .build()
-
- private val OPT_TRUST_CERT_FILE = Option.builder("t")
- .longOpt("trust-cert-file")
- .hasArg()
- .desc("File with trusted certificate bundle for authenticating clients")
- .build()
-
- private val options by lazy {
- val options = Options()
- options.addOption(OPT_PORT)
- options.addOption(OPT_CONFIG_URL)
- options.addOption(OPT_PK_FILE)
- options.addOption(OPT_CERT_FILE)
- options.addOption(OPT_TRUST_CERT_FILE)
- options
- }
- }
}
diff --git a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
index b2f4633a..b7d97028 100644
--- a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
+++ b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
@@ -20,13 +20,13 @@
package org.onap.dcae.collectors.veshv.main
import org.onap.dcae.collectors.veshv.boundary.ConfigurationProvider
-import org.onap.dcae.collectors.veshv.factory.CollectorFactory
-import org.onap.dcae.collectors.veshv.factory.ServerFactory
-import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory
-import org.onap.dcae.collectors.veshv.main.ArgBasedServerConfiguration.WrongArgumentException
+import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentException
import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
import org.onap.dcae.collectors.veshv.model.ServerConfiguration
import org.onap.dcae.collectors.veshv.model.routing
+import org.onap.dcae.collectors.veshv.factory.CollectorFactory
+import org.onap.dcae.collectors.veshv.factory.ServerFactory
+import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory
import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
import org.slf4j.LoggerFactory
import kotlin.system.exitProcess
diff --git a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt
index 38845945..923f9d58 100644
--- a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt
+++ b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt
@@ -25,7 +25,7 @@ import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
-import org.onap.dcae.collectors.veshv.model.SecurityConfiguration
+import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
import org.onap.dcae.collectors.veshv.model.ServerConfiguration
import java.nio.file.Paths
@@ -62,7 +62,6 @@ object ArgBasedServerConfigurationTest : Spek({
assertThat(result.port).isEqualTo(6969)
}
-
it("should set proper config url") {
assertThat(result.configurationUrl).isEqualTo(configurationUrl)
}
@@ -106,18 +105,18 @@ object ArgBasedServerConfigurationTest : Spek({
}
on("security config") {
- val secConf = result.securityConfiguration
+ val securityConfiguration = result.securityConfiguration
it("should set default trust cert file") {
- assertThat(secConf.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
+ assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE)
}
it("should set default server cert file") {
- assertThat(secConf.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
+ assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE)
}
it("should set default private key file") {
- assertThat(secConf.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
+ assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE)
}
}
}