dfa22f68e1f165176bda1180b818c38d48f0ccb0
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.services.workflow
18
19 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
20 import org.onap.ccsdk.sli.core.sli.*
21 import org.onap.ccsdk.sli.core.sli.provider.base.*
22 import org.slf4j.LoggerFactory
23 import org.slf4j.MDC
24 import org.springframework.beans.factory.annotation.Autowired
25 import org.springframework.context.ApplicationContext
26 import org.springframework.stereotype.Service
27 import java.util.*
28 import javax.annotation.PostConstruct
29
30 interface BlueprintSvcLogicService : SvcLogicServiceBase {
31
32     fun registerDefaultExecutors()
33
34     fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor)
35
36     fun unRegisterExecutors(name: String)
37
38     fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>, input: Any): Any
39
40     @Deprecated("Populate Graph Dynamically from Blueprints, No need to get from Database Store ")
41     override fun getStore(): SvcLogicStore {
42         TODO("not implemented")
43     }
44
45     @Deprecated("Not used in Micro service Implementation")
46     override fun hasGraph(module: String, rpc: String, version: String?, mode: String): Boolean {
47         TODO("not implemented")
48     }
49
50     @Deprecated("Not used in Micro service Implementation")
51     override fun execute(p0: String?, p1: String?, p2: String?, p3: String?, p4: Properties?): Properties {
52         TODO("not implemented")
53     }
54 }
55
56
57 @Service
58 class DefaultBlueprintSvcLogicService : BlueprintSvcLogicService {
59
60     private val log = LoggerFactory.getLogger(DefaultBlueprintSvcLogicService::class.java)
61
62     private val nodeExecutors: MutableMap<String, AbstractSvcLogicNodeExecutor> = hashMapOf()
63
64     @Autowired
65     private lateinit var context: ApplicationContext
66
67     @PostConstruct
68     override fun registerDefaultExecutors() {
69
70         val executeNodeExecutor = context.getBean(ExecuteNodeExecutor::class.java)
71         registerExecutors("execute", executeNodeExecutor)
72         registerExecutors("block", BlockNodeExecutor())
73         registerExecutors("return", ReturnNodeExecutor())
74         registerExecutors("break", BreakNodeExecutor())
75         registerExecutors("exit", ExitNodeExecutor())
76     }
77
78     override fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor) {
79         log.info("Registering executors($name) with type(${svcLogicNodeExecutor.javaClass}")
80         nodeExecutors[name] = svcLogicNodeExecutor
81     }
82
83     override fun unRegisterExecutors(name: String) {
84         if (nodeExecutors.containsKey(name)) {
85             log.info("UnRegistering executors($name)")
86             nodeExecutors.remove(name)
87         }
88     }
89
90     override fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>, input: Any): Any {
91         //Initialise BlueprintSvcLogic Context with Blueprint Runtime Service and Input Request
92         val blueprintSvcLogicContext = BlueprintSvcLogicContext()
93         blueprintSvcLogicContext.setBluePrintRuntimeService(bluePrintRuntimeService)
94         blueprintSvcLogicContext.setRequest(input)
95         // Execute the Graph
96         execute(graph, blueprintSvcLogicContext)
97         // Get the Response
98         return blueprintSvcLogicContext.getResponse()
99     }
100
101     override fun executeNode(node: SvcLogicNode?, ctx: SvcLogicContext): SvcLogicNode? {
102         if (node == null) {
103             return null
104         } else {
105             if (log.isDebugEnabled) {
106                 log.debug("Executing node {}", node.nodeId)
107             }
108
109             val executor = this.nodeExecutors[node.nodeType]
110
111             if (executor != null) {
112                 log.debug("Executing node executor for node type {} - {}", node.nodeType, executor.javaClass.name)
113                 return executor.execute(this, node, ctx)
114             } else {
115                 throw SvcLogicException("Attempted to execute a node of type " + node.nodeType + ", but no executor was registered for this type")
116             }
117         }
118     }
119
120     override fun execute(graph: SvcLogicGraph, svcLogicContext: SvcLogicContext): SvcLogicContext {
121         MDC.put("currentGraph", graph.toString())
122
123         var curNode: SvcLogicNode? = graph.rootNode
124         log.info("About to execute graph {}", graph.toString())
125
126         try {
127             while (curNode != null) {
128                 MDC.put("nodeId", curNode.nodeId.toString() + " (" + curNode.nodeType + ")")
129                 log.info("About to execute node # {} ({})", curNode.nodeId, curNode.nodeType)
130                 val nextNode = this.executeNode(curNode, svcLogicContext)
131                 curNode = nextNode
132             }
133         } catch (var5: ExitNodeException) {
134             log.debug("SvcLogicServiceImpl caught ExitNodeException")
135         }
136
137         MDC.remove("nodeId")
138         MDC.remove("currentGraph")
139         return svcLogicContext
140     }
141 }