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