c1a9829450920f4f4a2e73967057b716885637cc
[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 = "Unexpected error when parsing command line arguments",
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," +
57                         " using default: $DEFAULT_HEALTHCHECK_PORT" }
58                 DEFAULT_HEALTHCHECK_PORT
59             }
60
61     private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) =
62             Try { parseIfNotInitialized(args) }
63                     .toOption()
64                     .flatMap(cmdLineMapper)
65
66     private fun parseIfNotInitialized(args: Array<out String>): CommandLine {
67         if (!this::parsedCommandLine.isInitialized) {
68             parsedCommandLine = parseArgumentsArray(args)
69         }
70         return parsedCommandLine
71     }
72
73     private fun parseArgumentsArray(args: Array<out String>) =
74             cmdLineOptionsList
75                     .map { it.option }
76                     .fold(Options(), Options::addOption)
77                     .let { parser.parse(it, args) }
78
79     companion object {
80         private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060
81         private val logger = Logger(HvVesCommandLineParser::class)
82     }
83
84 }
85
86