c70d128a8b9d7f75b1d321286a0b6c5da68d8639
[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.impl.adapters
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.routing
25 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain.HVRANMEAS
26 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain.forNumber
27 import org.slf4j.LoggerFactory
28 import reactor.core.publisher.Flux
29 import java.io.StringReader
30 import java.time.Duration
31 import java.util.Base64
32 import java.util.concurrent.atomic.AtomicReference
33 import javax.json.Json
34 import javax.json.JsonObject
35
36
37 /**
38  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
39  * @since May 2018
40  */
41 internal class ConsulConfigurationProvider(private val url: String,
42                                            private val updateInterval: Duration,
43                                            private val http: HttpAdapter
44 ) : ConfigurationProvider {
45
46
47     private val logger = LoggerFactory.getLogger(ConsulConfigurationProvider::class.java)
48     private var lastConfigurationHash: AtomicReference<Int> = AtomicReference()
49
50     override fun invoke(): Flux<CollectorConfiguration> =
51             Flux.concat(createDefaultConfigurationFlux(), createConsulFlux())
52
53     private fun createDefaultConfigurationFlux(): Flux<CollectorConfiguration> = Flux.just(
54             CollectorConfiguration(
55                     kafkaBootstrapServers = "kafka:9092",
56                     routing = routing {
57                         defineRoute {
58                             fromDomain(HVRANMEAS)
59                             toTopic("ves_hvRanMeas")
60                             withFixedPartitioning()
61                         }
62                     }.build())
63     ).doOnNext { logger.info("Applied default configuration") }
64
65     private fun createConsulFlux(): Flux<CollectorConfiguration> = Flux.interval(updateInterval)
66             .flatMap { http.get(url) }
67             .doOnError { logger.error("Encountered an error when trying to acquire configuration from consul. " +
68                     "Shutting down..") }
69             .filter { it.hashCode() != lastConfigurationHash.get() }
70             .doOnNext { lastConfigurationHash.set(it.hashCode()) }
71             .map { getConfigurationJson(it) }
72             .map { createCollectorConfiguration(it) }
73
74
75     private fun getConfigurationJson(str: String): JsonObject {
76         val response = Json.createReader(StringReader(str)).readArray().getJsonObject(0)
77         val decodedValue = String(
78                 Base64.getDecoder().decode(response.getString("Value")))
79         logger.info("Obtained new configuration from consul:\n$decodedValue")
80         return Json.createReader(StringReader(decodedValue)).readObject()
81     }
82
83     private fun createCollectorConfiguration(configuration: JsonObject): CollectorConfiguration {
84         val routing = configuration.getJsonArray("routing")
85
86         return CollectorConfiguration(
87                 kafkaBootstrapServers = configuration.getString("kafkaBootstrapServers"),
88                 routing = org.onap.dcae.collectors.veshv.model.routing {
89                     for (route in routing) {
90                         val routeObj = route.asJsonObject()
91                         defineRoute {
92                             fromDomain(forNumber(routeObj.getInt("fromDomain")))
93                             toTopic(routeObj.getString("toTopic"))
94                             withFixedPartitioning()
95                         }
96                     }
97                 }.build()
98         )
99     }
100 }
101