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