160193844694d5c31e6dce19b1886be19fc391ef
[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.core.getOrElse
24 import arrow.effects.IO
25 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
26 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
27 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
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.sendAndHandleErrors
31 import org.onap.dcae.collectors.veshv.utils.http.sendEitherErrorOrResponse
32 import org.onap.dcae.collectors.veshv.utils.logging.Logger
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
34 import ratpack.handling.Chain
35 import ratpack.handling.Context
36 import ratpack.http.TypedData
37 import ratpack.server.RatpackServer
38 import ratpack.server.ServerConfig
39 import java.util.*
40
41 /**
42  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
43  * @since June 2018
44  */
45 internal class XnfApiServer(
46         private val xnfSimulator: XnfSimulator,
47         private val ongoingSimulations: OngoingSimulations) {
48
49     fun start(port: Int): IO<RatpackServer> = IO {
50         RatpackServer.start { server ->
51             server.serverConfig(ServerConfig.embedded().port(port))
52                     .handlers(this::configureHandlers)
53         }
54     }
55
56     private fun configureHandlers(chain: Chain) {
57         chain
58                 .post("simulator", ::startSimulationHandler)
59                 .post("simulator/async", ::startSimulationHandler)
60                 .get("simulator/:id", ::simulatorStatusHandler)
61                 .get("healthcheck") { ctx ->
62                     logger.info("Checking health")
63                     ctx.response.status(HttpConstants.STATUS_OK).send()
64                 }
65     }
66
67     private fun startSimulationHandler(ctx: Context) {
68         logger.info("Attempting to start asynchronous scenario")
69         ctx.request.body.then { body ->
70             val id = startSimulation(body)
71             when (id) {
72                 is Either.Left -> logger.warn { "Failed to start scenario, ${id.a}"}
73                 is Either.Right -> logger.info { "Scenario started, details: ${id.b}" }
74             }
75             ctx.response.sendEitherErrorOrResponse(id)
76         }
77     }
78
79     private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
80         return xnfSimulator.startSimulation(body.inputStream)
81                 .map(ongoingSimulations::startAsynchronousSimulation)
82                 .map(Responses::acceptedResponse)
83     }
84
85     private fun simulatorStatusHandler(ctx: Context) {
86         logger.debug("Checking task status")
87         val id = UUID.fromString(ctx.pathTokens["id"])
88         logger.debug { "Checking status for id: $id" }
89         val status = ongoingSimulations.status(id)
90         val response = Responses.statusResponse(status.toString(), status.message)
91         logger.info { "Task $id status: $response" }
92         ctx.response.sendAndHandleErrors(IO.just(response))
93     }
94
95     companion object {
96         private val logger = Logger(XnfApiServer::class)
97     }
98 }