3e93a400c6942110d0925b3221d39b7f43279d3c
[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.*
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
33 import java.io.File
34
35 internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) {
36     private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
37
38     private lateinit var parsedCommandLine: CommandLine
39
40     fun getConfigurationFile(args: Array<out String>): Either<WrongArgumentError, File> =
41             parse(args) {
42                 it.stringValue(CONFIGURATION_FILE).map(::File)
43             }.toEither {
44                 WrongArgumentError(
45                         message = "Unexpected error when parsing command line arguments",
46                         cmdLineOptionsList = cmdLineOptionsList)
47             }
48
49     fun getHealthcheckPort(args: Array<out String>): Int =
50             parse(args) {
51                 it.intValue(HEALTH_CHECK_API_PORT)
52             }.getOrElse {
53                 logger.info { "Healthcheck port missing on command line," +
54                         " using default: $DEFAULT_HEALTHCHECK_PORT" }
55                 DEFAULT_HEALTHCHECK_PORT
56             }
57
58     private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) =
59             Try { parseIfNotInitialized(args) }
60                     .toOption()
61                     .flatMap(cmdLineMapper)
62
63     private fun parseIfNotInitialized(args: Array<out String>): CommandLine {
64         if (!this::parsedCommandLine.isInitialized) {
65             parsedCommandLine = parseArgumentsArray(args)
66         }
67         return parsedCommandLine
68     }
69
70     private fun parseArgumentsArray(args: Array<out String>) =
71             cmdLineOptionsList
72                     .map { it.option }
73                     .fold(Options(), Options::addOption)
74                     .let { parser.parse(it, args) }
75
76     companion object {
77         private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060
78         private val logger = Logger(HvVesCommandLineParser::class)
79     }
80
81 }
82
83