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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.model
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
32 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
33 * @since December 2018
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()) {
42 val mdc: Map<String, String>
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 }
51 val fullMdc: Map<String, String>
52 get() = mdc + ServiceContext.mdc
54 private fun clientDn(): Option<String> = clientCert.map { it.subjectX500Principal.toString() }
55 private fun clientIp(): Option<String> = clientAddress.map(InetAddress::getHostAddress)
58 const val DEFAULT_STATUS_CODE = "INPROGRESS"
59 const val DEFAULT_VALUE = ""