Get rid of arrow-effects usage
[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-2019 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 org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
24 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
25 import org.onap.dcae.collectors.veshv.utils.NettyServerHandle
26 import org.onap.dcae.collectors.veshv.utils.ServerHandle
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 reactor.core.publisher.Mono
34 import reactor.netty.http.server.HttpServer
35 import reactor.netty.http.server.HttpServerRequest
36 import reactor.netty.http.server.HttpServerResponse
37 import reactor.netty.http.server.HttpServerRoutes
38 import java.io.InputStream
39 import java.net.InetSocketAddress
40 import java.util.*
41
42 /**
43  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
44  * @since June 2018
45  */
46 internal class XnfApiServer(
47         private val xnfSimulator: XnfSimulator,
48         private val ongoingSimulations: OngoingSimulations) {
49
50     fun start(socketAddress: InetSocketAddress): Mono<ServerHandle> =
51             HttpServer.create()
52                     .host(socketAddress.hostName)
53                     .port(socketAddress.port)
54                     .route(::setRoutes)
55                     .bind()
56                     .map { NettyServerHandle(it) }
57
58
59     private fun setRoutes(route: HttpServerRoutes) {
60         route
61                 .post("/simulator", ::startSimulationHandler)
62                 .post("/simulator/async", ::startSimulationHandler)
63                 .get("/simulator/:id", ::simulatorStatusHandler)
64     }
65
66     private fun startSimulationHandler(req: HttpServerRequest, res: HttpServerResponse): Mono<Void> {
67         logger.info { "Attempting to start asynchronous scenario" }
68         return req.receive().aggregate().asInputStream()
69                 .flatMap { 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                     res.sendEitherErrorOrResponse(id)
76                 }
77     }
78
79
80     private fun startSimulation(body: InputStream): Either<ParsingError, Response> =
81             xnfSimulator.startSimulation(body)
82                     .map(ongoingSimulations::startAsynchronousSimulation)
83                     .map(Responses::acceptedResponse)
84
85
86     private fun simulatorStatusHandler(req: HttpServerRequest, res: HttpServerResponse): Mono<Void> {
87         logger.debug { "Checking task status" }
88         val id = UUID.fromString(req.param("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         return res.sendAndHandleErrors(Mono.just(response))
94     }
95
96     companion object {
97         private val logger = Logger(XnfApiServer::class)
98     }
99 }