2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018-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
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.simulators.dcaeapp.impl.adapters
22 import arrow.core.Option
23 import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.DcaeAppSimulator
24 import org.onap.dcae.collectors.veshv.utils.NettyServerHandle
25 import org.onap.dcae.collectors.veshv.utils.ServerHandle
26 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
27 import org.onap.dcae.collectors.veshv.utils.http.HttpStatus
28 import org.onap.dcae.collectors.veshv.utils.http.Response
29 import org.onap.dcae.collectors.veshv.utils.http.Responses
30 import org.onap.dcae.collectors.veshv.utils.http.Responses.stringResponse
31 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
32 import org.onap.dcae.collectors.veshv.utils.http.sendOrError
33 import org.onap.dcae.collectors.veshv.utils.logging.Logger
34 import reactor.core.publisher.Mono
35 import reactor.netty.http.server.HttpServer
36 import reactor.netty.http.server.HttpServerRequest
37 import reactor.netty.http.server.HttpServerRoutes
38 import java.net.InetSocketAddress
41 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
44 internal class DcaeAppApiServer(private val simulator: DcaeAppSimulator) {
46 fun start(socketAddress: InetSocketAddress, kafkaTopics: Set<String>): Mono<ServerHandle> =
48 simulator.listenToTopics(kafkaTopics)
50 .host(socketAddress.hostName)
51 .port(socketAddress.port)
54 .map { NettyServerHandle(it) }
57 private fun setRoutes(route: HttpServerRoutes) {
59 .put("/configuration/topics") { req, res ->
61 .receive().aggregate().asString()
63 res.sendOrError { simulator.listenToTopics(it) }
66 .delete("/messages/{$TOPIC_PARAM_KEY}") { req, res ->
67 doWithTopicOrReturnInternalErrorResponse(req) {
68 logger.info { "Resetting simulator state for topic $it" }
69 simulator.resetState(it)
70 Mono.just(Responses.Success)
71 }.let(res::sendAndHandleErrors)
74 .get("/messages/{$TOPIC_PARAM_KEY}/count") { req, res ->
75 doWithTopicOrReturnInternalErrorResponse(req) {
76 logger.info { "Processing request for count of received messages for topic $it" }
79 val errorMessage = { COUNT_NOT_RESOLVED_MESSAGE + ". Reason: ${it.message}" }
80 logger.warn(errorMessage)
81 Mono.just(Responses.statusResponse(
82 name = "Count not found",
83 message = errorMessage(),
84 httpStatus = HttpStatus.NOT_FOUND
88 logger.info { "Returned number of received messages: ${it.messagesCount}" }
90 Responses.stringResponse(
91 message = it.messagesCount.toString(),
92 httpStatus = HttpStatus.OK
96 }.let(res::sendAndHandleErrors)
98 .post("/messages/{$TOPIC_PARAM_KEY}/validate") { req, res ->
100 .receive().aggregate().asInputStream()
101 .map { inputStream ->
102 doWithTopicOrReturnInternalErrorResponse(req) {
103 logger.info { "Processing request for message validation" }
104 simulator.validate(inputStream, it)
105 .map(::resolveValidationResponse)
108 .flatMap(res::sendAndHandleErrors)
110 .get("/healthcheck") { _, res ->
111 val status = HttpConstants.STATUS_OK
112 logger.info { "Healthcheck OK, returning status: $status" }
113 res.status(status).send()
117 private fun doWithTopicOrReturnInternalErrorResponse(req: HttpServerRequest,
118 topicConsumer: (String) -> Mono<Response>) =
119 Option.fromNullable(req.param(TOPIC_PARAM_KEY))
122 stringResponse("Failed to retrieve parameter from url",
123 HttpStatus.INTERNAL_SERVER_ERROR))
126 private fun resolveValidationResponse(isValid: Boolean): Response =
128 logger.info { "Comparison result: $VALID_RESPONSE_MESSAGE" }
131 logger.info { "Comparison result: $INVALID_RESPONSE_MESSAGE" }
137 private val logger = Logger(DcaeAppApiServer::class)
138 private const val VALID_RESPONSE_MESSAGE = "validation passed"
139 private const val INVALID_RESPONSE_MESSAGE = "consumed messages don't match data from validation request"
140 private const val COUNT_NOT_RESOLVED_MESSAGE = "Error - number of messages could not be specified"
141 private const val TOPIC_PARAM_KEY = "topic"
143 private val responseValid by lazy {
144 Responses.statusResponse(
146 message = DcaeAppApiServer.VALID_RESPONSE_MESSAGE
150 private val responseInvalid by lazy {
151 Responses.statusResponse(
153 message = DcaeAppApiServer.INVALID_RESPONSE_MESSAGE,
154 httpStatus = HttpStatus.BAD_REQUEST