6573d0e9a792f286714b3c4e4b41df0ee60163dd
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
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.functions.resource.resolution
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.asJsonString
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
27 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateOperationImplBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
29 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.ServiceTemplateBuilder
30 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
31 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
32 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
33
34 /** Component Extensions **/
35 fun ServiceTemplateBuilder.nodeTypeComponentResourceResolution() {
36     val nodeType = BluePrintTypes.nodeTypeComponentResourceResolution()
37     if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
38     this.nodeTypes!![nodeType.id!!] = nodeType
39 }
40
41 fun BluePrintTypes.nodeTypeComponentResourceResolution(): NodeType {
42     return nodeType(
43         id = "component-resource-resolution", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
44         derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
45         description = "Resource Assignment Component"
46     ) {
47
48         attribute(
49             ResourceResolutionComponent.ATTRIBUTE_ASSIGNMENT_PARAM, BluePrintConstants.DATA_TYPE_STRING,
50             true
51         )
52         attribute(
53             ResourceResolutionComponent.ATTRIBUTE_STATUS, BluePrintConstants.DATA_TYPE_STRING,
54             true
55         )
56
57         operation("ResourceResolutionComponent", "ResourceResolutionComponent Operation") {
58             inputs {
59                 property(
60                     ResourceResolutionComponent.INPUT_REQUEST_ID, BluePrintConstants.DATA_TYPE_STRING,
61                     true, "Request Id, Unique Id for the request."
62                 )
63
64                 property(
65                     ResourceResolutionComponent.INPUT_RESOURCE_ID, BluePrintConstants.DATA_TYPE_STRING,
66                     false, "Resource Id."
67                 )
68
69                 property(
70                     ResourceResolutionComponent.INPUT_ACTION_NAME, BluePrintConstants.DATA_TYPE_STRING,
71                     false, "Action Name of the process"
72                 )
73
74                 property(
75                     ResourceResolutionComponent.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
76                     false, "Dynamic Json Content or DSL Json reference."
77                 )
78
79                 property(
80                     ResourceResolutionComponent.INPUT_RESOLUTION_KEY, BluePrintConstants.DATA_TYPE_STRING,
81                     false, "Key for service instance related correlation."
82                 )
83
84                 property(
85                     ResourceResolutionComponent.INPUT_OCCURRENCE, BluePrintConstants.DATA_TYPE_INTEGER,
86                     false, "Number of time to perform the resolution."
87                 ) {
88                     defaultValue(1)
89                 }
90
91                 property(
92                     ResourceResolutionComponent.INPUT_STORE_RESULT, BluePrintConstants.DATA_TYPE_BOOLEAN,
93                     false, "Whether or not to store the output."
94                 )
95
96                 property(
97                     ResourceResolutionComponent.INPUT_RESOURCE_TYPE, BluePrintConstants.DATA_TYPE_STRING,
98                     false, "Request type."
99                 )
100
101                 property(
102                     ResourceResolutionComponent.INPUT_ARTIFACT_PREFIX_NAMES, BluePrintConstants.DATA_TYPE_LIST,
103                     true, "Template , Resource Assignment Artifact Prefix names"
104                 ) {
105                     entrySchema(BluePrintConstants.DATA_TYPE_STRING)
106                 }
107             }
108             outputs {
109                 property(
110                     ResourceResolutionComponent.OUTPUT_RESOURCE_ASSIGNMENT_PARAMS, BluePrintConstants.DATA_TYPE_STRING,
111                     true, "Output Response"
112                 )
113                 property(
114                     ResourceResolutionComponent.OUTPUT_STATUS, BluePrintConstants.DATA_TYPE_STRING,
115                     true, "Status of the Component Execution ( success or failure )"
116                 )
117             }
118         }
119     }
120 }
121
122 /** Component Builder */
123 fun TopologyTemplateBuilder.nodeTemplateComponentResourceResolution(
124     id: String,
125     description: String,
126     block: ComponentResourceResolutionNodeTemplateBuilder.() -> Unit
127 ) {
128     val nodeTemplate = BluePrintTypes.nodeTemplateComponentResourceResolution(
129         id, description,
130         block
131     )
132     if (nodeTemplates == null) nodeTemplates = hashMapOf()
133     nodeTemplates!![nodeTemplate.id!!] = nodeTemplate
134 }
135
136 fun BluePrintTypes.nodeTemplateComponentResourceResolution(
137     id: String,
138     description: String,
139     block: ComponentResourceResolutionNodeTemplateBuilder.() -> Unit
140 ): NodeTemplate {
141     return ComponentResourceResolutionNodeTemplateBuilder(id, description).apply(block).build()
142 }
143
144 class ComponentResourceResolutionNodeTemplateBuilder(id: String, description: String) :
145     AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder,
146         ComponentResourceResolutionNodeTemplateBuilder.InputsBuilder,
147         ComponentResourceResolutionNodeTemplateBuilder.OutputsBuilder>(
148         id, "component-script-executor",
149         "ComponentResourceResolution",
150         description
151     ) {
152
153     class InputsBuilder : PropertiesAssignmentBuilder() {
154
155         fun requestId(requestId: String) = requestId(requestId.asJsonPrimitive())
156
157         fun requestId(requestId: JsonNode) {
158             property(ResourceResolutionComponent.INPUT_REQUEST_ID, requestId)
159         }
160
161         fun resourceId(resourceId: String) = resourceId(resourceId.asJsonPrimitive())
162
163         fun resourceId(resourceId: JsonNode) {
164             property(ResourceResolutionComponent.INPUT_RESOURCE_ID, resourceId)
165         }
166
167         fun actionName(actionName: String) = actionName(actionName.asJsonPrimitive())
168
169         fun actionName(actionName: JsonNode) {
170             property(ResourceResolutionComponent.INPUT_ACTION_NAME, actionName)
171         }
172
173         fun resolutionKey(resolutionKey: String) = resolutionKey(resolutionKey.asJsonPrimitive())
174
175         fun resolutionKey(resolutionKey: JsonNode) {
176             property(ResourceResolutionComponent.INPUT_RESOLUTION_KEY, resolutionKey)
177         }
178
179         fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
180
181         fun dynamicProperties(dynamicProperties: JsonNode) {
182             property(ResourceResolutionComponent.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
183         }
184
185         fun occurrence(occurrence: Int) = occurrence(occurrence.asJsonPrimitive())
186
187         fun occurrence(resolutionKey: JsonNode) {
188             property(ResourceResolutionComponent.INPUT_OCCURRENCE, resolutionKey)
189         }
190
191         fun storeResult(storeResult: Boolean) = storeResult(storeResult.asJsonPrimitive())
192
193         fun storeResult(storeResult: JsonNode) {
194             property(ResourceResolutionComponent.INPUT_STORE_RESULT, storeResult)
195         }
196
197         fun resourceType(resourceType: String) = resourceType(resourceType.asJsonPrimitive())
198
199         fun resourceType(resourceType: JsonNode) {
200             property(ResourceResolutionComponent.INPUT_RESOURCE_TYPE, resourceType)
201         }
202
203         fun artifactPrefixNames(artifactPrefixNames: String) = artifactPrefixNames(artifactPrefixNames.jsonAsJsonType())
204
205         fun artifactPrefixNames(artifactPrefixNameList: List<String>) =
206             artifactPrefixNames(artifactPrefixNameList.asJsonString())
207
208         fun artifactPrefixNames(artifactPrefixNames: JsonNode) {
209             property(ResourceResolutionComponent.INPUT_ARTIFACT_PREFIX_NAMES, artifactPrefixNames)
210         }
211     }
212
213     class OutputsBuilder : PropertiesAssignmentBuilder() {
214
215         fun status(status: String) = status(status.asJsonPrimitive())
216
217         fun status(status: JsonNode) {
218             property(ResourceResolutionComponent.OUTPUT_STATUS, status)
219         }
220
221         fun resourceAssignmentParams(resourceAssignmentParams: String) =
222             resourceAssignmentParams(resourceAssignmentParams.asJsonType())
223
224         fun resourceAssignmentParams(resourceAssignmentParams: JsonNode) {
225             property(ResourceResolutionComponent.OUTPUT_RESOURCE_ASSIGNMENT_PARAMS, resourceAssignmentParams)
226         }
227     }
228 }