63de270e4596f32ef3574ee31b154c524bfbdf96
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.main
21
22 import org.apache.commons.cli.CommandLine
23 import org.apache.commons.cli.DefaultParser
24 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
25 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
26 import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
27 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption
28 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
29 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.UPDATE_INTERVAL
30 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL
31 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.PRIVATE_KEY_FILE
32 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CERT_FILE
33 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_CERT_FILE
34 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.IDLE_TIMEOUT_SEC
35 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.DUMMY_MODE
36
37 import java.time.Duration
38
39 internal object DefaultValues {
40     const val PORT = 6061
41     const val UPDATE_INTERVAL = 300L
42     const val CONFIG_URL = ""
43     const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
44     const val CERT_FILE = "/etc/ves-hv/server.crt"
45     const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
46     const val IDLE_TIMEOUT_SEC = 60L
47 }
48
49 internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
50     override val cmdLineOptionsList = listOf(
51             LISTEN_PORT,
52             UPDATE_INTERVAL,
53             CONSUL_CONFIG_URL,
54             PRIVATE_KEY_FILE,
55             CERT_FILE,
56             TRUST_CERT_FILE,
57             IDLE_TIMEOUT_SEC,
58             DUMMY_MODE
59     )
60
61     override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration {
62         val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT)
63         val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL)
64         val updateInterval = cmdLine.longValue(UPDATE_INTERVAL, DefaultValues.UPDATE_INTERVAL)
65         val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
66         val dummyMode = cmdLine.hasOption(DUMMY_MODE)
67         val security = createSecurityConfiguration(cmdLine)
68         return ServerConfiguration(
69                 port = port,
70                 configurationUrl = configUrl,
71                 configurationUpdateInterval = Duration.ofSeconds(updateInterval),
72                 securityConfiguration = security,
73                 idleTimeout = Duration.ofSeconds(idleTimeoutSec),
74                 dummyMode = dummyMode)
75     }
76
77     private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
78         val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
79         val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
80         val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
81
82         return SecurityConfiguration(
83                 privateKey = stringPathToPath(pkFile),
84                 cert = stringPathToPath(certFile),
85                 trustedCert = stringPathToPath(trustCertFile)
86         )
87     }
88 }