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