c6730a4cb59fe90ced43bffdbbb0d0b198f55a26
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.config.impl
21
22 import arrow.core.Either
23 import arrow.core.Option
24 import arrow.core.Try
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
36 import java.io.File
37
38 internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) {
39     private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
40
41     private lateinit var parsedCommandLine: CommandLine
42
43     fun getConfigurationFile(args: Array<out String>): Either<WrongArgumentError, File> =
44             parse(args) {
45                 it.stringValue(CONFIGURATION_FILE).map(::File)
46             }.toEither {
47                 WrongArgumentError(
48                         message = "Base configuration filepath missing on command line",
49                         cmdLineOptionsList = cmdLineOptionsList)
50             }
51
52     fun getHealthcheckPort(args: Array<out String>): Int =
53             parse(args) {
54                 it.intValue(HEALTH_CHECK_API_PORT)
55             }.getOrElse {
56                 logger.info { "Healthcheck port missing on command line, using default: $DEFAULT_HEALTHCHECK_PORT" }
57                 DEFAULT_HEALTHCHECK_PORT
58             }
59
60     private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) =
61             Try { parseIfNotInitialized(args) }
62                     .toOption()
63                     .flatMap(cmdLineMapper)
64
65     private fun parseIfNotInitialized(args: Array<out String>): CommandLine {
66         if (!this::parsedCommandLine.isInitialized) {
67             parsedCommandLine = parseArgumentsArray(args)
68         }
69         return parsedCommandLine
70     }
71
72     private fun parseArgumentsArray(args: Array<out String>) =
73             cmdLineOptionsList
74                     .map { it.option }
75                     .fold(Options(), Options::addOption)
76                     .let { parser.parse(it, args) }
77
78     companion object {
79         private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060
80         private val logger = Logger(HvVesCommandLineParser::class)
81     }
82
83 }
84
85