[DCAE] INFO.yaml update
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / utils / http / netty.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 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.utils.http
21
22 import arrow.core.Either
23 import org.onap.dcae.collectors.veshv.utils.logging.Logger
24 import reactor.core.publisher.Mono
25 import reactor.core.publisher.toMono
26 import reactor.netty.NettyOutbound
27 import reactor.netty.http.server.HttpServerResponse
28 import javax.json.Json
29
30 private val logger = Logger("org.onap.dcae.collectors.veshv.utils.http.netty")
31
32 fun HttpServerResponse.sendOrError(action: ()->Unit) = sendAndHandleErrors(
33         Mono
34                 .fromSupplier(action)
35                 .map {
36                     Response(
37                             HttpStatus.OK,
38                             Content(
39                                     ContentType.JSON,
40                                     Json.createObjectBuilder().add("response", "Request accepted").build()
41                             )
42                     )
43                 }
44 )
45
46 fun HttpServerResponse.sendAndHandleErrors(response: Mono<Response>) =
47         response
48                 .onErrorResume {
49                     logger.withWarn { log("Error occurred. Sending .", it) }
50                     errorResponse(it.localizedMessage).toMono()
51                 }
52                 .flatMap {
53                     sendResponse(it).then()
54                 }
55
56 fun <A> HttpServerResponse.sendEitherErrorOrResponse(response: Either<A, Response>) =
57         when (response) {
58             is Either.Left -> sendResponse(errorResponse(response.a.toString())).then()
59             is Either.Right -> sendAndHandleErrors(Mono.just(response.b))
60         }
61
62
63 fun HttpServerResponse.sendResponse(response: Response): NettyOutbound {
64     val respWithStatus = status(response.status.number)
65     val responseContent = response.content
66
67     return respWithStatus.sendString(
68             Mono.just(responseContent.serializer.run { responseContent.value.show() })
69     )
70 }
71
72 private fun errorResponse(message: String?): Response =
73         Response(
74                 HttpStatus.INTERNAL_SERVER_ERROR,
75                 Content(
76                         ContentType.JSON,
77                         Json.createObjectBuilder().add("error", message).build()
78                 )
79         )