Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
22 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTypeValidatorService
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintWorkflowValidator
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
25 import org.onap.ccsdk.cds.controllerblueprints.validation.utils.PropertyAssignmentValidationUtils
26 import org.slf4j.LoggerFactory
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         paths.add(workflowName)
46         paths.joinToString(BlueprintConstants.PATH_DIVIDER)
47
48         // Validate Workflow Inputs
49         validateInputs(workflow)
50
51         // Validate Workflow outputs
52         validateOutputs(workflow)
53
54         // Step Validation Start
55         paths.add("steps")
56         workflow.steps?.forEach { stepName, step ->
57             paths.add(stepName)
58             paths.joinToString(BlueprintConstants.PATH_DIVIDER)
59
60             // Validate target
61             step.target?.let {
62                 try {
63                     val nodeTemplate = bluePrintRuntimeService.bluePrintContext().nodeTemplateByName(it)
64
65                     val nodeTypeDerivedFrom = bluePrintRuntimeService.bluePrintContext().nodeTemplateNodeType(it).derivedFrom
66
67                     check(
68                         nodeTypeDerivedFrom == BlueprintConstants.MODEL_TYPE_NODE_WORKFLOW ||
69                             nodeTypeDerivedFrom == BlueprintConstants.MODEL_TYPE_NODE_COMPONENT
70                     ) {
71                         "NodeType(${nodeTemplate.type}) derived from is '$nodeTypeDerivedFrom', Expected " +
72                             "'${BlueprintConstants.MODEL_TYPE_NODE_WORKFLOW}' or '${BlueprintConstants.MODEL_TYPE_NODE_COMPONENT}'"
73                     }
74                 } catch (e: Exception) {
75                     bluePrintRuntimeService.getBlueprintError()
76                         .addError(
77                             "Failed to validate Workflow($workflowName)'s step($stepName)'s " +
78                                 "definition",
79                             paths.joinToString(BlueprintConstants.PATH_DIVIDER), e.message!!
80                         )
81                 }
82             }
83         }
84         paths.removeAt(paths.lastIndex)
85         // Step Validation Ends
86         paths.removeAt(paths.lastIndex)
87
88         paths.removeAt(paths.lastIndex)
89     }
90
91     private fun validateInputs(workflow: Workflow) {
92         workflow.inputs?.let {
93             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.inputs!!)
94         }
95     }
96
97     private fun validateOutputs(workflow: Workflow) {
98         workflow.outputs?.let {
99
100             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.outputs!!)
101
102             PropertyAssignmentValidationUtils(bluePrintRuntimeService.bluePrintContext())
103                 .validatePropertyDefinitionNAssignments(workflow.outputs!!)
104         }
105         // Validate Value or Expression
106         workflow.outputs?.forEach { propertyName, propertyDefinition ->
107         }
108     }
109 }