54ead6f754e1486884617d2b7c5849ec0f9ad5cf
[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.Status
26 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator
27 import org.onap.dcae.collectors.veshv.utils.http.Content
28 import org.onap.dcae.collectors.veshv.utils.http.ContentType
29 import org.onap.dcae.collectors.veshv.utils.http.HttpConstants
30 import org.onap.dcae.collectors.veshv.utils.http.HttpStatus
31 import org.onap.dcae.collectors.veshv.utils.http.Response
32 import org.onap.dcae.collectors.veshv.utils.http.Responses
33 import org.onap.dcae.collectors.veshv.utils.http.sendAndHandleErrors
34 import org.onap.dcae.collectors.veshv.utils.http.sendEitherErrorOrResponse
35 import org.onap.dcae.collectors.veshv.utils.logging.Logger
36 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
37 import ratpack.handling.Chain
38 import ratpack.handling.Context
39 import ratpack.http.TypedData
40 import ratpack.server.RatpackServer
41 import ratpack.server.ServerConfig
42 import java.util.*
43 import javax.json.Json
44
45 /**
46  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
47  * @since June 2018
48  */
49 internal class XnfApiServer(
50         private val xnfSimulator: XnfSimulator,
51         private val ongoingSimulations: OngoingSimulations) {
52
53     fun start(port: Int): IO<RatpackServer> = IO {
54         RatpackServer.start { server ->
55             server.serverConfig(ServerConfig.embedded().port(port))
56                     .handlers(this::configureHandlers)
57         }
58     }
59
60     private fun configureHandlers(chain: Chain) {
61         chain
62                 .post("simulator", ::startSimulationHandler)
63                 .post("simulator/async", ::startSimulationHandler)
64                 .get("simulator/:id", ::simulatorStatusHandler)
65                 .get("healthcheck") { ctx ->
66                     logger.info("Checking health")
67                     ctx.response.status(HttpConstants.STATUS_OK).send()
68                 }
69     }
70
71     private fun startSimulationHandler(ctx: Context) {
72         logger.info("Starting asynchronous scenario")
73         ctx.request.body.then { body ->
74             val id = startSimulation(body)
75             ctx.response.sendEitherErrorOrResponse(id)
76         }
77     }
78
79     private fun startSimulation(body: TypedData): Either<ParsingError, Response> {
80         return xnfSimulator.startSimulation(body.inputStream)
81                 .map(ongoingSimulations::startAsynchronousSimulation)
82                 .map(Responses::acceptedResponse)
83     }
84
85     private fun simulatorStatusHandler(ctx: Context) {
86         val id = UUID.fromString(ctx.pathTokens["id"])
87         val status = ongoingSimulations.status(id)
88         val response = Responses.statusResponse(status.toString(), status.message)
89         ctx.response.sendAndHandleErrors(IO.just(response))
90     }
91
92     companion object {
93         private val logger = Logger(XnfApiServer::class)
94     }
95 }