rt_sink.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "fmt"
  13. "math"
  14. "strings"
  15. "devt.de/krotik/ecal/engine"
  16. "devt.de/krotik/ecal/parser"
  17. "devt.de/krotik/ecal/scope"
  18. "devt.de/krotik/ecal/util"
  19. )
  20. /*
  21. sinkRuntime is the runtime for sink declarations.
  22. */
  23. type sinkRuntime struct {
  24. *baseRuntime
  25. }
  26. /*
  27. sinkRuntimeInst returns a new runtime component instance.
  28. */
  29. func sinkRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  30. return &sinkRuntime{newBaseRuntime(erp, node)}
  31. }
  32. /*
  33. Validate this node and all its child nodes.
  34. */
  35. func (rt *sinkRuntime) Validate() error {
  36. err := rt.baseRuntime.Validate()
  37. if err == nil {
  38. // Check that all children are valid
  39. for _, child := range rt.node.Children[1:] {
  40. switch child.Name {
  41. case parser.NodeKINDMATCH:
  42. case parser.NodeSCOPEMATCH:
  43. case parser.NodeSTATEMATCH:
  44. case parser.NodePRIORITY:
  45. case parser.NodeSUPPRESSES:
  46. case parser.NodeSTATEMENTS:
  47. continue
  48. default:
  49. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  50. fmt.Sprintf("Unknown expression in sink declaration %v", child.Token.Val),
  51. child)
  52. }
  53. if err != nil {
  54. break
  55. }
  56. }
  57. }
  58. return err
  59. }
  60. /*
  61. Eval evaluate this runtime component.
  62. */
  63. func (rt *sinkRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  64. _, err := rt.baseRuntime.Eval(vs, is, tid)
  65. if err == nil {
  66. var rule *engine.Rule
  67. var statements *parser.ASTNode
  68. rule, statements, err = rt.createRule(vs, is, tid)
  69. if err == nil && statements != nil {
  70. if len(rt.node.Meta) > 0 &&
  71. (rt.node.Meta[0].Type() == parser.MetaDataPreComment ||
  72. rt.node.Meta[0].Type() == parser.MetaDataPostComment) {
  73. rule.Desc = strings.TrimSpace(rt.node.Meta[0].Value())
  74. }
  75. rule.Action = func(p engine.Processor, m engine.Monitor, e *engine.Event, tid uint64) error { // Action of the rule
  76. // Create a new root variable scope
  77. sinkVS := scope.NewScope(fmt.Sprintf("sink: %v", rule.Name))
  78. // Create a new instance state with the monitor - everything called
  79. // by the rule will have access to the current monitor.
  80. sinkIs := map[string]interface{}{
  81. "monitor": m,
  82. }
  83. err = sinkVS.SetValue("event", map[interface{}]interface{}{
  84. "name": e.Name(),
  85. "kind": strings.Join(e.Kind(), engine.RuleKindSeparator),
  86. "state": e.State(),
  87. })
  88. if err == nil {
  89. scope.SetParentOfScope(sinkVS, vs)
  90. if _, err = statements.Runtime.Eval(sinkVS, sinkIs, tid); err != nil {
  91. if sre, ok := err.(*util.RuntimeErrorWithDetail); ok {
  92. sre.Environment = sinkVS
  93. } else {
  94. var data interface{}
  95. rerr := rt.erp.NewRuntimeError(util.ErrSink, err.Error(), rt.node).(*util.RuntimeError)
  96. if e, ok := err.(*util.RuntimeError); ok {
  97. rerr = e
  98. } else if r, ok := err.(*returnValue); ok {
  99. rerr = r.RuntimeError
  100. data = r.returnValue
  101. }
  102. // Provide additional information for unexpected errors
  103. err = &util.RuntimeErrorWithDetail{
  104. RuntimeError: rerr,
  105. Environment: sinkVS,
  106. Data: data,
  107. }
  108. }
  109. }
  110. }
  111. return err
  112. }
  113. if err = rt.erp.Processor.AddRule(rule); err != nil {
  114. err = rt.erp.NewRuntimeError(util.ErrInvalidState, err.Error(), rt.node)
  115. }
  116. }
  117. }
  118. return nil, err
  119. }
  120. /*
  121. createRule creates a rule for the ECA engine.
  122. */
  123. func (rt *sinkRuntime) createRule(vs parser.Scope, is map[string]interface{},
  124. tid uint64) (*engine.Rule, *parser.ASTNode, error) {
  125. var kindMatch, scopeMatch, suppresses []string
  126. var stateMatch map[string]interface{}
  127. var priority int
  128. var statements *parser.ASTNode
  129. var err error
  130. // Create default scope
  131. scopeMatch = []string{}
  132. // Get sink name
  133. sinkName := fmt.Sprint(rt.node.Children[0].Token.Val)
  134. // Collect values from children
  135. for _, child := range rt.node.Children[1:] {
  136. switch child.Name {
  137. case parser.NodeKINDMATCH:
  138. kindMatch, err = rt.makeStringList(child, vs, is, tid)
  139. break
  140. case parser.NodeSCOPEMATCH:
  141. scopeMatch, err = rt.makeStringList(child, vs, is, tid)
  142. break
  143. case parser.NodeSTATEMATCH:
  144. var val interface{}
  145. stateMatch = make(map[string]interface{})
  146. if val, err = child.Runtime.Eval(vs, is, tid); err == nil {
  147. for k, v := range val.(map[interface{}]interface{}) {
  148. stateMatch[fmt.Sprint(k)] = v
  149. }
  150. }
  151. break
  152. case parser.NodePRIORITY:
  153. var val interface{}
  154. if val, err = child.Runtime.Eval(vs, is, tid); err == nil {
  155. priority = int(math.Floor(val.(float64)))
  156. }
  157. break
  158. case parser.NodeSUPPRESSES:
  159. suppresses, err = rt.makeStringList(child, vs, is, tid)
  160. break
  161. case parser.NodeSTATEMENTS:
  162. statements = child
  163. break
  164. }
  165. if err != nil {
  166. break
  167. }
  168. }
  169. return &engine.Rule{
  170. Name: sinkName, // Name
  171. KindMatch: kindMatch, // Kind match
  172. ScopeMatch: scopeMatch, // Match on event cascade scope
  173. StateMatch: stateMatch, // No state match
  174. Priority: priority, // Priority of the rule
  175. SuppressionList: suppresses, // List of suppressed rules by this rule
  176. }, statements, err
  177. }
  178. /*
  179. makeStringList evaluates a given child node into a list of strings.
  180. */
  181. func (rt *sinkRuntime) makeStringList(child *parser.ASTNode, vs parser.Scope,
  182. is map[string]interface{}, tid uint64) ([]string, error) {
  183. var ret []string
  184. val, err := child.Runtime.Eval(vs, is, tid)
  185. if err == nil {
  186. for _, v := range val.([]interface{}) {
  187. ret = append(ret, fmt.Sprint(v))
  188. }
  189. }
  190. return ret, err
  191. }
  192. // Sink child nodes
  193. // ================
  194. /*
  195. sinkDetailRuntime is the runtime for sink detail declarations.
  196. */
  197. type sinkDetailRuntime struct {
  198. *baseRuntime
  199. valType string
  200. }
  201. /*
  202. Eval evaluate this runtime component.
  203. */
  204. func (rt *sinkDetailRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  205. var ret interface{}
  206. _, err := rt.baseRuntime.Eval(vs, is, tid)
  207. if err == nil {
  208. if ret, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  209. // Check value is of expected type
  210. if rt.valType == "list" {
  211. if _, ok := ret.([]interface{}); !ok {
  212. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  213. fmt.Sprintf("Expected a list as value"),
  214. rt.node)
  215. }
  216. } else if rt.valType == "map" {
  217. if _, ok := ret.(map[interface{}]interface{}); !ok {
  218. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  219. fmt.Sprintf("Expected a map as value"),
  220. rt.node)
  221. }
  222. } else if rt.valType == "int" {
  223. if _, ok := ret.(float64); !ok {
  224. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  225. fmt.Sprintf("Expected a number as value"),
  226. rt.node)
  227. }
  228. }
  229. }
  230. }
  231. return ret, err
  232. }
  233. /*
  234. kindMatchRuntimeInst returns a new runtime component instance.
  235. */
  236. func kindMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  237. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  238. }
  239. /*
  240. scopeMatchRuntimeInst returns a new runtime component instance.
  241. */
  242. func scopeMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  243. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  244. }
  245. /*
  246. stateMatchRuntimeInst returns a new runtime component instance.
  247. */
  248. func stateMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  249. return &sinkDetailRuntime{newBaseRuntime(erp, node), "map"}
  250. }
  251. /*
  252. priorityRuntimeInst returns a new runtime component instance.
  253. */
  254. func priorityRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  255. return &sinkDetailRuntime{newBaseRuntime(erp, node), "int"}
  256. }
  257. /*
  258. suppressesRuntimeInst returns a new runtime component instance.
  259. */
  260. func suppressesRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  261. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  262. }