Rename hv-collector-client-simulator
[dcaegen2/collectors/hv-ves.git] / hv-collector-client-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / xnf / impl / HttpServer.kt
diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt
deleted file mode 100644 (file)
index c545ac8..0000000
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * dcaegen2-collectors-veshv
- * ================================================================================
- * Copyright (C) 2018 NOKIA
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcae.collectors.veshv.simulators.xnf.impl
-
-import arrow.effects.IO
-import org.onap.dcae.collectors.veshv.domain.WireFrame
-import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters
-import org.onap.dcae.collectors.veshv.utils.logging.Logger
-import ratpack.exec.Promise
-import ratpack.handling.Chain
-import ratpack.handling.Context
-import ratpack.server.RatpackServer
-import ratpack.server.ServerConfig
-import reactor.core.publisher.Flux
-import reactor.core.scheduler.Schedulers
-import javax.json.Json
-import javax.json.JsonObject
-
-/**
- * @author Jakub Dudycz <jakub.dudycz@nokia.com>
- * @since June 2018
- */
-internal class HttpServer(private val vesClient: VesHvClient) {
-
-    fun start(port: Int = DEFAULT_PORT): IO<RatpackServer> = IO {
-        RatpackServer.start { server ->
-            server.serverConfig(ServerConfig.embedded().port(port))
-                    .handlers(this::configureHandlers)
-        }
-    }
-
-
-    private fun configureHandlers(chain: Chain) {
-        chain
-                .post("simulator/sync") { ctx ->
-                    createMessageFlux(ctx)
-                            .map { vesClient.sendIo(it) }
-                            .map { it.unsafeRunSync() }
-                            .onError { handleException(it, ctx) }
-                            .then { sendAcceptedResponse(ctx) }
-                }
-                .post("simulator/async") { ctx ->
-                    createMessageFlux(ctx)
-                            .map { vesClient.sendRx(it) }
-                            .map { it.subscribeOn(Schedulers.elastic()).subscribe() }
-                            .onError { handleException(it, ctx) }
-                            .then { sendAcceptedResponse(ctx) }
-                }
-    }
-
-    private fun createMessageFlux(ctx: Context): Promise<Flux<WireFrame>> {
-        return ctx.request.body
-                .map { Json.createReader(it.inputStream).readObject() }
-                .map { extractMessageParameters(it) }
-                .map { MessageGeneratorImpl.INSTANCE.createMessageFlux(it) }
-    }
-
-    private fun sendAcceptedResponse(ctx: Context) {
-        ctx.response
-                .status(STATUS_OK)
-                .send(CONTENT_TYPE_APPLICATION_JSON, Json.createObjectBuilder()
-                        .add("response", "Request accepted")
-                        .build()
-                        .toString())
-    }
-
-    private fun handleException(t: Throwable, ctx: Context) {
-        logger.warn("Failed to process the request - ${t.localizedMessage}")
-        logger.debug("Exception thrown when processing the request", t)
-        ctx.response
-                .status(STATUS_BAD_REQUEST)
-                .send(CONTENT_TYPE_APPLICATION_JSON, Json.createObjectBuilder()
-                        .add("response", "Request was not accepted")
-                        .add("exception", t.localizedMessage)
-                        .build()
-                        .toString())
-    }
-
-    private fun extractMessageParameters(request: JsonObject): MessageParameters =
-            try {
-                val commonEventHeader = MessageGeneratorImpl.INSTANCE
-                        .parseCommonHeader(request.getJsonObject("commonEventHeader"))
-                val messagesAmount = request.getJsonNumber("messagesAmount").longValue()
-                MessageParameters(commonEventHeader, messagesAmount)
-            } catch (e: Exception) {
-                throw ValidationException("Validating request body failed", e)
-            }
-
-
-    companion object {
-        private val logger = Logger(HttpServer::class)
-        const val DEFAULT_PORT = 5000
-        const val STATUS_OK = 200
-        const val STATUS_BAD_REQUEST = 400
-        const val CONTENT_TYPE_APPLICATION_JSON = "application/json"
-    }
-}
-
-internal class ValidationException(message: String?, cause: Exception) : Exception(message, cause)