2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.services.workflow
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
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
42 interface BlueprintSvcLogicService : SvcLogicServiceBase {
44 fun registerDefaultExecutors()
46 fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor)
48 fun unRegisterExecutors(name: String)
50 suspend fun execute(graph: SvcLogicGraph, bluePrintRuntimeService: BluePrintRuntimeService<*>, input: Any): Any
52 override fun execute(module: String, rpc: String, version: String, mode: String, ctx: SvcLogicContext): SvcLogicContext {
53 TODO("not implemented")
56 @Deprecated("Populate Graph Dynamically from Blueprints, No need to get from Database Store ")
57 override fun getStore(): SvcLogicStore {
58 TODO("not implemented")
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")
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")
73 class DefaultBlueprintSvcLogicService : BlueprintSvcLogicService {
75 private val log = LoggerFactory.getLogger(DefaultBlueprintSvcLogicService::class.java)
77 private val nodeExecutors: MutableMap<String, AbstractSvcLogicNodeExecutor> = hashMapOf()
80 private lateinit var context: ApplicationContext
83 override fun registerDefaultExecutors() {
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())
93 override fun registerExecutors(name: String, svcLogicNodeExecutor: AbstractSvcLogicNodeExecutor) {
94 log.debug("Registering executors($name) with type(${svcLogicNodeExecutor.javaClass}")
95 nodeExecutors[name] = svcLogicNodeExecutor
98 override fun unRegisterExecutors(name: String) {
99 if (nodeExecutors.containsKey(name)) {
100 log.info("UnRegistering executors($name)")
101 nodeExecutors.remove(name)
105 override suspend fun execute(
106 graph: SvcLogicGraph,
107 bluePrintRuntimeService: BluePrintRuntimeService<*>,
110 // Initialise BlueprintSvcLogic Context with Blueprint Runtime Service and Input Request
111 val blueprintSvcLogicContext = BlueprintSvcLogicContext()
112 blueprintSvcLogicContext.setBluePrintRuntimeService(bluePrintRuntimeService)
113 blueprintSvcLogicContext.setRequest(input)
115 execute(graph, blueprintSvcLogicContext)
117 return blueprintSvcLogicContext.getResponse()
120 override fun executeNode(node: SvcLogicNode?, ctx: SvcLogicContext): SvcLogicNode? {
124 if (log.isDebugEnabled) {
125 log.debug("Executing node {}", node.nodeId)
128 val executor = this.nodeExecutors[node.nodeType]
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)
134 throw SvcLogicException("Attempted to execute a node of type " + node.nodeType + ", but no executor was registered for this type")
139 override fun execute(graph: SvcLogicGraph, svcLogicContext: SvcLogicContext): SvcLogicContext {
140 MDC.put("currentGraph", graph.toString())
142 var curNode: SvcLogicNode? = graph.rootNode
143 log.info("About to execute graph {}", graph.toString())
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)
152 } catch (var5: ExitNodeException) {
153 log.debug("SvcLogicServiceImpl caught ExitNodeException")
157 MDC.remove("currentGraph")
158 return svcLogicContext