Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / cds / blueprintsprocessor / services / workflow / BlueprintSvcLogicService.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.blueprintsprocessor.services.workflow
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
21 import org.onap.ccsdk.sli.core.sli.*
22 import org.onap.ccsdk.sli.core.sli.provider.base.*
23 import org.slf4j.LoggerFactory
24 import org.slf4j.MDC
25 import org.springframework.beans.factory.annotation.Autowired
26 import org.springframework.context.ApplicationContext
27 import org.springframework.stereotype.Service
28 import java.util.*
29 import javax.annotation.PostConstruct
30
31 interface BlueprintSvcLogicService : SvcLogicServiceBase {
32
33     fun registerDefaultExecutors()
34
35     fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor)
36
37     fun unRegisterExecutors(name: String)
38
39     suspend fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>, input: Any): Any
40
41     @Deprecated("Populate Graph Dynamically from Blueprints, No need to get from Database Store ")
42     override fun getStore(): SvcLogicStore {
43         TODO("not implemented")
44     }
45
46     @Deprecated("Not used in Micro service Implementation")
47     override fun hasGraph(module: String, rpc: String, version: String?, mode: String): Boolean {
48         TODO("not implemented")
49     }
50
51     @Deprecated("Not used in Micro service Implementation")
52     override fun execute(p0: String?, p1: String?, p2: String?, p3: String?, p4: Properties?): Properties {
53         TODO("not implemented")
54     }
55 }
56
57
58 @Service
59 class DefaultBlueprintSvcLogicService : BlueprintSvcLogicService {
60
61     private val log = LoggerFactory.getLogger(DefaultBlueprintSvcLogicService::class.java)
62
63     private val nodeExecutors: MutableMap<String, AbstractSvcLogicNodeExecutor> = hashMapOf()
64
65     @Autowired
66     private lateinit var context: ApplicationContext
67
68     @PostConstruct
69     override fun registerDefaultExecutors() {
70
71         val executeNodeExecutor = context.getBean(ExecuteNodeExecutor::class.java)
72         registerExecutors("execute", executeNodeExecutor)
73         registerExecutors("block", BlockNodeExecutor())
74         registerExecutors("return", ReturnNodeExecutor())
75         registerExecutors("break", BreakNodeExecutor())
76         registerExecutors("exit", ExitNodeExecutor())
77     }
78
79     override fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor) {
80         log.info("Registering executors($name) with type(${svcLogicNodeExecutor.javaClass}")
81         nodeExecutors[name] = svcLogicNodeExecutor
82     }
83
84     override fun unRegisterExecutors(name: String) {
85         if (nodeExecutors.containsKey(name)) {
86             log.info("UnRegistering executors($name)")
87             nodeExecutors.remove(name)
88         }
89     }
90
91     override suspend fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>,
92                                  input: Any): Any {
93         //Initialise BlueprintSvcLogic Context with Blueprint Runtime Service and Input Request
94         val blueprintSvcLogicContext = BlueprintSvcLogicContext()
95         blueprintSvcLogicContext.setBluePrintRuntimeService(bluePrintRuntimeService)
96         blueprintSvcLogicContext.setRequest(input)
97         // Execute the Graph
98         execute(graph, blueprintSvcLogicContext)
99         // Get the Response
100         return blueprintSvcLogicContext.getResponse()
101     }
102
103     override fun executeNode(node: SvcLogicNode?, ctx: SvcLogicContext): SvcLogicNode? {
104         if (node == null) {
105             return null
106         } else {
107             if (log.isDebugEnabled) {
108                 log.debug("Executing node {}", node.nodeId)
109             }
110
111             val executor = this.nodeExecutors[node.nodeType]
112
113             if (executor != null) {
114                 log.debug("Executing node executor for node type {} - {}", node.nodeType, executor.javaClass.name)
115                 return executor.execute(this, node, ctx)
116             } else {
117                 throw SvcLogicException("Attempted to execute a node of type " + node.nodeType + ", but no executor was registered for this type")
118             }
119         }
120     }
121
122     override fun execute(graph: SvcLogicGraph, svcLogicContext: SvcLogicContext): SvcLogicContext {
123         MDC.put("currentGraph", graph.toString())
124
125         var curNode: SvcLogicNode? = graph.rootNode
126         log.info("About to execute graph {}", graph.toString())
127
128         try {
129             while (curNode != null) {
130                 MDC.put("nodeId", curNode.nodeId.toString() + " (" + curNode.nodeType + ")")
131                 log.info("About to execute node # {} ({})", curNode.nodeId, curNode.nodeType)
132                 val nextNode = this.executeNode(curNode, svcLogicContext)
133                 curNode = nextNode
134             }
135         } catch (var5: ExitNodeException) {
136             log.debug("SvcLogicServiceImpl caught ExitNodeException")
137         }
138
139         MDC.remove("nodeId")
140         MDC.remove("currentGraph")
141         return svcLogicContext
142     }
143 }