ee7a7dead9bf7659611073613df2bced14f238a2
[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.ssh
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.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
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.ServiceTemplateBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
29 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.relationshipType
30
31 /** Relationships Types DSL for Message Producer */
32 fun ServiceTemplateBuilder.relationshipTypeConnectsToSshClient() {
33     val relationshipType = BluePrintTypes.relationshipTypeConnectsToSshClient()
34     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
35     this.relationshipTypes!![relationshipType.id!!] = relationshipType
36 }
37
38 fun BluePrintTypes.relationshipTypeConnectsToSshClient(): RelationshipType {
39     return relationshipType(
40         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_SSH_CLIENT,
41         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
42         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
43         description = "Relationship connects to through SSH Client."
44     ) {
45         property(
46             BluePrintConstants.PROPERTY_CONNECTION_CONFIG,
47             BluePrintConstants.DATA_TYPE_MAP,
48             true,
49             "Connection Config details."
50         )
51         validTargetTypes(arrayListOf(BluePrintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
52     }
53 }
54
55 /** Relationships Templates for Ssh */
56 fun TopologyTemplateBuilder.relationshipTemplateSshClient(
57     name: String,
58     description: String,
59     block: SshRelationshipTemplateBuilder.() -> Unit
60 ) {
61     if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
62     val relationshipTemplate = SshRelationshipTemplateBuilder(name, description).apply(block).build()
63     relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
64 }
65
66 open class SshRelationshipTemplateBuilder(name: String, description: String) :
67     RelationshipTemplateBuilder(
68         name,
69         BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO_SSH_CLIENT, description
70     ) {
71
72     fun basicAuth(block: BasicAuthSshClientPropertiesAssignmentBuilder.() -> Unit) {
73         property(BluePrintConstants.PROPERTY_CONNECTION_CONFIG, BluePrintTypes.basicAuthSshProperties(block))
74     }
75 }
76
77 fun BluePrintTypes.basicAuthSshProperties(block: BasicAuthSshClientPropertiesAssignmentBuilder.() -> Unit): JsonNode {
78     val sshProperties = BasicAuthSshClientPropertiesAssignmentBuilder().apply(block).build()
79     sshProperties[SshClientProperties::type.name] = SshLibConstants.TYPE_BASIC_AUTH.asJsonPrimitive()
80     return sshProperties.asJsonType()
81 }
82
83 open class SshClientPropertiesAssignmentBuilder : PropertiesAssignmentBuilder() {
84
85     fun connectionTimeOut(connectionTimeOut: Int) = connectionTimeOut(connectionTimeOut.asJsonPrimitive())
86
87     fun connectionTimeOut(connectionTimeOut: JsonNode) =
88         property(SshClientProperties::connectionTimeOut.name, connectionTimeOut)
89
90     fun port(port: Int) = port(port.asJsonPrimitive())
91
92     fun port(port: JsonNode) = property(SshClientProperties::port.name, port)
93
94     fun host(host: String) = host(host.asJsonPrimitive())
95
96     fun host(host: JsonNode) = property(SshClientProperties::host.name, host)
97 }
98
99 class BasicAuthSshClientPropertiesAssignmentBuilder : SshClientPropertiesAssignmentBuilder() {
100
101     fun username(username: String) = username(username.asJsonPrimitive())
102
103     fun username(username: JsonNode) = property(BasicAuthSshClientProperties::username.name, username)
104
105     fun password(password: String) = password(password.asJsonPrimitive())
106
107     fun password(password: JsonNode) = property(BasicAuthSshClientProperties::password.name, password)
108 }