Custom detekt rule for logger usage check
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-xnf-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / xnf / impl / adapters / XnfApiServer.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.xnf.impl.adapters
21
22 import arrow.core.Either
23 import arrow.effects.IO
24 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
25 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
26 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
27 import org.onap.dcae.collectors.veshv.utils.http.Response
28 import org.onap.dcae.collectors.veshv.utils.http.Responses
29 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
30 import org.onap.dcae.collectors.veshv.utils.http.sendEitherErrorOrResponse
31 import org.onap.dcae.collectors.veshv.utils.logging.Logger
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
33 import ratpack.handling.Chain
34 import ratpack.handling.Context
35 import ratpack.http.TypedData
36 import ratpack.server.RatpackServer
37 import ratpack.server.ServerConfig
38 import java.util.*
39
40 /**
41  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42  * @since June 2018
43  */
44 internal class XnfApiServer(
45         private val xnfSimulator: XnfSimulator,
46         private val ongoingSimulations: OngoingSimulations) {
47
48     fun start(port: Int): IO<RatpackServer> = IO {
49         RatpackServer.start { server ->
50             server.serverConfig(ServerConfig.embedded().port(port))
51                     .handlers(this::configureHandlers)
52         }
53     }
54
55     private fun configureHandlers(chain: Chain) {
56         chain
57                 .post("simulator", ::startSimulationHandler)
58                 .post("simulator/async", ::startSimulationHandler)
59                 .get("simulator/:id", ::simulatorStatusHandler)
60                 .get("healthcheck") { ctx ->
61                     logger.info("Checking health")
62                     ctx.response.status(HttpConstants.STATUS_OK).send()
63                 }
64     }
65
66     private fun startSimulationHandler(ctx: Context) {
67         logger.info("Starting asynchronous scenario")
68         ctx.request.body.then { body ->
69             val id = startSimulation(body)
70             ctx.response.sendEitherErrorOrResponse(id)
71         }
72     }
73
74     private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
75         return xnfSimulator.startSimulation(body.inputStream)
76                 .map(ongoingSimulations::startAsynchronousSimulation)
77                 .map(Responses::acceptedResponse)
78     }
79
80     private fun simulatorStatusHandler(ctx: Context) {
81         val id = UUID.fromString(ctx.pathTokens["id"])
82         val status = ongoingSimulations.status(id)
83         val response = Responses.statusResponse(status.toString(), status.message)
84         ctx.response.sendAndHandleErrors(IO.just(response))
85     }
86
87     companion object {
88         private val logger = Logger(XnfApiServer::class)
89     }
90 }