X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=hv-collector-client-simulator%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fdcae%2Fcollectors%2Fveshv%2Fsimulators%2Fxnf%2Fimpl%2FHttpServer.kt;fp=hv-collector-client-simulator%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fdcae%2Fcollectors%2Fveshv%2Fsimulators%2Fxnf%2Fimpl%2FHttpServer.kt;h=bc1cff7ca95a6a89452197377f9bf926bd9ce92c;hb=85a59b8d29c6f81720fe3d2e59926740173fcae9;hp=0000000000000000000000000000000000000000;hpb=ecf7cb09d4bce389b615257d3323ada0840100e8;p=dcaegen2%2Fcollectors%2Fhv-ves.git 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 new file mode 100644 index 00000000..bc1cff7c --- /dev/null +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/HttpServer.kt @@ -0,0 +1,96 @@ +/* + * ============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 org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import ratpack.handling.Chain +import ratpack.handling.Context +import ratpack.server.RatpackServer +import ratpack.server.ServerConfig +import reactor.core.publisher.Mono +import javax.json.Json +import javax.json.JsonObject + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +class HttpServer(private val vesClient: VesHvClient) { + + fun start(port: Int = DEFAULT_PORT): Mono = Mono.fromCallable { + RatpackServer.of { + it.serverConfig(ServerConfig.embedded().port(port)).handlers(this::configureHandlers) + } + }.doOnNext { it.start() } + + + private fun configureHandlers(chain: Chain) { + chain.post("simulator") { ctx -> + ctx.request.body + .map { Json.createReader(it.inputStream).readObject() } + .map { extractMessageParameters(it) } + .map { MessageFactory.createMessageFlux(it) } + .onError { handleException(it, ctx) } + .then { + vesClient.send(it) + 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 = MessageFactory + .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)