654f16a6585bcfb178f1450a71803f5a56027311
[dcaegen2/collectors/hv-ves.git] /
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.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.Response
27 import org.onap.dcae.collectors.veshv.utils.http.Responses
28 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
29 import org.onap.dcae.collectors.veshv.utils.http.sendEitherErrorOrResponse
30 import org.onap.dcae.collectors.veshv.utils.logging.Logger
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
32 import ratpack.handling.Chain
33 import ratpack.handling.Context
34 import ratpack.http.TypedData
35 import ratpack.server.RatpackServer
36 import ratpack.server.ServerConfig
37 import java.net.InetSocketAddress
38 import java.util.*
39
40 /**
41  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42  * @since June 2018
43  */
44 internal class XnfApiServer(
45         private val xnfSimulator: XnfSimulator,
46         private val ongoingSimulations: OngoingSimulations) {
47
48     fun start(socketAddress: InetSocketAddress): IO<RatpackServer> = IO {
49         RatpackServer.start { server ->
50             server.serverConfig(ServerConfig.embedded()
51                     .port(socketAddress.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     }
62
63     private fun startSimulationHandler(ctx: Context) {
64         logger.info { "Attempting to start asynchronous scenario" }
65         ctx.request.body.then { body ->
66             val id = startSimulation(body)
67             when (id) {
68                 is Either.Left -> logger.warn { "Failed to start scenario, ${id.a}" }
69                 is Either.Right -> logger.info { "Scenario started, details: ${id.b}" }
70             }
71             ctx.response.sendEitherErrorOrResponse(id)
72         }
73     }
74
75     private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
76         return xnfSimulator.startSimulation(body.inputStream)
77                 .map(ongoingSimulations::startAsynchronousSimulation)
78                 .map(Responses::acceptedResponse)
79     }
80
81
82     private fun simulatorStatusHandler(ctx: Context) {
83         logger.debug { "Checking task status" }
84         val id = UUID.fromString(ctx.pathTokens["id"])
85         logger.debug { "Checking status for id: $id" }
86         val status = ongoingSimulations.status(id)
87         val response = Responses.statusResponse(status.toString(), status.message)
88         logger.info { "Task $id status: $response" }
89         ctx.response.sendAndHandleErrors(IO.just(response))
90     }
91
92     companion object {
93         private val logger = Logger(XnfApiServer::class)
94     }
95 }