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.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
 
  41  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
 
  44 internal class XnfApiServer(
 
  45         private val xnfSimulator: XnfSimulator,
 
  46         private val ongoingSimulations: OngoingSimulations) {
 
  48     fun start(port: Int): IO<RatpackServer> = IO {
 
  49         RatpackServer.start { server ->
 
  50             server.serverConfig(ServerConfig.embedded().port(port))
 
  51                     .handlers(this::configureHandlers)
 
  55     private fun configureHandlers(chain: 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()
 
  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)
 
  74     private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
 
  75         return xnfSimulator.startSimulation(body.inputStream)
 
  76                 .map(ongoingSimulations::startAsynchronousSimulation)
 
  77                 .map(Responses::acceptedResponse)
 
  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))
 
  88         private val logger = Logger(XnfApiServer::class)