71ed509bd4c20f07c06eebf3426802012eb8cb0c
[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.flatMap
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 java.io.File
36
37 class ArgHvVesConfiguration(private val parser: CommandLineParser = DefaultParser()) {
38     val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
39
40     fun getConfiguration(cmdLine: CommandLine): Option<File> =
41             cmdLine.stringValue(CONFIGURATION_FILE).map(::File)
42
43     fun getHealthcheckPort(cmdLine: CommandLine): Option<Int> =
44             cmdLine.intValue(HEALTH_CHECK_API_PORT)
45
46     lateinit var parsingResult: CommandLine
47
48     fun parseToFile(args: Array<out String>): Either<WrongArgumentError, File> {
49         parseIfEmpty(args)
50         return Try { parsingResult }.toEither()
51                 .mapLeft { WrongArgumentError(it, cmdLineOptionsList) }
52                 .map(this::getConfiguration)
53                 .flatMap {
54                     it.toEither {
55                         WrongArgumentError(
56                                 message = "Unexpected error when parsing command line arguments",
57                                 cmdLineOptionsList = cmdLineOptionsList)
58                     }
59                 }
60     }
61
62     fun parseToInt(args: Array<out String>): Either<WrongArgumentError, Int> {
63         parseIfEmpty(args)
64         return Try { parsingResult }.toEither()
65                 .mapLeft { WrongArgumentError(it, cmdLineOptionsList) }
66                 .map(this::getHealthcheckPort)
67                 .flatMap {
68                     it.toEither {
69                         WrongArgumentError(
70                                 message = "Unexpected error when parsing command line arguments",
71                                 cmdLineOptionsList = cmdLineOptionsList)
72                     }
73                 }
74     }
75
76     private fun parseIfEmpty(args: Array<out String>) {
77         if (!this::parsingResult.isInitialized) {
78             parsingResult = parseArgumentsArray(args)
79         }
80     }
81
82     private fun parseArgumentsArray(args: Array<out String>) =
83             cmdLineOptionsList
84                     .map { it.option }
85                     .fold(Options(), Options::addOption)
86                     .let { parser.parse(it, args) }
87
88 }
89
90