2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.nats.service
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
29 @Service(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY)
30 open class BluePrintNatsLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
32 fun bluePrintNatsService(jsonNode: JsonNode): BluePrintNatsService {
33 val natsConnectionProperties = natsConnectionProperties(jsonNode)
34 return bluePrintNatsService(natsConnectionProperties)
37 fun bluePrintNatsService(selector: String): BluePrintNatsService {
38 val prefix = "${NatsLibConstants.PROPERTY_NATS_PREFIX}$selector"
39 val natsConnectionProperties = natsConnectionProperties(prefix)
40 return bluePrintNatsService(natsConnectionProperties)
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)!!
49 NatsLibConstants.TYPE_TLS_AUTH -> {
50 JacksonUtils.readValue(jsonNode, TLSAuthNatsConnectionProperties::class.java)!!
53 throw BluePrintProcessorException("NATS type($type) not supported")
58 fun natsConnectionProperties(prefix: String): NatsConnectionProperties {
59 val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
61 NatsLibConstants.TYPE_TOKEN_AUTH -> {
62 tokenAuthNatsConnectionProperties(prefix)
64 NatsLibConstants.TYPE_TLS_AUTH -> {
65 tlsAuthNatsConnectionProperties(prefix)
68 throw BluePrintProcessorException("NATS type($type) not supported")
73 private fun tokenAuthNatsConnectionProperties(prefix: String): TokenAuthNatsConnectionProperties {
74 return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthNatsConnectionProperties::class.java)
77 private fun tlsAuthNatsConnectionProperties(prefix: String): TLSAuthNatsConnectionProperties {
78 return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthNatsConnectionProperties::class.java)
81 fun bluePrintNatsService(natsConnectionProperties: NatsConnectionProperties):
82 BluePrintNatsService {
83 return when (natsConnectionProperties) {
84 is TokenAuthNatsConnectionProperties -> {
85 TokenAuthNatsService(natsConnectionProperties)
87 is TLSAuthNatsConnectionProperties -> {
88 TLSAuthNatsService(natsConnectionProperties)
91 throw BluePrintProcessorException("couldn't get NATS service for properties $natsConnectionProperties")