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
22 import arrow.core.Either
23 import arrow.core.Option
25 import arrow.core.getOrElse
26 import org.apache.commons.cli.CommandLine
27 import org.apache.commons.cli.CommandLineParser
28 import org.apache.commons.cli.DefaultParser
29 import org.apache.commons.cli.Options
30 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE
31 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
32 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
33 import org.onap.dcae.collectors.veshv.commandline.intValue
34 import org.onap.dcae.collectors.veshv.commandline.stringValue
35 import org.onap.dcae.collectors.veshv.utils.logging.Logger
38 internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) {
39 private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
41 private lateinit var parsedCommandLine: CommandLine
43 fun getConfigurationFile(args: Array<out String>): Either<WrongArgumentError, File> =
45 it.stringValue(CONFIGURATION_FILE).map(::File)
48 message = "Base configuration filepath missing on command line",
49 cmdLineOptionsList = cmdLineOptionsList)
52 fun getHealthcheckPort(args: Array<out String>): Int =
54 it.intValue(HEALTH_CHECK_API_PORT)
56 logger.info { "Healthcheck port missing on command line, using default: $DEFAULT_HEALTHCHECK_PORT" }
57 DEFAULT_HEALTHCHECK_PORT
60 private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) =
61 Try { parseIfNotInitialized(args) }
63 .flatMap(cmdLineMapper)
65 private fun parseIfNotInitialized(args: Array<out String>): CommandLine {
66 if (!this::parsedCommandLine.isInitialized) {
67 parsedCommandLine = parseArgumentsArray(args)
69 return parsedCommandLine
72 private fun parseArgumentsArray(args: Array<out String>) =
75 .fold(Options(), Options::addOption)
76 .let { parser.parse(it, args) }
79 private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060
80 private val logger = Logger(HvVesCommandLineParser::class)