2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.collectors.veshv.config.impl
23 import org.apache.commons.cli.CommandLine
24 import org.apache.commons.cli.CommandLineParser
25 import org.apache.commons.cli.DefaultParser
26 import org.apache.commons.cli.Options
27 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE
28 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
29 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
30 import org.onap.dcae.collectors.veshv.commandline.intValue
31 import org.onap.dcae.collectors.veshv.commandline.stringValue
32 import org.onap.dcae.collectors.veshv.utils.logging.Logger
35 internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) {
36 private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
38 private lateinit var parsedCommandLine: CommandLine
40 fun getConfigurationFile(args: Array<out String>): Either<WrongArgumentError, File> =
42 it.stringValue(CONFIGURATION_FILE).map(::File)
45 message = "Unexpected error when parsing command line arguments",
46 cmdLineOptionsList = cmdLineOptionsList)
49 fun getHealthcheckPort(args: Array<out String>): Int =
51 it.intValue(HEALTH_CHECK_API_PORT)
53 logger.info { "Healthcheck port missing on command line," +
54 " using default: $DEFAULT_HEALTHCHECK_PORT" }
55 DEFAULT_HEALTHCHECK_PORT
58 private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) =
59 Try { parseIfNotInitialized(args) }
61 .flatMap(cmdLineMapper)
63 private fun parseIfNotInitialized(args: Array<out String>): CommandLine {
64 if (!this::parsedCommandLine.isInitialized) {
65 parsedCommandLine = parseArgumentsArray(args)
67 return parsedCommandLine
70 private fun parseArgumentsArray(args: Array<out String>) =
73 .fold(Options(), Options::addOption)
74 .let { parser.parse(it, args) }
77 private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060
78 private val logger = Logger(HvVesCommandLineParser::class)