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
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.utils.NettyServerHandle
27 import org.onap.dcae.collectors.veshv.utils.ServerHandle
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 reactor.core.publisher.Mono
35 import reactor.netty.http.server.HttpServer
36 import reactor.netty.http.server.HttpServerRequest
37 import reactor.netty.http.server.HttpServerResponse
38 import reactor.netty.http.server.HttpServerRoutes
39 import java.io.InputStream
40 import java.net.InetSocketAddress
44 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
47 internal class XnfApiServer(
48 private val xnfSimulator: XnfSimulator,
49 private val ongoingSimulations: OngoingSimulations) {
51 fun start(socketAddress: InetSocketAddress): IO<ServerHandle> = IO {
53 .host(socketAddress.hostName)
54 .port(socketAddress.port)
56 .let { NettyServerHandle(it.bindNow()) }
59 private fun setRoutes(route: HttpServerRoutes) {
61 .post("/simulator", ::startSimulationHandler)
62 .post("/simulator/async", ::startSimulationHandler)
63 .get("/simulator/:id", ::simulatorStatusHandler)
66 private fun startSimulationHandler(req: HttpServerRequest, res: HttpServerResponse): Mono<Void> {
67 logger.info { "Attempting to start asynchronous scenario" }
68 return req.receive().aggregate().asInputStream()
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 res.sendEitherErrorOrResponse(id)
80 private fun startSimulation(body: InputStream): Either<ParsingError, Response> =
81 xnfSimulator.startSimulation(body)
82 .map(ongoingSimulations::startAsynchronousSimulation)
83 .map(Responses::acceptedResponse)
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))
97 private val logger = Logger(XnfApiServer::class)