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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters
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.simulators.xnf.impl.XnfStatus.BUSY
27 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfStatus.DETAILED_STATUS_NODE
28 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfStatus.IDLE
29 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
30 import org.onap.dcae.collectors.veshv.utils.http.Response
31 import org.onap.dcae.collectors.veshv.utils.http.Responses
32 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
33 import org.onap.dcae.collectors.veshv.utils.http.sendEitherErrorOrResponse
34 import org.onap.dcae.collectors.veshv.utils.logging.Logger
35 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
36 import ratpack.handling.Chain
37 import ratpack.handling.Context
38 import ratpack.http.TypedData
39 import ratpack.server.RatpackServer
40 import ratpack.server.ServerConfig
42 import javax.json.Json
45 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
48 internal class XnfApiServer(
49 private val xnfSimulator: XnfSimulator,
50 private val ongoingSimulations: OngoingSimulations) {
52 fun start(port: Int): IO<RatpackServer> = IO {
53 RatpackServer.start { server ->
54 server.serverConfig(ServerConfig.embedded().port(port))
55 .handlers(this::configureHandlers)
59 private fun configureHandlers(chain: Chain) {
61 .post("simulator", ::startSimulationHandler)
62 .post("simulator/async", ::startSimulationHandler)
63 .get("simulator/:id", ::simulatorStatusHandler)
64 .get("healthcheck", ::healthcheckHandler)
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)
72 is Either.Left -> logger.warn { "Failed to start scenario, ${id.a}" }
73 is Either.Right -> logger.info { "Scenario started, details: ${id.b}" }
75 ctx.response.sendEitherErrorOrResponse(id)
79 private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
80 return xnfSimulator.startSimulation(body.inputStream)
81 .map(ongoingSimulations::startAsynchronousSimulation)
82 .map(Responses::acceptedResponse)
86 private fun simulatorStatusHandler(ctx: Context) {
87 logger.debug { "Checking task status" }
88 val id = UUID.fromString(ctx.pathTokens["id"])
89 logger.debug { "Checking status for id: $id" }
90 val status = ongoingSimulations.status(id)
91 val response = Responses.statusResponse(status.toString(), status.message)
92 logger.info { "Task $id status: $response" }
93 ctx.response.sendAndHandleErrors(IO.just(response))
96 private fun healthcheckHandler(ctx: Context) {
97 val healthCheckDetailedMessage = createHealthCheckDetailedMessage()
98 val simulatorStatus = HttpConstants.STATUS_OK
99 logger.info { "Returning simulator status: ${simulatorStatus} ${healthCheckDetailedMessage}" }
100 ctx.response.status(simulatorStatus).send(healthCheckDetailedMessage)
103 private fun createHealthCheckDetailedMessage() =
104 Json.createObjectBuilder()
105 .add(DETAILED_STATUS_NODE, when {
106 ongoingSimulations.isAnySimulationPending() -> BUSY
112 private val logger = Logger(XnfApiServer::class)