Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / nats-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / nats / service / BluePrintNatsLibPropertyService.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.nats.service
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
21 import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsConnectionProperties
22 import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants
23 import org.onap.ccsdk.cds.blueprintsprocessor.nats.TLSAuthNatsConnectionProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.nats.TokenAuthNatsConnectionProperties
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.springframework.stereotype.Service
28
29 @Service(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY)
30 open class BluePrintNatsLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
31
32     fun bluePrintNatsService(jsonNode: JsonNode): BluePrintNatsService {
33         val natsConnectionProperties = natsConnectionProperties(jsonNode)
34         return bluePrintNatsService(natsConnectionProperties)
35     }
36
37     fun bluePrintNatsService(selector: String): BluePrintNatsService {
38         val prefix = "${NatsLibConstants.PROPERTY_NATS_PREFIX}$selector"
39         val natsConnectionProperties = natsConnectionProperties(prefix)
40         return bluePrintNatsService(natsConnectionProperties)
41     }
42
43     /** NATS Lib Property Service */
44     fun natsConnectionProperties(jsonNode: JsonNode): NatsConnectionProperties {
45         return when (val type = jsonNode.get("type").textValue()) {
46             NatsLibConstants.TYPE_TOKEN_AUTH -> {
47                 JacksonUtils.readValue(jsonNode, TokenAuthNatsConnectionProperties::class.java)!!
48             }
49             NatsLibConstants.TYPE_TLS_AUTH -> {
50                 JacksonUtils.readValue(jsonNode, TLSAuthNatsConnectionProperties::class.java)!!
51             }
52             else -> {
53                 throw BluePrintProcessorException("NATS type($type) not supported")
54             }
55         }
56     }
57
58     fun natsConnectionProperties(prefix: String): NatsConnectionProperties {
59         val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
60         return when (type) {
61             NatsLibConstants.TYPE_TOKEN_AUTH -> {
62                 tokenAuthNatsConnectionProperties(prefix)
63             }
64             NatsLibConstants.TYPE_TLS_AUTH -> {
65                 tlsAuthNatsConnectionProperties(prefix)
66             }
67             else -> {
68                 throw BluePrintProcessorException("NATS type($type) not supported")
69             }
70         }
71     }
72
73     private fun tokenAuthNatsConnectionProperties(prefix: String): TokenAuthNatsConnectionProperties {
74         return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthNatsConnectionProperties::class.java)
75     }
76
77     private fun tlsAuthNatsConnectionProperties(prefix: String): TLSAuthNatsConnectionProperties {
78         return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthNatsConnectionProperties::class.java)
79     }
80
81     fun bluePrintNatsService(natsConnectionProperties: NatsConnectionProperties):
82         BluePrintNatsService {
83             return when (natsConnectionProperties) {
84                 is TokenAuthNatsConnectionProperties -> {
85                     TokenAuthNatsService(natsConnectionProperties)
86                 }
87                 is TLSAuthNatsConnectionProperties -> {
88                     TLSAuthNatsService(natsConnectionProperties)
89                 }
90                 else -> {
91                     throw BluePrintProcessorException("couldn't get NATS service for properties $natsConnectionProperties")
92                 }
93             }
94         }
95 }