5c4301b38748dc8068c3f4e0a96c7421969246d5
[ccsdk/cds.git] /
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
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
25 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
26 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.RelationshipTemplateBuilder
27 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
29
30 /** Relationships Types DSL for NATS Producer */
31 fun BluePrintTypes.relationshipTypeConnectsToNats(): RelationshipType {
32     return relationshipType(
33         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS,
34         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
35         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
36         description = "Relationship connects to through NATS Client."
37     ) {
38         property(
39             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
40             BluePrintConstants.DATA_TYPE_MAP,
41             true,
42             "Connection Config details."
43         )
44         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
45     }
46 }
47
48 /** Relationships Templates for Nats */
49 fun TopologyTemplateBuilder.relationshipTemplateNats(
50     name: String,
51     description: String,
52     block: NatsRelationshipTemplateBuilder.() -> Unit
53 ) {
54     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
55     val relationshipTemplate = NatsRelationshipTemplateBuilder(name, description).apply(block).build()
56     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
57 }
58
59 class NatsRelationshipTemplateBuilder(name: String, description: String) :
60     RelationshipTemplateBuilder(
61         name,
62         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_NATS, description
63     ) {
64
65     fun tokenAuth(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit) {
66         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tokenAuthNatsProperties(block))
67     }
68
69     fun tlsAuth(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit) {
70         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.tlsAuthNatsProperties(block))
71     }
72 }
73
74 fun BluePrintTypes.tokenAuthNatsProperties(block: NatsTokenAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
75     val assignments = NatsTokenAuthPropertiesAssignmentBuilder().apply(block).build()
76     assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TOKEN_AUTH.asJsonPrimitive()
77     return assignments.asJsonNode()
78 }
79
80 fun BluePrintTypes.tlsAuthNatsProperties(block: NatsTLSAuthPropertiesAssignmentBuilder.() -> Unit): JsonNode {
81     val assignments = NatsTLSAuthPropertiesAssignmentBuilder().apply(block).build()
82     assignments[NatsConnectionProperties::type.name] = NatsLibConstants.TYPE_TLS_AUTH.asJsonPrimitive()
83     return assignments.asJsonNode()
84 }
85
86 open class NatsConnectionPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
87
88     fun clusterId(clusterId: String) = clusterId(clusterId.asJsonPrimitive())
89
90     fun clusterId(clusterId: JsonNode) = property(NatsConnectionProperties::clusterId, clusterId)
91
92     fun clientId(clientId: String) = clientId(clientId.asJsonPrimitive())
93
94     fun clientId(clientId: JsonNode) = property(NatsConnectionProperties::clientId, clientId)
95
96     fun host(host: String) = host(host.asJsonPrimitive())
97
98     fun host(host: JsonNode) = property(NatsConnectionProperties::host, host)
99
100     fun monitoringSelector(monitoringSelector: String) = monitoringSelector(monitoringSelector.asJsonPrimitive())
101
102     fun monitoringSelector(monitoringSelector: JsonNode) =
103         property(NatsConnectionProperties::monitoringSelector, monitoringSelector)
104 }
105
106 class NatsTokenAuthPropertiesAssignmentBuilder : NatsConnectionPropertiesAssignmentBuilder() {
107     fun token(selector: String) = token(selector.asJsonPrimitive())
108
109     fun token(selector: JsonNode) = property(TokenAuthNatsConnectionProperties::token, selector)
110 }
111
112 class NatsTLSAuthPropertiesAssignmentBuilder : NatsConnectionPropertiesAssignmentBuilder()