From adcd4f2bc695840e9ecbc05003bc52c675f22fec Mon Sep 17 00:00:00 2001 From: KAPIL SINGAL Date: Fri, 22 Jan 2021 11:49:51 -0500 Subject: Renaming Files having BluePrint to have Blueprint Replacing BluePrint with Blueprint throughout Issue-ID: CCSDK-3098 Signed-off-by: KAPIL SINGAL Change-Id: Ibee8bad07ae7d9287073db2d4f2f2cd730fa8b96 --- .../nats/BluePrintNatsExtensions.kt | 36 --- .../nats/BluePrintNatsLibConfiguration.kt | 49 ---- .../nats/BluePrintNatsLibData.kt | 45 ---- .../nats/BlueprintNatsExtensions.kt | 36 +++ .../nats/BlueprintNatsLibConfiguration.kt | 49 ++++ .../nats/BlueprintNatsLibData.kt | 45 ++++ .../blueprintsprocessor/nats/NatsPropertiesDSL.kt | 30 +-- .../service/BluePrintNatsLibPropertyService.kt | 95 -------- .../nats/service/BluePrintNatsService.kt | 143 ----------- .../service/BlueprintNatsLibPropertyService.kt | 95 ++++++++ .../nats/service/BlueprintNatsService.kt | 143 +++++++++++ .../nats/service/TLSAuthNatsService.kt | 2 +- .../nats/service/TokenAuthNatsService.kt | 2 +- .../nats/utils/NatsClusterUtils.kt | 6 +- .../nats/NatsPropertiesDSLTest.kt | 10 +- .../nats/service/BluePrintNatsExtensionsTest.kt | 51 ---- .../service/BluePrintNatsLibPropertyServiceTest.kt | 56 ----- .../nats/service/BluePrintNatsServiceTest.kt | 267 --------------------- .../nats/service/BlueprintNatsExtensionsTest.kt | 51 ++++ .../service/BlueprintNatsLibPropertyServiceTest.kt | 56 +++++ .../nats/service/BlueprintNatsServiceTest.kt | 267 +++++++++++++++++++++ 21 files changed, 767 insertions(+), 767 deletions(-) delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsExtensions.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibConfiguration.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibData.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsExtensions.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibConfiguration.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibData.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyService.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsService.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyService.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsService.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsExtensionsTest.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyServiceTest.kt delete mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsServiceTest.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsExtensionsTest.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyServiceTest.kt create mode 100644 ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsServiceTest.kt (limited to 'ms/blueprintsprocessor/modules/commons/nats-lib') diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsExtensions.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsExtensions.kt deleted file mode 100644 index a585c972b..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsExtensions.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats - -import com.fasterxml.jackson.databind.JsonNode -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType - -fun io.nats.client.Message.strData(): String { - return String(this.data) -} - -fun io.nats.streaming.Message.strData(): String { - return String(this.data) -} - -fun io.nats.client.Message.asJsonType(): JsonNode { - return this.data.asJsonType() -} - -fun io.nats.streaming.Message.asJsonType(): JsonNode { - return this.data.asJsonType() -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibConfiguration.kt deleted file mode 100644 index a47dd8918..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibConfiguration.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats - -import org.onap.ccsdk.cds.blueprintsprocessor.nats.service.BluePrintNatsLibPropertyService -import org.onap.ccsdk.cds.blueprintsprocessor.nats.service.BluePrintNatsService -import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService -import org.springframework.context.annotation.ComponentScan -import org.springframework.context.annotation.Configuration - -@Configuration -@ComponentScan -open class BluePrintNatsLibConfiguration - -/** - * Exposed Dependency Service by this NATS Lib Module - */ -fun BluePrintDependencyService.natsLibPropertyService(): BluePrintNatsLibPropertyService = - instance(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY) - -fun BluePrintDependencyService.controllerNatsService(): BluePrintNatsService { - return natsLibPropertyService().bluePrintNatsService(NatsLibConstants.DEFULT_NATS_SELECTOR) -} - -class NatsLibConstants { - companion object { - - const val SERVICE_BLUEPRINT_NATS_LIB_PROPERTY = "blueprint-nats-lib-property-service" - const val DEFULT_NATS_SELECTOR = "cds-controller" - const val PROPERTY_NATS_PREFIX = "blueprintsprocessor.nats." - const val PROPERTY_NATS_CLUSTER_ID = "NATS_CLUSTER_ID" - const val TYPE_TOKEN_AUTH = "token-auth" - const val TYPE_TLS_AUTH = "tls-auth" - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibData.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibData.kt deleted file mode 100644 index a989dcf1e..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BluePrintNatsLibData.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats - -import org.onap.ccsdk.cds.blueprintsprocessor.nats.utils.NatsClusterUtils -import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils - -open class NatsConnectionProperties { - - lateinit var type: String - var clusterId: String = NatsClusterUtils.clusterId() - var clientId: String = ClusterUtils.clusterNodeId() - lateinit var host: String - - /** Rest endpoint selector to access Monitoring API */ - var monitoringSelector: String? = null -} - -open class TokenAuthNatsConnectionProperties : NatsConnectionProperties() { - - lateinit var token: String -} - -open class TLSAuthNatsConnectionProperties : NatsConnectionProperties() { - - var trustCertCollection: String? = null - - /** Below Used only for Mutual TLS */ - var clientCertChain: String? = null - var clientPrivateKey: String? = null -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsExtensions.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsExtensions.kt new file mode 100644 index 000000000..a585c972b --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsExtensions.kt @@ -0,0 +1,36 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType + +fun io.nats.client.Message.strData(): String { + return String(this.data) +} + +fun io.nats.streaming.Message.strData(): String { + return String(this.data) +} + +fun io.nats.client.Message.asJsonType(): JsonNode { + return this.data.asJsonType() +} + +fun io.nats.streaming.Message.asJsonType(): JsonNode { + return this.data.asJsonType() +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibConfiguration.kt new file mode 100644 index 000000000..c83906c3f --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibConfiguration.kt @@ -0,0 +1,49 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats + +import org.onap.ccsdk.cds.blueprintsprocessor.nats.service.BlueprintNatsLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.nats.service.BlueprintNatsService +import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintDependencyService +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.Configuration + +@Configuration +@ComponentScan +open class BlueprintNatsLibConfiguration + +/** + * Exposed Dependency Service by this NATS Lib Module + */ +fun BlueprintDependencyService.natsLibPropertyService(): BlueprintNatsLibPropertyService = + instance(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY) + +fun BlueprintDependencyService.controllerNatsService(): BlueprintNatsService { + return natsLibPropertyService().bluePrintNatsService(NatsLibConstants.DEFULT_NATS_SELECTOR) +} + +class NatsLibConstants { + companion object { + + const val SERVICE_BLUEPRINT_NATS_LIB_PROPERTY = "blueprint-nats-lib-property-service" + const val DEFULT_NATS_SELECTOR = "cds-controller" + const val PROPERTY_NATS_PREFIX = "blueprintsprocessor.nats." + const val PROPERTY_NATS_CLUSTER_ID = "NATS_CLUSTER_ID" + const val TYPE_TOKEN_AUTH = "token-auth" + const val TYPE_TLS_AUTH = "tls-auth" + } +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibData.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibData.kt new file mode 100644 index 000000000..a989dcf1e --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/BlueprintNatsLibData.kt @@ -0,0 +1,45 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats + +import org.onap.ccsdk.cds.blueprintsprocessor.nats.utils.NatsClusterUtils +import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils + +open class NatsConnectionProperties { + + lateinit var type: String + var clusterId: String = NatsClusterUtils.clusterId() + var clientId: String = ClusterUtils.clusterNodeId() + lateinit var host: String + + /** Rest endpoint selector to access Monitoring API */ + var monitoringSelector: String? = null +} + +open class TokenAuthNatsConnectionProperties : NatsConnectionProperties() { + + lateinit var token: String +} + +open class TLSAuthNatsConnectionProperties : NatsConnectionProperties() { + + var trustCertCollection: String? = null + + /** Below Used only for Mutual TLS */ + var clientCertChain: String? = null + var clientPrivateKey: String? = null +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSL.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSL.kt index 4c5471796..9e771a6e5 100644 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSL.kt +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSL.kt @@ -17,8 +17,8 @@ package org.onap.ccsdk.cds.blueprintsprocessor.nats import com.fasterxml.jackson.databind.JsonNode -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType @@ -30,25 +30,25 @@ import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType /** Relationships Types DSL for NATS Producer */ fun ServiceTemplateBuilder.relationshipTypeConnectsToNats() { - val relationshipType = BluePrintTypes.relationshipTypeConnectsToNats() + val relationshipType = BlueprintTypes.relationshipTypeConnectsToNats() if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf() this.relationshipTypes!![relationshipType.id!!] = relationshipType } -fun BluePrintTypes.relationshipTypeConnectsToNats(): RelationshipType { +fun BlueprintTypes.relationshipTypeConnectsToNats(): RelationshipType { return relationshipType( - id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, - version = BluePrintConstants.DEFAULT_VERSION_NUMBER, - derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO, + id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, + version = BlueprintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO, description = "Relationship connects to through NATS Client." ) { property( - BluePrintConstants.PROPERTY_CONNECTION_CONFIG, - BluePrintConstants.DATA_TYPE_MAP, + BlueprintConstants.PROPERTY_CONNECTION_CONFIG, + BlueprintConstants.DATA_TYPE_MAP, true, "Connection Config details." ) - validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT)) + validTargetTypes(arrayListOf(BlueprintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT)) } } @@ -66,25 +66,25 @@ fun TopologyTemplateBuilder.relationshipTemplateNats( class NatsRelationshipTemplateBuilder(name: String, description: String) : RelationshipTemplateBuilder( name, - BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, description + BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, description ) { fun tokenAuth(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit) { - property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthNatsProperties(block)) + property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tokenAuthNatsProperties(block)) } fun tlsAuth(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit) { - property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tlsAuthNatsProperties(block)) + property(BlueprintConstants.PROPERTY_CONNECTION_CONFIG, BlueprintTypes.tlsAuthNatsProperties(block)) } } -fun BluePrintTypes.tokenAuthNatsProperties(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode { +fun BlueprintTypes.tokenAuthNatsProperties(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode { val assignments = NatsTokenAuthPropertiesAssignmentBuilder().apply(block).build() assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive() return assignments.asJsonNode() } -fun BluePrintTypes.tlsAuthNatsProperties(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode { +fun BlueprintTypes.tlsAuthNatsProperties(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode { val assignments = NatsTLSAuthPropertiesAssignmentBuilder().apply(block).build() assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TLS_AUTH.asJsonPrimitive() return assignments.asJsonNode() diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyService.kt deleted file mode 100644 index 1ac44bc94..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyService.kt +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats.service - -import com.fasterxml.jackson.databind.JsonNode -import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService -import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsConnectionProperties -import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants -import org.onap.ccsdk.cds.blueprintsprocessor.nats.TLSAuthNatsConnectionProperties -import org.onap.ccsdk.cds.blueprintsprocessor.nats.TokenAuthNatsConnectionProperties -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.springframework.stereotype.Service - -@Service(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY) -open class BluePrintNatsLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) { - - fun bluePrintNatsService(jsonNode: JsonNode): BluePrintNatsService { - val natsConnectionProperties = natsConnectionProperties(jsonNode) - return bluePrintNatsService(natsConnectionProperties) - } - - fun bluePrintNatsService(selector: String): BluePrintNatsService { - val prefix = "${NatsLibConstants.PROPERTY_NATS_PREFIX}$selector" - val natsConnectionProperties = natsConnectionProperties(prefix) - return bluePrintNatsService(natsConnectionProperties) - } - - /** NATS Lib Property Service */ - fun natsConnectionProperties(jsonNode: JsonNode): NatsConnectionProperties { - return when (val type = jsonNode.get("type").textValue()) { - NatsLibConstants.TYPE_TOKEN_AUTH -> { - JacksonUtils.readValue(jsonNode, TokenAuthNatsConnectionProperties::class.java)!! - } - NatsLibConstants.TYPE_TLS_AUTH -> { - JacksonUtils.readValue(jsonNode, TLSAuthNatsConnectionProperties::class.java)!! - } - else -> { - throw BluePrintProcessorException("NATS type($type) not supported") - } - } - } - - fun natsConnectionProperties(prefix: String): NatsConnectionProperties { - val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java) - return when (type) { - NatsLibConstants.TYPE_TOKEN_AUTH -> { - tokenAuthNatsConnectionProperties(prefix) - } - NatsLibConstants.TYPE_TLS_AUTH -> { - tlsAuthNatsConnectionProperties(prefix) - } - else -> { - throw BluePrintProcessorException("NATS type($type) not supported") - } - } - } - - private fun tokenAuthNatsConnectionProperties(prefix: String): TokenAuthNatsConnectionProperties { - return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthNatsConnectionProperties::class.java) - } - - private fun tlsAuthNatsConnectionProperties(prefix: String): TLSAuthNatsConnectionProperties { - return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthNatsConnectionProperties::class.java) - } - - fun bluePrintNatsService(natsConnectionProperties: NatsConnectionProperties): - BluePrintNatsService { - return when (natsConnectionProperties) { - is TokenAuthNatsConnectionProperties -> { - TokenAuthNatsService(natsConnectionProperties) - } - is TLSAuthNatsConnectionProperties -> { - TLSAuthNatsService(natsConnectionProperties) - } - else -> { - throw BluePrintProcessorException("couldn't get NATS service for properties $natsConnectionProperties") - } - } - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsService.kt deleted file mode 100644 index 9548fe78d..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsService.kt +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -@file:Suppress("BlockingMethodInNonBlockingContext") - -package org.onap.ccsdk.cds.blueprintsprocessor.nats.service - -import io.nats.client.Dispatcher -import io.nats.streaming.MessageHandler -import io.nats.streaming.StreamingConnection -import io.nats.streaming.Subscription -import io.nats.streaming.SubscriptionOptions -import java.time.Duration - -interface BluePrintNatsService { - - /** Create and Return the NATS streaming connection */ - suspend fun connection(): StreamingConnection - - /** Send one request [message] to the [subject] and get only one reply - * The request message subscriber may be multi instances consumer or load balance consumer. - * If it is multi instances consumer, then we will get only first responses from subscribers. - * - */ - suspend fun requestAndGetOneReply(subject: String, message: ByteArray, timeout: Long): io.nats.client.Message { - return connection().natsConnection.request(subject, message, Duration.ofMillis(timeout)) - } - - /** Send one request [message] to the [subject] and get multiple replies in [replySubject] with [messageHandler] - * The request message subscriber may be multi instances consumer or load balance consumer. - * If it is multi instances consumer, then we will get multiple responses from subscribers. - * Include the unSubscribe logic's in [messageHandler] implementation. - */ - suspend fun requestAndGetMultipleReplies( - subject: String, - replySubject: String, - message: ByteArray, - messageHandler: io.nats.client.MessageHandler - ) { - val natsConnection = connection().natsConnection - val dispatcher = natsConnection.createDispatcher(messageHandler) - /** Reply subject consumer */ - dispatcher.subscribe(replySubject) - - /** Publish the request message and expect the reply messages in reply subject consumer */ - natsConnection.publish(subject, replySubject, message) - } - - /** Synchronous reply Subscribe the [subject] with the [messageHandler]. - * This is used only the message has to be consumed by all instances in the cluster and message handler must reply. - */ - suspend fun replySubscribe( - subject: String, - messageHandler: io.nats.client.MessageHandler - ): Dispatcher { - val natsConnection = connection().natsConnection - val dispatcher = natsConnection.createDispatcher(messageHandler) - return dispatcher.subscribe(subject) - } - - /** - * Synchronous reply Subscriber will listen for [subject] with [loadBalanceGroup]. - * This is used only the message has to be consumed by only one instance in the cluster. - * server will now load balance messages between the members of the queue group and message handler must reply. - */ - suspend fun loadBalanceReplySubscribe( - subject: String, - loadBalanceGroup: String, - messageHandler: io.nats.client.MessageHandler - ): Dispatcher { - val natsConnection = connection().natsConnection - val dispatcher = natsConnection.createDispatcher(messageHandler) - return dispatcher.subscribe(subject, loadBalanceGroup) - } - - /** Publish the [message] to all subscribers on the [subject] */ - suspend fun publish(subject: String, message: ByteArray) { - connection().publish(subject, message) - } - - /** Subscribe the [subject] with the [messageHandler]. - * This is used only the message has to be consumed by all instances in the cluster. - */ - suspend fun subscribe( - subject: String, - messageHandler: MessageHandler - ): Subscription { - return connection().subscribe(subject, messageHandler) - } - - /** Subscribe the [subject] with the [messageHandler] and [subscriptionOptions]. - * This is used only the message has to be consumed by all instances in the cluster. - */ - suspend fun subscribe( - subject: String, - messageHandler: MessageHandler, - subscriptionOptions: SubscriptionOptions - ): Subscription { - return connection().subscribe(subject, messageHandler, subscriptionOptions) - } - - /** - * https://docs.nats.io/developing-with-nats/receiving/queues - * subscribers will listen for [subject] with [loadBalanceGroup]. - * This is used only the message has to be consumed by only one instance in the cluster. - * server will now load balance messages between the members of the queue group. - */ - suspend fun loadBalanceSubscribe( - subject: String, - loadBalanceGroup: String, - messageHandler: MessageHandler - ): Subscription { - return connection().subscribe(subject, loadBalanceGroup, messageHandler) - } - - /** - * https://docs.nats.io/developing-with-nats/receiving/queues - * subscribers will listen for [subject] with [loadBalanceGroup] and [subscriptionOptions]. - * This is used only the message has to be consumed by only one instance in the cluster. - * server will now load balance messages between the members of the queue group. - */ - suspend fun loadBalanceSubscribe( - subject: String, - loadBalanceGroup: String, - messageHandler: MessageHandler, - subscriptionOptions: SubscriptionOptions - ): Subscription { - return connection().subscribe(subject, loadBalanceGroup, messageHandler, subscriptionOptions) - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyService.kt new file mode 100644 index 000000000..457bf8703 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyService.kt @@ -0,0 +1,95 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats.service + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService +import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsConnectionProperties +import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants +import org.onap.ccsdk.cds.blueprintsprocessor.nats.TLSAuthNatsConnectionProperties +import org.onap.ccsdk.cds.blueprintsprocessor.nats.TokenAuthNatsConnectionProperties +import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.springframework.stereotype.Service + +@Service(NatsLibConstants.SERVICE_BLUEPRINT_NATS_LIB_PROPERTY) +open class BlueprintNatsLibPropertyService(private var bluePrintPropertiesService: BlueprintPropertiesService) { + + fun bluePrintNatsService(jsonNode: JsonNode): BlueprintNatsService { + val natsConnectionProperties = natsConnectionProperties(jsonNode) + return bluePrintNatsService(natsConnectionProperties) + } + + fun bluePrintNatsService(selector: String): BlueprintNatsService { + val prefix = "${NatsLibConstants.PROPERTY_NATS_PREFIX}$selector" + val natsConnectionProperties = natsConnectionProperties(prefix) + return bluePrintNatsService(natsConnectionProperties) + } + + /** NATS Lib Property Service */ + fun natsConnectionProperties(jsonNode: JsonNode): NatsConnectionProperties { + return when (val type = jsonNode.get("type").textValue()) { + NatsLibConstants.TYPE_TOKEN_AUTH -> { + JacksonUtils.readValue(jsonNode, TokenAuthNatsConnectionProperties::class.java)!! + } + NatsLibConstants.TYPE_TLS_AUTH -> { + JacksonUtils.readValue(jsonNode, TLSAuthNatsConnectionProperties::class.java)!! + } + else -> { + throw BlueprintProcessorException("NATS type($type) not supported") + } + } + } + + fun natsConnectionProperties(prefix: String): NatsConnectionProperties { + val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java) + return when (type) { + NatsLibConstants.TYPE_TOKEN_AUTH -> { + tokenAuthNatsConnectionProperties(prefix) + } + NatsLibConstants.TYPE_TLS_AUTH -> { + tlsAuthNatsConnectionProperties(prefix) + } + else -> { + throw BlueprintProcessorException("NATS type($type) not supported") + } + } + } + + private fun tokenAuthNatsConnectionProperties(prefix: String): TokenAuthNatsConnectionProperties { + return bluePrintPropertiesService.propertyBeanType(prefix, TokenAuthNatsConnectionProperties::class.java) + } + + private fun tlsAuthNatsConnectionProperties(prefix: String): TLSAuthNatsConnectionProperties { + return bluePrintPropertiesService.propertyBeanType(prefix, TLSAuthNatsConnectionProperties::class.java) + } + + fun bluePrintNatsService(natsConnectionProperties: NatsConnectionProperties): + BlueprintNatsService { + return when (natsConnectionProperties) { + is TokenAuthNatsConnectionProperties -> { + TokenAuthNatsService(natsConnectionProperties) + } + is TLSAuthNatsConnectionProperties -> { + TLSAuthNatsService(natsConnectionProperties) + } + else -> { + throw BlueprintProcessorException("couldn't get NATS service for properties $natsConnectionProperties") + } + } + } +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsService.kt new file mode 100644 index 000000000..67de95133 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsService.kt @@ -0,0 +1,143 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("BlockingMethodInNonBlockingContext") + +package org.onap.ccsdk.cds.blueprintsprocessor.nats.service + +import io.nats.client.Dispatcher +import io.nats.streaming.MessageHandler +import io.nats.streaming.StreamingConnection +import io.nats.streaming.Subscription +import io.nats.streaming.SubscriptionOptions +import java.time.Duration + +interface BlueprintNatsService { + + /** Create and Return the NATS streaming connection */ + suspend fun connection(): StreamingConnection + + /** Send one request [message] to the [subject] and get only one reply + * The request message subscriber may be multi instances consumer or load balance consumer. + * If it is multi instances consumer, then we will get only first responses from subscribers. + * + */ + suspend fun requestAndGetOneReply(subject: String, message: ByteArray, timeout: Long): io.nats.client.Message { + return connection().natsConnection.request(subject, message, Duration.ofMillis(timeout)) + } + + /** Send one request [message] to the [subject] and get multiple replies in [replySubject] with [messageHandler] + * The request message subscriber may be multi instances consumer or load balance consumer. + * If it is multi instances consumer, then we will get multiple responses from subscribers. + * Include the unSubscribe logic's in [messageHandler] implementation. + */ + suspend fun requestAndGetMultipleReplies( + subject: String, + replySubject: String, + message: ByteArray, + messageHandler: io.nats.client.MessageHandler + ) { + val natsConnection = connection().natsConnection + val dispatcher = natsConnection.createDispatcher(messageHandler) + /** Reply subject consumer */ + dispatcher.subscribe(replySubject) + + /** Publish the request message and expect the reply messages in reply subject consumer */ + natsConnection.publish(subject, replySubject, message) + } + + /** Synchronous reply Subscribe the [subject] with the [messageHandler]. + * This is used only the message has to be consumed by all instances in the cluster and message handler must reply. + */ + suspend fun replySubscribe( + subject: String, + messageHandler: io.nats.client.MessageHandler + ): Dispatcher { + val natsConnection = connection().natsConnection + val dispatcher = natsConnection.createDispatcher(messageHandler) + return dispatcher.subscribe(subject) + } + + /** + * Synchronous reply Subscriber will listen for [subject] with [loadBalanceGroup]. + * This is used only the message has to be consumed by only one instance in the cluster. + * server will now load balance messages between the members of the queue group and message handler must reply. + */ + suspend fun loadBalanceReplySubscribe( + subject: String, + loadBalanceGroup: String, + messageHandler: io.nats.client.MessageHandler + ): Dispatcher { + val natsConnection = connection().natsConnection + val dispatcher = natsConnection.createDispatcher(messageHandler) + return dispatcher.subscribe(subject, loadBalanceGroup) + } + + /** Publish the [message] to all subscribers on the [subject] */ + suspend fun publish(subject: String, message: ByteArray) { + connection().publish(subject, message) + } + + /** Subscribe the [subject] with the [messageHandler]. + * This is used only the message has to be consumed by all instances in the cluster. + */ + suspend fun subscribe( + subject: String, + messageHandler: MessageHandler + ): Subscription { + return connection().subscribe(subject, messageHandler) + } + + /** Subscribe the [subject] with the [messageHandler] and [subscriptionOptions]. + * This is used only the message has to be consumed by all instances in the cluster. + */ + suspend fun subscribe( + subject: String, + messageHandler: MessageHandler, + subscriptionOptions: SubscriptionOptions + ): Subscription { + return connection().subscribe(subject, messageHandler, subscriptionOptions) + } + + /** + * https://docs.nats.io/developing-with-nats/receiving/queues + * subscribers will listen for [subject] with [loadBalanceGroup]. + * This is used only the message has to be consumed by only one instance in the cluster. + * server will now load balance messages between the members of the queue group. + */ + suspend fun loadBalanceSubscribe( + subject: String, + loadBalanceGroup: String, + messageHandler: MessageHandler + ): Subscription { + return connection().subscribe(subject, loadBalanceGroup, messageHandler) + } + + /** + * https://docs.nats.io/developing-with-nats/receiving/queues + * subscribers will listen for [subject] with [loadBalanceGroup] and [subscriptionOptions]. + * This is used only the message has to be consumed by only one instance in the cluster. + * server will now load balance messages between the members of the queue group. + */ + suspend fun loadBalanceSubscribe( + subject: String, + loadBalanceGroup: String, + messageHandler: MessageHandler, + subscriptionOptions: SubscriptionOptions + ): Subscription { + return connection().subscribe(subject, loadBalanceGroup, messageHandler, subscriptionOptions) + } +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TLSAuthNatsService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TLSAuthNatsService.kt index 00a972eff..1bde8e534 100644 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TLSAuthNatsService.kt +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TLSAuthNatsService.kt @@ -25,7 +25,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList import javax.net.ssl.SSLContext open class TLSAuthNatsService(private val natsConnectionProperties: TLSAuthNatsConnectionProperties) : - BluePrintNatsService { + BlueprintNatsService { lateinit var streamingConnection: StreamingConnection diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TokenAuthNatsService.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TokenAuthNatsService.kt index 7c24b70d0..9d4aacfbd 100644 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TokenAuthNatsService.kt +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/TokenAuthNatsService.kt @@ -25,7 +25,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.logger import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList open class TokenAuthNatsService(private val natsConnectionProperties: TokenAuthNatsConnectionProperties) : - BluePrintNatsService { + BlueprintNatsService { private val log = logger(TokenAuthNatsService::class) diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/utils/NatsClusterUtils.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/utils/NatsClusterUtils.kt index a7726a14b..a155a076d 100644 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/utils/NatsClusterUtils.kt +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/utils/NatsClusterUtils.kt @@ -17,7 +17,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.nats.utils import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils object NatsClusterUtils { @@ -32,7 +32,7 @@ object NatsClusterUtils { } fun currentApplicationSubject(subject: String): String { - return "${BluePrintConstants.APP_NAME}.$subject" + return "${BlueprintConstants.APP_NAME}.$subject" } fun currentNodeDurable(subject: String): String { @@ -40,6 +40,6 @@ object NatsClusterUtils { } fun applicationLoadBalanceGroup(): String { - return "${BluePrintConstants.APP_NAME}" + return "${BlueprintConstants.APP_NAME}" } } diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSLTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSLTest.kt index a95b900fe..fa70f0738 100644 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSLTest.kt +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/NatsPropertiesDSLTest.kt @@ -17,7 +17,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.nats import org.junit.Test -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants import org.onap.ccsdk.cds.controllerblueprints.core.dsl.getInput import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipTypeConnectsTo import org.onap.ccsdk.cds.controllerblueprints.core.dsl.serviceTemplate @@ -59,12 +59,12 @@ class NatsPropertiesDSLTest { assertNotNull(relationshipTypes, "failed to get relationship types") assertEquals(2, relationshipTypes.size, "relationshipTypes doesn't match") assertNotNull( - relationshipTypes[BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO], - "failed to get ${BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO}" + relationshipTypes[BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO], + "failed to get ${BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO}" ) assertNotNull( - relationshipTypes[BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS], - "failed to get ${BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS}" + relationshipTypes[BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS], + "failed to get ${BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS}" ) } } diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsExtensionsTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsExtensionsTest.kt deleted file mode 100644 index ec120dc18..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsExtensionsTest.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats.service - -import io.mockk.every -import io.mockk.mockk -import org.junit.Test -import org.onap.ccsdk.cds.blueprintsprocessor.nats.asJsonType -import org.onap.ccsdk.cds.blueprintsprocessor.nats.strData -import org.onap.ccsdk.cds.controllerblueprints.core.asByteArray -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString -import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType -import java.nio.charset.Charset -import kotlin.test.assertEquals - -class BluePrintNatsExtensionsTest { - - @Test - fun testMessageStrConversion() { - val mockMessage = mockk() - every { mockMessage.data } returns "I am message".toByteArray(Charset.defaultCharset()) - - val messageData = mockMessage.strData() - assertEquals("I am message", messageData) - } - - @Test - fun testMessageJsonConversion() { - val json = """{"name":"value"}""" - - val mockMessage = mockk() - every { mockMessage.data } returns json.jsonAsJsonType().asByteArray() - - val messageData = mockMessage.asJsonType().asJsonString() - assertEquals(json, messageData) - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyServiceTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyServiceTest.kt deleted file mode 100644 index 9a332d589..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsLibPropertyServiceTest.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats.service - -import org.junit.Test -import org.junit.runner.RunWith -import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService -import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration -import org.onap.ccsdk.cds.blueprintsprocessor.nats.BluePrintNatsLibConfiguration -import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.context.TestPropertySource -import org.springframework.test.context.junit4.SpringRunner -import kotlin.test.assertTrue - -@RunWith(SpringRunner::class) -@ContextConfiguration( - classes = [ - BluePrintNatsLibConfiguration::class, - BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class - ] -) -@TestPropertySource( - properties = - [ - "blueprintsprocessor.nats.cds-controller.type=token-auth", - "blueprintsprocessor.nats.cds-controller.host=nats://localhost:4222", - "blueprintsprocessor.nats.cds-controller.token=tokenAuth" - ] -) -class BluePrintNatsLibPropertyServiceTest { - - @Autowired - lateinit var bluePrintNatsLibPropertyService: BluePrintNatsLibPropertyService - - @Test - fun testNatsProperties() { - assertTrue(::bluePrintNatsLibPropertyService.isInitialized) - bluePrintNatsLibPropertyService.bluePrintNatsService(NatsLibConstants.DEFULT_NATS_SELECTOR) - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsServiceTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsServiceTest.kt deleted file mode 100644 index 721828ac9..000000000 --- a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BluePrintNatsServiceTest.kt +++ /dev/null @@ -1,267 +0,0 @@ -/* - * Copyright © 2018-2019 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.nats.service - -import io.mockk.every -import io.mockk.mockk -import io.mockk.spyk -import io.nats.streaming.MessageHandler -import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking -import org.junit.Test -import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsConnectionProperties -import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants -import org.onap.ccsdk.cds.blueprintsprocessor.nats.TokenAuthNatsConnectionProperties -import org.onap.ccsdk.cds.blueprintsprocessor.nats.strData -import org.onap.ccsdk.cds.blueprintsprocessor.nats.utils.SubscriptionOptionsUtils -import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType -import kotlin.test.assertNotNull - -class BluePrintNatsServiceTest { - - @Test - fun testTokenAuthNatService() { - val configuration = """{ - "type" : "${NatsLibConstants.TYPE_TOKEN_AUTH}", - "host" : "nats://localhost:4222", - "token" : "tokenAuth" - } - """.trimIndent() - - val bluePrintNatsLibPropertyService = BluePrintNatsLibPropertyService(mockk()) - - val spkBluePrintNatsLibPropertyService = spyk(bluePrintNatsLibPropertyService) - every { - spkBluePrintNatsLibPropertyService - .bluePrintNatsService(any()) - } returns TokenAuthNatsService( - mockk() - ) - - val bluePrintNatsService = - spkBluePrintNatsLibPropertyService.bluePrintNatsService(configuration.jsonAsJsonType()) - assertNotNull(bluePrintNatsService, "failed to get NATS Service") - } - - @Test - fun testTLSAuthNatService() { - val configuration = """{ - "type" : "${NatsLibConstants.TYPE_TLS_AUTH}", - "host" : "nats://localhost:4222" - } - """.trimIndent() - - val bluePrintNatsLibPropertyService = BluePrintNatsLibPropertyService(mockk()) - - val spkBluePrintNatsLibPropertyService = spyk(bluePrintNatsLibPropertyService) - every { - spkBluePrintNatsLibPropertyService - .bluePrintNatsService(any()) - } returns TLSAuthNatsService( - mockk() - ) - - val bluePrintNatsService = - spkBluePrintNatsLibPropertyService.bluePrintNatsService(configuration.jsonAsJsonType()) - assertNotNull(bluePrintNatsService, "failed to get NATS Service") - } - - /** Enable to test only on local desktop. Don't enable in Build server - * Start the Server with : nats-streaming-server -cid cds-cluster --auth tokenAuth -m 8222 -V - */ - // @Test - fun localIntegrationTest() { - runBlocking { - - val connectionProperties = TokenAuthNatsConnectionProperties().apply { - host = "nats://localhost:4222,nats://localhost:4223" - clientId = "client-1" - token = "tokenAuth" - } - val natsService = TokenAuthNatsService(connectionProperties) - val streamingConnection = natsService.connection() - assertNotNull(streamingConnection, "failed to create nats connection") - - val connectionProperties2 = TokenAuthNatsConnectionProperties().apply { - host = "nats://localhost:4222,nats://localhost:4223" - clientId = "client-2" - token = "tokenAuth" - } - val tlsAuthNatsService2 = TokenAuthNatsService(connectionProperties2) - val streamingConnection2 = tlsAuthNatsService2.connection() - assertNotNull(streamingConnection2, "failed to create nats connection 2") - - testMultiPublish(natsService) - testLoadBalance(natsService) - testLimitSubscription(natsService) - testRequestReply(natsService) - testMultiRequestReply(natsService) - delay(1000) - } - } - - private fun testMultiPublish(natsService: BluePrintNatsService) { - runBlocking { - /** Multiple Publish Message Test **/ - val messageHandler1 = - MessageHandler { message -> println("Multi Publish Message Handler 1: ${message.strData()}") } - val messageHandler2 = - MessageHandler { message -> println("Multi Publish Message Handler 2: ${message.strData()}") } - - natsService.subscribe("multi-publish", messageHandler1) - natsService.subscribe("multi-publish", messageHandler2) - - repeat(5) { - natsService.publish("multi-publish", "multi publish message-$it".toByteArray()) - } - } - } - - private fun testLoadBalance(natsService: BluePrintNatsService) { - runBlocking { - /** Load balance Publish Message Test **/ - val lbMessageHandler1 = - MessageHandler { message -> println("LB Publish Message Handler 1: ${message.strData()}") } - val lbMessageHandler2 = - MessageHandler { message -> println("LB Publish Message Handler 2: ${message.strData()}") } - - val sub1 = natsService.loadBalanceSubscribe("lb-publish", "lb-group", lbMessageHandler1) - val sub2 = natsService.loadBalanceSubscribe("lb-publish", "lb-group", lbMessageHandler2) - - repeat(5) { - natsService.publish("lb-publish", "lb publish message-$it".toByteArray()) - } - sub1.unsubscribe() - sub2.unsubscribe() - } - } - - private fun testLimitSubscription(natsService: BluePrintNatsService) { - runBlocking { - /** Load balance Publish Message Test **/ - val lbMessageHandler1 = - MessageHandler { message -> - runBlocking { - println("LB Publish Message Handler 1: ${message.strData()}") - message.ack() - } - } - val lbMessageHandler2 = - MessageHandler { message -> - runBlocking { - println("LB Publish Message Handler 2: ${message.strData()}") - message.ack() - } - } - - val sub1 = natsService.loadBalanceSubscribe( - "lb-publish", "lb-group", lbMessageHandler1, - SubscriptionOptionsUtils.manualAckWithRateLimit(1) - ) - val sub2 = natsService.loadBalanceSubscribe( - "lb-publish", "lb-group", lbMessageHandler2, - SubscriptionOptionsUtils.manualAckWithRateLimit(1) - ) - - repeat(10) { - natsService.publish("lb-publish", "lb limit message-$it".toByteArray()) - } - sub1.unsubscribe() - sub2.unsubscribe() - } - } - - private fun testRequestReply(natsService: BluePrintNatsService) { - runBlocking { - val lbMessageHandler1 = io.nats.client.MessageHandler { message -> - println("LB RR Request Handler 1: ${String(message.data)} will reply to(${message.replyTo})") - message.connection.publish( - message.replyTo, - "Notification ${String(message.data)} reply from 1".toByteArray() - ) - } - - val lbMessageHandler2 = io.nats.client.MessageHandler { message -> - println("LB RR Request Handler 2: ${String(message.data)} will reply to(${message.replyTo})") - message.connection.publish( - message.replyTo, - "Notification ${String(message.data)} reply from 2".toByteArray() - ) - } - - natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler1) - natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler2) - - repeat(5) { - val message = natsService.requestAndGetOneReply( - "rr-request", - "rr message-$it".toByteArray(), - 1000 - ) - println("Received : ${message.strData()}") - } - } - } - - private fun testMultiRequestReply(natsService: BluePrintNatsService) { - runBlocking { - /** Request Reply **/ - val lbMessageHandler1 = io.nats.client.MessageHandler { message -> - println("LB RR Request Handler 1: ${String(message.data)} will reply to(${message.replyTo})") - message.connection.publish( - message.replyTo, - "Notification ${message.strData()} reply from 1".toByteArray() - ) - message.connection.publish( - message.replyTo, - "Completion ${message.strData()} reply from 1".toByteArray() - ) - } - val lbMessageHandler2 = io.nats.client.MessageHandler { message -> - println("LB RR Request Handler 2: ${message.strData()} will reply to(${message.replyTo})") - message.connection.publish( - message.replyTo, - "Notification ${message.strData()} reply from 2".toByteArray() - ) - message.connection.publish( - message.replyTo, - "Completion ${message.strData()} reply from 2".toByteArray() - ) - } - - natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler1) - natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler2) - - /** Should unsubscribe on completion message */ - val rrReplyMessageHandler = io.nats.client.MessageHandler { message -> - val messageContent = message.strData() - println("RR Reply Handler : $messageContent") - if (messageContent.startsWith("Completion")) { - message.subscription.unsubscribe() - } - } - repeat(5) { - natsService.requestAndGetMultipleReplies( - "rr-request", - "rr-reply-$it", - "rr message-$it".toByteArray(), - rrReplyMessageHandler - ) - } - } - } -} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsExtensionsTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsExtensionsTest.kt new file mode 100644 index 000000000..816b6c809 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsExtensionsTest.kt @@ -0,0 +1,51 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats.service + +import io.mockk.every +import io.mockk.mockk +import org.junit.Test +import org.onap.ccsdk.cds.blueprintsprocessor.nats.asJsonType +import org.onap.ccsdk.cds.blueprintsprocessor.nats.strData +import org.onap.ccsdk.cds.controllerblueprints.core.asByteArray +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType +import java.nio.charset.Charset +import kotlin.test.assertEquals + +class BlueprintNatsExtensionsTest { + + @Test + fun testMessageStrConversion() { + val mockMessage = mockk() + every { mockMessage.data } returns "I am message".toByteArray(Charset.defaultCharset()) + + val messageData = mockMessage.strData() + assertEquals("I am message", messageData) + } + + @Test + fun testMessageJsonConversion() { + val json = """{"name":"value"}""" + + val mockMessage = mockk() + every { mockMessage.data } returns json.jsonAsJsonType().asByteArray() + + val messageData = mockMessage.asJsonType().asJsonString() + assertEquals(json, messageData) + } +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyServiceTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyServiceTest.kt new file mode 100644 index 000000000..5ef4a72c6 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsLibPropertyServiceTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats.service + +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService +import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.nats.BlueprintNatsLibConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner +import kotlin.test.assertTrue + +@RunWith(SpringRunner::class) +@ContextConfiguration( + classes = [ + BlueprintNatsLibConfiguration::class, + BlueprintPropertyConfiguration::class, BlueprintPropertiesService::class + ] +) +@TestPropertySource( + properties = + [ + "blueprintsprocessor.nats.cds-controller.type=token-auth", + "blueprintsprocessor.nats.cds-controller.host=nats://localhost:4222", + "blueprintsprocessor.nats.cds-controller.token=tokenAuth" + ] +) +class BlueprintNatsLibPropertyServiceTest { + + @Autowired + lateinit var bluePrintNatsLibPropertyService: BlueprintNatsLibPropertyService + + @Test + fun testNatsProperties() { + assertTrue(::bluePrintNatsLibPropertyService.isInitialized) + bluePrintNatsLibPropertyService.bluePrintNatsService(NatsLibConstants.DEFULT_NATS_SELECTOR) + } +} diff --git a/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsServiceTest.kt b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsServiceTest.kt new file mode 100644 index 000000000..8e7ee63f1 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/nats-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/nats/service/BlueprintNatsServiceTest.kt @@ -0,0 +1,267 @@ +/* + * Copyright © 2018-2019 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.nats.service + +import io.mockk.every +import io.mockk.mockk +import io.mockk.spyk +import io.nats.streaming.MessageHandler +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.junit.Test +import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsConnectionProperties +import org.onap.ccsdk.cds.blueprintsprocessor.nats.NatsLibConstants +import org.onap.ccsdk.cds.blueprintsprocessor.nats.TokenAuthNatsConnectionProperties +import org.onap.ccsdk.cds.blueprintsprocessor.nats.strData +import org.onap.ccsdk.cds.blueprintsprocessor.nats.utils.SubscriptionOptionsUtils +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType +import kotlin.test.assertNotNull + +class BlueprintNatsServiceTest { + + @Test + fun testTokenAuthNatService() { + val configuration = """{ + "type" : "${NatsLibConstants.TYPE_TOKEN_AUTH}", + "host" : "nats://localhost:4222", + "token" : "tokenAuth" + } + """.trimIndent() + + val bluePrintNatsLibPropertyService = BlueprintNatsLibPropertyService(mockk()) + + val spkBlueprintNatsLibPropertyService = spyk(bluePrintNatsLibPropertyService) + every { + spkBlueprintNatsLibPropertyService + .bluePrintNatsService(any()) + } returns TokenAuthNatsService( + mockk() + ) + + val bluePrintNatsService = + spkBlueprintNatsLibPropertyService.bluePrintNatsService(configuration.jsonAsJsonType()) + assertNotNull(bluePrintNatsService, "failed to get NATS Service") + } + + @Test + fun testTLSAuthNatService() { + val configuration = """{ + "type" : "${NatsLibConstants.TYPE_TLS_AUTH}", + "host" : "nats://localhost:4222" + } + """.trimIndent() + + val bluePrintNatsLibPropertyService = BlueprintNatsLibPropertyService(mockk()) + + val spkBlueprintNatsLibPropertyService = spyk(bluePrintNatsLibPropertyService) + every { + spkBlueprintNatsLibPropertyService + .bluePrintNatsService(any()) + } returns TLSAuthNatsService( + mockk() + ) + + val bluePrintNatsService = + spkBlueprintNatsLibPropertyService.bluePrintNatsService(configuration.jsonAsJsonType()) + assertNotNull(bluePrintNatsService, "failed to get NATS Service") + } + + /** Enable to test only on local desktop. Don't enable in Build server + * Start the Server with : nats-streaming-server -cid cds-cluster --auth tokenAuth -m 8222 -V + */ + // @Test + fun localIntegrationTest() { + runBlocking { + + val connectionProperties = TokenAuthNatsConnectionProperties().apply { + host = "nats://localhost:4222,nats://localhost:4223" + clientId = "client-1" + token = "tokenAuth" + } + val natsService = TokenAuthNatsService(connectionProperties) + val streamingConnection = natsService.connection() + assertNotNull(streamingConnection, "failed to create nats connection") + + val connectionProperties2 = TokenAuthNatsConnectionProperties().apply { + host = "nats://localhost:4222,nats://localhost:4223" + clientId = "client-2" + token = "tokenAuth" + } + val tlsAuthNatsService2 = TokenAuthNatsService(connectionProperties2) + val streamingConnection2 = tlsAuthNatsService2.connection() + assertNotNull(streamingConnection2, "failed to create nats connection 2") + + testMultiPublish(natsService) + testLoadBalance(natsService) + testLimitSubscription(natsService) + testRequestReply(natsService) + testMultiRequestReply(natsService) + delay(1000) + } + } + + private fun testMultiPublish(natsService: BlueprintNatsService) { + runBlocking { + /** Multiple Publish Message Test **/ + val messageHandler1 = + MessageHandler { message -> println("Multi Publish Message Handler 1: ${message.strData()}") } + val messageHandler2 = + MessageHandler { message -> println("Multi Publish Message Handler 2: ${message.strData()}") } + + natsService.subscribe("multi-publish", messageHandler1) + natsService.subscribe("multi-publish", messageHandler2) + + repeat(5) { + natsService.publish("multi-publish", "multi publish message-$it".toByteArray()) + } + } + } + + private fun testLoadBalance(natsService: BlueprintNatsService) { + runBlocking { + /** Load balance Publish Message Test **/ + val lbMessageHandler1 = + MessageHandler { message -> println("LB Publish Message Handler 1: ${message.strData()}") } + val lbMessageHandler2 = + MessageHandler { message -> println("LB Publish Message Handler 2: ${message.strData()}") } + + val sub1 = natsService.loadBalanceSubscribe("lb-publish", "lb-group", lbMessageHandler1) + val sub2 = natsService.loadBalanceSubscribe("lb-publish", "lb-group", lbMessageHandler2) + + repeat(5) { + natsService.publish("lb-publish", "lb publish message-$it".toByteArray()) + } + sub1.unsubscribe() + sub2.unsubscribe() + } + } + + private fun testLimitSubscription(natsService: BlueprintNatsService) { + runBlocking { + /** Load balance Publish Message Test **/ + val lbMessageHandler1 = + MessageHandler { message -> + runBlocking { + println("LB Publish Message Handler 1: ${message.strData()}") + message.ack() + } + } + val lbMessageHandler2 = + MessageHandler { message -> + runBlocking { + println("LB Publish Message Handler 2: ${message.strData()}") + message.ack() + } + } + + val sub1 = natsService.loadBalanceSubscribe( + "lb-publish", "lb-group", lbMessageHandler1, + SubscriptionOptionsUtils.manualAckWithRateLimit(1) + ) + val sub2 = natsService.loadBalanceSubscribe( + "lb-publish", "lb-group", lbMessageHandler2, + SubscriptionOptionsUtils.manualAckWithRateLimit(1) + ) + + repeat(10) { + natsService.publish("lb-publish", "lb limit message-$it".toByteArray()) + } + sub1.unsubscribe() + sub2.unsubscribe() + } + } + + private fun testRequestReply(natsService: BlueprintNatsService) { + runBlocking { + val lbMessageHandler1 = io.nats.client.MessageHandler { message -> + println("LB RR Request Handler 1: ${String(message.data)} will reply to(${message.replyTo})") + message.connection.publish( + message.replyTo, + "Notification ${String(message.data)} reply from 1".toByteArray() + ) + } + + val lbMessageHandler2 = io.nats.client.MessageHandler { message -> + println("LB RR Request Handler 2: ${String(message.data)} will reply to(${message.replyTo})") + message.connection.publish( + message.replyTo, + "Notification ${String(message.data)} reply from 2".toByteArray() + ) + } + + natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler1) + natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler2) + + repeat(5) { + val message = natsService.requestAndGetOneReply( + "rr-request", + "rr message-$it".toByteArray(), + 1000 + ) + println("Received : ${message.strData()}") + } + } + } + + private fun testMultiRequestReply(natsService: BlueprintNatsService) { + runBlocking { + /** Request Reply **/ + val lbMessageHandler1 = io.nats.client.MessageHandler { message -> + println("LB RR Request Handler 1: ${String(message.data)} will reply to(${message.replyTo})") + message.connection.publish( + message.replyTo, + "Notification ${message.strData()} reply from 1".toByteArray() + ) + message.connection.publish( + message.replyTo, + "Completion ${message.strData()} reply from 1".toByteArray() + ) + } + val lbMessageHandler2 = io.nats.client.MessageHandler { message -> + println("LB RR Request Handler 2: ${message.strData()} will reply to(${message.replyTo})") + message.connection.publish( + message.replyTo, + "Notification ${message.strData()} reply from 2".toByteArray() + ) + message.connection.publish( + message.replyTo, + "Completion ${message.strData()} reply from 2".toByteArray() + ) + } + + natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler1) + natsService.loadBalanceReplySubscribe("rr-request", "rr-group", lbMessageHandler2) + + /** Should unsubscribe on completion message */ + val rrReplyMessageHandler = io.nats.client.MessageHandler { message -> + val messageContent = message.strData() + println("RR Reply Handler : $messageContent") + if (messageContent.startsWith("Completion")) { + message.subscription.unsubscribe() + } + } + repeat(5) { + natsService.requestAndGetMultipleReplies( + "rr-request", + "rr-reply-$it", + "rr message-$it".toByteArray(), + rrReplyMessageHandler + ) + } + } + } +} -- cgit 1.2.3-korg