e705808c9b54dcf1bca09695a1597169ede9530b
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-validation / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / validation / BluePrintWorkflowValidatorImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.validation
19
20 import org.slf4j.LoggerFactory
21 import org.onap.ccsdk.cds.controllerblueprints.validation.utils.PropertyAssignmentValidationUtils
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowValidator
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
27 import org.springframework.beans.factory.config.ConfigurableBeanFactory
28 import org.springframework.context.annotation.Scope
29 import org.springframework.stereotype.Service
30
31 @Service("default-workflow-validator")
32 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
33 open class BluePrintWorkflowValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintWorkflowValidator {
34
35     private val log= LoggerFactory.getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
36     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
37
38     var paths: MutableList<String> = arrayListOf()
39
40     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, workflowName: String, workflow: Workflow) {
41         log.info("Validating Workflow($workflowName)")
42
43         this.bluePrintRuntimeService = bluePrintRuntimeService
44
45
46         paths.add(workflowName)
47         paths.joinToString(BluePrintConstants.PATH_DIVIDER)
48
49         // Validate Workflow Inputs
50         validateInputs(workflow)
51
52         // Validate Workflow outputs
53         validateOutputs(workflow)
54
55         // Step Validation Start
56         paths.add("steps")
57         workflow.steps?.forEach { stepName, step ->
58             paths.add(stepName)
59             paths.joinToString(BluePrintConstants.PATH_DIVIDER)
60
61             // Validate target
62             step.target?.let {
63                 try {
64                     val nodeTemplate = bluePrintRuntimeService.bluePrintContext().nodeTemplateByName(it)
65
66                     val nodeTypeDerivedFrom = bluePrintRuntimeService.bluePrintContext().nodeTemplateNodeType(it).derivedFrom
67
68                     check(nodeTypeDerivedFrom == BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW
69                             || nodeTypeDerivedFrom == BluePrintConstants.MODEL_TYPE_NODE_COMPONENT) {
70                         "NodeType(${nodeTemplate.type}) derived from is '$nodeTypeDerivedFrom', Expected " +
71                                 "'${BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW}' or '${BluePrintConstants.MODEL_TYPE_NODE_COMPONENT}'"
72                     }
73                 } catch (e: Exception) {
74                     bluePrintRuntimeService.getBluePrintError()
75                             .addError("Failed to validate Workflow($workflowName)'s step($stepName)'s " +
76                                     "definition", paths.joinToString(BluePrintConstants.PATH_DIVIDER), e.message!!)
77                 }
78             }
79
80         }
81         paths.removeAt(paths.lastIndex)
82         // Step Validation Ends
83         paths.removeAt(paths.lastIndex)
84
85         paths.removeAt(paths.lastIndex)
86     }
87
88     private fun validateInputs(workflow: Workflow) {
89         workflow.inputs?.let {
90             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.inputs!!)
91         }
92     }
93
94     private fun validateOutputs(workflow: Workflow) {
95         workflow.outputs?.let {
96
97             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.outputs!!)
98
99             PropertyAssignmentValidationUtils(bluePrintRuntimeService.bluePrintContext())
100                     .validatePropertyDefinitionNAssignments(workflow.outputs!!)
101         }
102         // Validate Value or Expression
103         workflow.outputs?.forEach { propertyName, propertyDefinition ->
104
105         }
106     }
107
108 }