Improve Rest Service API
[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 com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import org.onap.ccsdk.cds.controllerblueprints.validation.utils.PropertyAssignmentValidationUtils
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowValidator
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Service
31
32 @Service("default-workflow-validator")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class BluePrintWorkflowValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintWorkflowValidator {
35
36     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
37     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
38
39     var paths: MutableList<String> = arrayListOf()
40
41     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, workflowName: String, workflow: Workflow) {
42         log.info("Validating Workflow($workflowName)")
43
44         this.bluePrintRuntimeService = bluePrintRuntimeService
45
46
47         paths.add(workflowName)
48         paths.joinToString(BluePrintConstants.PATH_DIVIDER)
49
50         // Validate Workflow Inputs
51         validateInputs(workflow)
52
53         // Validate Workflow outputs
54         validateOutputs(workflow)
55
56         // Step Validation Start
57         paths.add("steps")
58         workflow.steps?.forEach { stepName, step ->
59             paths.add(stepName)
60             paths.joinToString(BluePrintConstants.PATH_DIVIDER)
61
62             // Validate target
63             step.target?.let {
64                 try {
65                     val nodeTemplate = bluePrintRuntimeService.bluePrintContext().nodeTemplateByName(it)
66
67                     val nodeTypeDerivedFrom = bluePrintRuntimeService.bluePrintContext().nodeTemplateNodeType(it).derivedFrom
68
69                     check(nodeTypeDerivedFrom == BluePrintConstants.MODEL_TYPE_NODE_DG
70                             || nodeTypeDerivedFrom == BluePrintConstants.MODEL_TYPE_NODE_COMPONENT) {
71                         "NodeType(${nodeTemplate.type}) derived from is '$nodeTypeDerivedFrom', Expected " +
72                                 "'${BluePrintConstants.MODEL_TYPE_NODE_DG}' or '${BluePrintConstants.MODEL_TYPE_NODE_COMPONENT}'"
73                     }
74                 } catch (e: Exception) {
75                     bluePrintRuntimeService.getBluePrintError()
76                             .addError("Failed to validate Workflow($workflowName)'s step($stepName)'s " +
77                                     "definition", paths.joinToString(BluePrintConstants.PATH_DIVIDER), e.message!!)
78                 }
79             }
80
81         }
82         paths.removeAt(paths.lastIndex)
83         // Step Validation Ends
84         paths.removeAt(paths.lastIndex)
85
86         paths.removeAt(paths.lastIndex)
87     }
88
89     private fun validateInputs(workflow: Workflow) {
90         workflow.inputs?.let {
91             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.inputs!!)
92         }
93     }
94
95     private fun validateOutputs(workflow: Workflow) {
96         workflow.outputs?.let {
97
98             bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, workflow.outputs!!)
99
100             PropertyAssignmentValidationUtils(bluePrintRuntimeService.bluePrintContext())
101                     .validatePropertyDefinitionNAssignments(workflow.outputs!!)
102         }
103         // Validate Value or Expression
104         workflow.outputs?.forEach { propertyName, propertyDefinition ->
105
106         }
107     }
108
109 }