ab7d738562d6e3dd362eca9a3a01dde92105ed1d
[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.*
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker
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 : SvcLogicService {
32
33     fun registerDefaultExecutors()
34
35     fun registerExecutors(name: String, svcLogicNodeExecutor: SvcLogicNodeExecutor)
36
37     fun unRegisterExecutors(name: String)
38
39     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     @Deprecated("Not used in Micro service Implementation")
57     override fun execute(p0: String?, p1: String?, p2: String?, p3: String?, p4: Properties?, p5: DOMDataBroker?): Properties {
58         TODO("not implemented")
59     }
60 }
61
62
63 @Service
64 class DefaultBlueprintSvcLogicService : BlueprintSvcLogicService {
65
66     private val log = LoggerFactory.getLogger(DefaultBlueprintSvcLogicService::class.java)
67
68     private val nodeExecutors: MutableMap<String, SvcLogicNodeExecutor> = hashMapOf()
69
70     @Autowired
71     private lateinit var context: ApplicationContext
72
73     @PostConstruct
74     override fun registerDefaultExecutors() {
75
76         val executeNodeExecutor = context.getBean(ExecuteNodeExecutor::class.java)
77         registerExecutors("execute", executeNodeExecutor)
78         registerExecutors("block", BlockNodeExecutor())
79         registerExecutors("return", ReturnNodeExecutor())
80         registerExecutors("break", BreakNodeExecutor())
81         registerExecutors("exit", ExitNodeExecutor())
82     }
83
84     override fun registerExecutors(name: String, svcLogicNodeExecutor: SvcLogicNodeExecutor) {
85         log.info("Registering executors($name) with type(${svcLogicNodeExecutor.javaClass}")
86         nodeExecutors.put(name, svcLogicNodeExecutor)
87     }
88
89     override fun unRegisterExecutors(name: String) {
90         if (nodeExecutors.containsKey(name)) {
91             log.info("UnRegistering executors($name)")
92             nodeExecutors.remove(name)
93         }
94     }
95
96     override fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>, input: Any): Any {
97         //Initialise BlueprintSvcLogic Context with Blueprint Runtime Service and Input Request
98         val blueprintSvcLogicContext = BlueprintSvcLogicContext()
99         blueprintSvcLogicContext.setBluePrintRuntimeService(bluePrintRuntimeService)
100         blueprintSvcLogicContext.setRequest(input)
101         // Execute the Graph
102         execute(graph, blueprintSvcLogicContext)
103         // Get the Response
104         return blueprintSvcLogicContext.getResponse()
105     }
106
107     override fun executeNode(node: SvcLogicNode?, ctx: SvcLogicContext): SvcLogicNode? {
108         if (node == null) {
109             return null
110         } else {
111             if (log.isDebugEnabled()) {
112                 log.debug("Executing node {}", node.getNodeId())
113             }
114
115             val executor = this.nodeExecutors[node.getNodeType()]
116
117             if (executor != null) {
118                 log.debug("Executing node executor for node type {} - {}", node.getNodeType(), executor.javaClass.name)
119                 return executor.execute(this, node, ctx)
120             } else {
121                 throw SvcLogicException("Attempted to execute a node of type " + node.getNodeType() + ", but no executor was registered for this type")
122             }
123         }
124     }
125
126     override fun execute(graph: SvcLogicGraph, svcLogicContext: SvcLogicContext): SvcLogicContext {
127         MDC.put("currentGraph", graph.toString())
128
129         var curNode: SvcLogicNode? = graph.getRootNode()
130         log.info("About to execute graph {}", graph.toString())
131
132         try {
133             while (curNode != null) {
134                 MDC.put("nodeId", curNode.nodeId.toString() + " (" + curNode.nodeType + ")")
135                 log.info("About to execute node # {} ({})", curNode.nodeId, curNode.nodeType)
136                 val nextNode = this.executeNode(curNode, svcLogicContext)
137                 curNode = nextNode
138             }
139         } catch (var5: ExitNodeException) {
140             log.debug("SvcLogicServiceImpl caught ExitNodeException")
141         }
142
143         MDC.remove("nodeId")
144         MDC.remove("currentGraph")
145         return svcLogicContext
146     }
147 }