2  * ============LICENSE_START=======================================================
 
   3  * dcaegen2-collectors-veshv
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 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.main
 
  22 import arrow.core.Option
 
  24 import arrow.core.getOrElse
 
  25 import arrow.instances.option.monad.monad
 
  26 import arrow.typeclasses.binding
 
  27 import org.apache.commons.cli.CommandLine
 
  28 import org.apache.commons.cli.DefaultParser
 
  29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
 
  30 import org.onap.dcae.collectors.veshv.model.ConfigurationProviderParams
 
  31 import org.onap.dcae.collectors.veshv.model.KafkaConfiguration
 
  32 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
 
  33 import org.onap.dcae.collectors.veshv.model.ServiceContext
 
  34 import org.onap.dcae.collectors.veshv.ssl.boundary.createSecurityConfiguration
 
  35 import org.onap.dcae.collectors.veshv.utils.arrow.doOnFailure
 
  36 import org.onap.dcae.collectors.veshv.utils.commandline.*
 
  37 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL
 
  38 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KAFKA_SERVERS
 
  39 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_FIRST_REQUEST_DELAY
 
  40 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_REQUEST_INTERVAL
 
  41 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.DUMMY_MODE
 
  42 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
 
  43 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.IDLE_TIMEOUT_SEC
 
  44 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KEY_STORE_FILE
 
  45 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KEY_STORE_PASSWORD
 
  46 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
 
  47 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.MAXIMUM_PAYLOAD_SIZE_BYTES
 
  48 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.SSL_DISABLE
 
  49 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_STORE_FILE
 
  50 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_STORE_PASSWORD
 
  51 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LOG_LEVEL
 
  52 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 
  53 import org.onap.dcae.collectors.veshv.utils.logging.Logger
 
  54 import java.net.InetSocketAddress
 
  55 import java.time.Duration
 
  58 internal class ArgVesHvConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
 
  59     override val cmdLineOptionsList = listOf(
 
  61             HEALTH_CHECK_API_PORT,
 
  64             CONSUL_FIRST_REQUEST_DELAY,
 
  65             CONSUL_REQUEST_INTERVAL,
 
  72             MAXIMUM_PAYLOAD_SIZE_BYTES,
 
  77     override fun getConfiguration(cmdLine: CommandLine): Option<ServerConfiguration> =
 
  78             Option.monad().binding {
 
  79                 val healthCheckApiPort = cmdLine.intValue(
 
  80                         HEALTH_CHECK_API_PORT,
 
  81                         DefaultValues.HEALTH_CHECK_API_PORT
 
  83                 val kafkaServers = cmdLine.stringValue(KAFKA_SERVERS).bind()
 
  84                 val listenPort = cmdLine.intValue(LISTEN_PORT).bind()
 
  85                 val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
 
  86                 val maxPayloadSizeBytes = cmdLine.intValue(
 
  87                         MAXIMUM_PAYLOAD_SIZE_BYTES,
 
  88                         DefaultValues.MAX_PAYLOAD_SIZE_BYTES
 
  90                 val dummyMode = cmdLine.hasOption(DUMMY_MODE)
 
  91                 val security = createSecurityConfiguration(cmdLine)
 
  93                             logger.withError(ServiceContext::mdc) {
 
  94                                 log("Could not read security keys", ex)
 
  99                 val logLevel = cmdLine.stringValue(LOG_LEVEL, DefaultValues.LOG_LEVEL)
 
 100                 val configurationProviderParams = createConfigurationProviderParams(cmdLine).bind()
 
 102                         serverListenAddress = InetSocketAddress(listenPort),
 
 103                         kafkaConfiguration = KafkaConfiguration(kafkaServers, maxPayloadSizeBytes),
 
 104                         healthCheckApiListenAddress = InetSocketAddress(healthCheckApiPort),
 
 105                         configurationProviderParams = configurationProviderParams,
 
 106                         securityConfiguration = security,
 
 107                         idleTimeout = Duration.ofSeconds(idleTimeoutSec),
 
 108                         maximumPayloadSizeBytes = maxPayloadSizeBytes,
 
 109                         dummyMode = dummyMode,
 
 110                         logLevel = determineLogLevel(logLevel)
 
 115     private fun createConfigurationProviderParams(cmdLine: CommandLine): Option<ConfigurationProviderParams> =
 
 116             Option.monad().binding {
 
 117                 val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL).bind()
 
 118                 val firstRequestDelay = cmdLine.longValue(
 
 119                         CONSUL_FIRST_REQUEST_DELAY,
 
 120                         DefaultValues.CONSUL_FIRST_REQUEST_DELAY
 
 122                 val requestInterval = cmdLine.longValue(
 
 123                         CONSUL_REQUEST_INTERVAL,
 
 124                         DefaultValues.CONSUL_REQUEST_INTERVAL
 
 126                 ConfigurationProviderParams(
 
 128                         Duration.ofSeconds(firstRequestDelay),
 
 129                         Duration.ofSeconds(requestInterval)
 
 133     private fun determineLogLevel(logLevel: String) = LogLevel.optionFromString(logLevel)
 
 136                     "Failed to parse $logLevel as $LOG_LEVEL command line. " +
 
 137                             "Using default log level (${DefaultValues.LOG_LEVEL})"
 
 139                 LogLevel.valueOf(DefaultValues.LOG_LEVEL)
 
 143     internal object DefaultValues {
 
 144         const val HEALTH_CHECK_API_PORT = 6060
 
 145         const val CONSUL_FIRST_REQUEST_DELAY = 10L
 
 146         const val CONSUL_REQUEST_INTERVAL = 5L
 
 147         const val IDLE_TIMEOUT_SEC = 60L
 
 148         const val MAX_PAYLOAD_SIZE_BYTES = WireFrameMessage.DEFAULT_MAX_PAYLOAD_SIZE_BYTES
 
 149         val LOG_LEVEL = LogLevel.INFO.name
 
 153         private val logger = Logger(ArgVesHvConfiguration::class)