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