Custom detekt rule for logger usage check
[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 ratpack.handling.Chain
30 import ratpack.server.RatpackServer
31 import ratpack.server.ServerConfig
32
33 /**
34  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
35  * @since May 2018
36  */
37 class DcaeAppApiServer(private val simulator: DcaeAppSimulator) {
38     private val responseValid by lazy {
39         Responses.statusResponse(
40                 name = "valid",
41                 message = "validation succeeded"
42         )
43     }
44
45     private val responseInvalid by lazy {
46         Responses.statusResponse(
47                 name = "invalid",
48                 message = "validation failed",
49                 httpStatus = HttpStatus.BAD_REQUEST
50         )
51     }
52
53
54     fun start(port: Int, kafkaTopics: Set<String>): IO<RatpackServer> =
55             simulator.listenToTopics(kafkaTopics).map {
56                 RatpackServer.start { server ->
57                     server.serverConfig(ServerConfig.embedded().port(port))
58                             .handlers(::setupHandlers)
59                 }
60             }
61
62     private fun setupHandlers(chain: Chain) {
63         chain
64                 .put("configuration/topics") { ctx ->
65                     ctx.request.body.then { body ->
66                         val operation = simulator.listenToTopics(body.text)
67                         ctx.response.sendOrError(operation)
68                     }
69
70                 }
71                 .delete("messages") { ctx ->
72                     ctx.response.contentType(CONTENT_TEXT)
73                     ctx.response.sendOrError(simulator.resetState())
74                 }
75                 .get("messages/all/count") { ctx ->
76                     simulator.state().fold(
77                             { ctx.response.status(HttpConstants.STATUS_NOT_FOUND) },
78                             {
79                                 ctx.response
80                                         .contentType(CONTENT_TEXT)
81                                         .send(it.messagesCount.toString())
82                             })
83                 }
84                 .post("messages/all/validate") { ctx ->
85                     ctx.request.body.then { body ->
86                         val response = simulator.validate(body.inputStream)
87                                 .map { isValid ->
88                                     if (isValid) responseValid else responseInvalid
89                                 }
90                         ctx.response.sendAndHandleErrors(response)
91                     }
92                 }
93                 .get("healthcheck") { ctx ->
94                     ctx.response.status(HttpConstants.STATUS_OK).send()
95                 }
96     }
97
98     companion object {
99         private const val CONTENT_TEXT = "text/plain"
100     }
101 }