8750d98b33b3a8144372cda66f22a70d83089e2a
[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<*>): 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<*>): Any {
97         //Initialise BlueprintSvcLogic Context
98         val blueprintSvcLogicContext = BlueprintSvcLogicContext()
99         blueprintSvcLogicContext.setBluePrintRuntimeService(bluePrintRuntimeService)
100         // Execute the Graph
101         execute(graph, blueprintSvcLogicContext)
102         // Get the Response
103         return blueprintSvcLogicContext.getResponse()
104     }
105
106     override fun executeNode(node: SvcLogicNode?, ctx: SvcLogicContext): SvcLogicNode? {
107         if (node == null) {
108             return null
109         } else {
110             if (log.isDebugEnabled()) {
111                 log.debug("Executing node {}", node.getNodeId())
112             }
113
114             val executor = this.nodeExecutors[node.getNodeType()]
115
116             if (executor != null) {
117                 log.debug("Executing node executor for node type {} - {}", node.getNodeType(), executor.javaClass.name)
118                 return executor.execute(this, node, ctx)
119             } else {
120                 throw SvcLogicException("Attempted to execute a node of type " + node.getNodeType() + ", but no executor was registered for this type")
121             }
122         }
123     }
124
125     override fun execute(graph: SvcLogicGraph, svcLogicContext: SvcLogicContext): SvcLogicContext {
126         MDC.put("currentGraph", graph.toString())
127
128         val ctx = svcLogicContext as BlueprintSvcLogicContext
129
130         val blueprintRuntimeService = ctx.getBluePrintService()
131
132         log.info("Blueprint Runtime Service : ${blueprintRuntimeService}")
133
134         var curNode: SvcLogicNode? = graph.getRootNode()
135         log.info("About to execute graph {}", graph.toString())
136
137         try {
138             while (curNode != null) {
139                 MDC.put("nodeId", curNode.nodeId.toString() + " (" + curNode.nodeType + ")")
140                 log.info("About to execute node # {} ({})", curNode.nodeId, curNode.nodeType)
141                 val nextNode = this.executeNode(curNode, ctx)
142                 curNode = nextNode
143             }
144         } catch (var5: ExitNodeException) {
145             log.debug("SvcLogicServiceImpl caught ExitNodeException")
146         }
147
148         MDC.remove("nodeId")
149         MDC.remove("currentGraph")
150         return ctx
151     }
152 }