Merge "Extract transforming logic from validator"
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-domain / src / main / kotlin / org / onap / dcae / collectors / veshv / domain / logging / ClientContext.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-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.domain.logging
21
22 import arrow.core.None
23 import arrow.core.Option
24 import arrow.core.getOrElse
25 import io.netty.buffer.ByteBufAllocator
26 import java.net.InetAddress
27 import java.security.cert.X509Certificate
28 import java.util.*
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since December 2018
33  */
34 data class ClientContext(
35         val alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT,
36         var clientAddress: Option<InetAddress> = None,
37         var clientCert: Option<X509Certificate> = None,
38         val requestId: String = UUID.randomUUID().toString(), // Should be somehow propagated to DMAAP
39         val invocationId: String = UUID.randomUUID().toString()) {
40
41     val mdc: Map<String, String>
42         get() = mapOf(
43                 OnapMdc.REQUEST_ID to requestId,
44                 OnapMdc.INVOCATION_ID to invocationId,
45                 OnapMdc.STATUS_CODE to DEFAULT_STATUS_CODE,
46                 OnapMdc.CLIENT_NAME to clientDn().getOrElse { DEFAULT_VALUE },
47                 OnapMdc.CLIENT_IP to clientIp().getOrElse { DEFAULT_VALUE }
48         )
49
50     val fullMdc: Map<String, String>
51         get() = mdc + ServiceContext.mdc
52
53     private fun clientDn(): Option<String> = clientCert.map { it.subjectX500Principal.toString() }
54     private fun clientIp(): Option<String> = clientAddress.map(InetAddress::getHostAddress)
55
56     companion object {
57         const val DEFAULT_STATUS_CODE = "INPROGRESS"
58         const val DEFAULT_VALUE = ""
59     }
60 }