Add log diagnostic context
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-dcae-app-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / dcaeapp / impl / adapters / DcaeAppApiServer.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.simulators.dcaeapp.impl.adapters
21
22 import arrow.effects.IO
23 import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.DcaeAppSimulator
24 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
25 import org.onap.dcae.collectors.veshv.utils.http.HttpStatus
26 import org.onap.dcae.collectors.veshv.utils.http.Responses
27 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
28 import org.onap.dcae.collectors.veshv.utils.http.sendOrError
29 import org.onap.dcae.collectors.veshv.utils.logging.Logger
30 import ratpack.handling.Chain
31 import ratpack.server.RatpackServer
32 import ratpack.server.ServerConfig
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
36  * @since May 2018
37  */
38 class DcaeAppApiServer(private val simulator: DcaeAppSimulator) {
39     private val responseValid by lazy {
40         Responses.statusResponse(
41                 name = "valid",
42                 message = VALID_RESPONSE_MESSAGE
43         )
44     }
45
46     private val responseInvalid by lazy {
47         Responses.statusResponse(
48                 name = "invalid",
49                 message = INVALID_RESPONSE_MESSAGE,
50                 httpStatus = HttpStatus.BAD_REQUEST
51         )
52     }
53
54
55     fun start(port: Int, kafkaTopics: Set<String>): IO<RatpackServer> =
56             simulator.listenToTopics(kafkaTopics).map {
57                 RatpackServer.start { server ->
58                     server.serverConfig(ServerConfig.embedded().port(port))
59                             .handlers(::setupHandlers)
60                 }
61             }
62
63     private fun setupHandlers(chain: Chain) {
64         chain
65                 .put("configuration/topics") { ctx ->
66                     ctx.request.body.then { body ->
67                         val operation = simulator.listenToTopics(body.text)
68                         ctx.response.sendOrError(operation)
69                     }
70
71                 }
72                 .delete("messages") { ctx ->
73                     ctx.response.contentType(CONTENT_TEXT)
74                     logger.info { "Resetting simulator state" }
75                     ctx.response.sendOrError(simulator.resetState())
76                 }
77                 .get("messages/all/count") { ctx ->
78                     logger.info { "Processing request for count of received messages" }
79                     simulator.state().fold(
80                             {
81                                 ctx.response.status(HttpConstants.STATUS_NOT_FOUND)
82                                 logger.warn { "Error - number of messages could not be specified" }
83                             },
84                             {
85                                 logger.info { "Returned number of received messages: ${it.messagesCount}" }
86                                 ctx.response
87                                         .contentType(CONTENT_TEXT)
88                                         .send(it.messagesCount.toString())
89                             })
90                 }
91                 .post("messages/all/validate") { ctx ->
92                     ctx.request.body.then { body ->
93                         logger.info { "Processing request for message validation" }
94                         val response = simulator.validate(body.inputStream)
95                                 .map { isValid ->
96                                     if (isValid) {
97                                         logger.info { "Comparison result: $VALID_RESPONSE_MESSAGE" }
98                                         responseValid
99                                     } else {
100                                         logger.info { "Comparison result: $INVALID_RESPONSE_MESSAGE" }
101                                         responseInvalid
102                                     }
103                                 }
104                         ctx.response.sendAndHandleErrors(response)
105                     }
106                 }
107                 .get("healthcheck") { ctx ->
108                     val status = HttpConstants.STATUS_OK
109                     logger.info { "Healthcheck OK, returning status: $status" }
110                     ctx.response.status(status).send()
111                 }
112     }
113
114     companion object {
115         private const val CONTENT_TEXT = "text/plain"
116         private const val VALID_RESPONSE_MESSAGE = "validation passed"
117         private const val INVALID_RESPONSE_MESSAGE = "consumed messages don't match data from validation request"
118         private val logger = Logger(DcaeAppApiServer::class)
119     }
120 }
121