Fix TCP stream framing issue
[dcaegen2/collectors/hv-ves.git] / hv-collector-main / src / main / kotlin / org / onap / dcae / collectors / veshv / main / main.kt
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.onap.dcae.collectors.veshv.boundary.ConfigurationProvider
23 import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
24 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
25 import org.onap.dcae.collectors.veshv.model.routing
26 import org.onap.dcae.collectors.veshv.factory.CollectorFactory
27 import org.onap.dcae.collectors.veshv.factory.ServerFactory
28 import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory
29 import org.onap.dcae.collectors.veshv.main.ArgBasedServerConfiguration.WrongArgumentException
30 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
31 import org.slf4j.LoggerFactory
32 import kotlin.system.exitProcess
33
34 private val logger = LoggerFactory.getLogger("main")
35
36 fun main(args: Array<String>) {
37     try {
38         val serverConfiguration = ArgBasedServerConfiguration().parse(args)
39
40         val collectorProvider = CollectorFactory(
41                 resolveConfigurationProvider(serverConfiguration),
42                 AdapterFactory.loggingSink()
43         ).createVesHvCollectorProvider()
44         ServerFactory.createNettyTcpServer(serverConfiguration, collectorProvider).start().block()
45     } catch (ex: WrongArgumentException) {
46         ex.printMessage()
47         ex.printHelp("java org.onap.dcae.collectors.veshv.main.MainKt")
48         exitProcess(1)
49     }
50 }
51
52
53 private fun resolveConfigurationProvider(serverConfiguration: ServerConfiguration): ConfigurationProvider {
54
55     if (serverConfiguration.configurationUrl.isEmpty()) {
56         logger.info("Configuration url not specified - using default config")
57         val sampleConfig = CollectorConfiguration(
58                 kafkaBootstrapServers = "dmaap.cluster.local:9969",
59                 routing = routing {
60                     defineRoute {
61                         fromDomain(Domain.HVRANMEAS)
62                         toTopic("ves_hvRanMeas")
63                         withFixedPartitioning()
64                     }
65                 }.build()
66         )
67         return AdapterFactory.staticConfigurationProvider(sampleConfig)
68     }
69
70     logger.info("Using configuration url: ${serverConfiguration.configurationUrl}")
71     return AdapterFactory.consulConfigurationProvider(serverConfiguration.configurationUrl)
72 }