rt_sink.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. nameNode := rt.node.Children[0]
  39. // Make sure there is a constant as a name
  40. if _, ok := nameNode.Runtime.(*identifierRuntime); !ok {
  41. return rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  42. "Must have an identifier as sink name", rt.node)
  43. }
  44. // Check that all children are valid
  45. for _, child := range rt.node.Children[1:] {
  46. switch child.Name {
  47. case parser.NodeKINDMATCH:
  48. case parser.NodeSCOPEMATCH:
  49. case parser.NodeSTATEMATCH:
  50. case parser.NodePRIORITY:
  51. case parser.NodeSUPPRESSES:
  52. case parser.NodeSTATEMENTS:
  53. continue
  54. default:
  55. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  56. fmt.Sprintf("Unknown expression in sink declaration %v", child.Token.Val),
  57. child)
  58. }
  59. if err != nil {
  60. break
  61. }
  62. }
  63. }
  64. return err
  65. }
  66. /*
  67. Eval evaluate this runtime component.
  68. */
  69. func (rt *sinkRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  70. var kindMatch, scopeMatch, suppresses []string
  71. var stateMatch map[string]interface{}
  72. var priority int
  73. var statements *parser.ASTNode
  74. _, err := rt.baseRuntime.Eval(vs, is)
  75. if err == nil {
  76. // Create default scope
  77. scopeMatch = []string{}
  78. // Get the name of the sink
  79. name := rt.node.Children[0].Token.Val
  80. // Create helper function
  81. makeStringList := func(child *parser.ASTNode) ([]string, error) {
  82. var ret []string
  83. val, err := child.Runtime.Eval(vs, is)
  84. if err == nil {
  85. for _, v := range val.([]interface{}) {
  86. ret = append(ret, fmt.Sprint(v))
  87. }
  88. }
  89. return ret, err
  90. }
  91. // Collect values from children
  92. for _, child := range rt.node.Children[1:] {
  93. switch child.Name {
  94. case parser.NodeKINDMATCH:
  95. kindMatch, err = makeStringList(child)
  96. break
  97. case parser.NodeSCOPEMATCH:
  98. scopeMatch, err = makeStringList(child)
  99. break
  100. case parser.NodeSTATEMATCH:
  101. var val interface{}
  102. stateMatch = make(map[string]interface{})
  103. if val, err = child.Runtime.Eval(vs, is); err == nil {
  104. for k, v := range val.(map[interface{}]interface{}) {
  105. stateMatch[fmt.Sprint(k)] = v
  106. }
  107. }
  108. break
  109. case parser.NodePRIORITY:
  110. var val interface{}
  111. if val, err = child.Runtime.Eval(vs, is); err == nil {
  112. priority = int(math.Floor(val.(float64)))
  113. }
  114. break
  115. case parser.NodeSUPPRESSES:
  116. suppresses, err = makeStringList(child)
  117. break
  118. case parser.NodeSTATEMENTS:
  119. statements = child
  120. break
  121. }
  122. if err != nil {
  123. break
  124. }
  125. }
  126. if err == nil && statements != nil {
  127. var desc string
  128. sinkName := fmt.Sprint(name)
  129. if len(rt.node.Meta) > 0 &&
  130. (rt.node.Meta[0].Type() == parser.MetaDataPreComment ||
  131. rt.node.Meta[0].Type() == parser.MetaDataPostComment) {
  132. desc = strings.TrimSpace(rt.node.Meta[0].Value())
  133. }
  134. rule := &engine.Rule{
  135. Name: sinkName, // Name
  136. Desc: desc, // Description
  137. KindMatch: kindMatch, // Kind match
  138. ScopeMatch: scopeMatch, // Match on event cascade scope
  139. StateMatch: stateMatch, // No state match
  140. Priority: priority, // Priority of the rule
  141. SuppressionList: suppresses, // List of suppressed rules by this rule
  142. Action: func(p engine.Processor, m engine.Monitor, e *engine.Event) error { // Action of the rule
  143. // Create a new root variable scope
  144. sinkVS := scope.NewScope(fmt.Sprintf("sink: %v", sinkName))
  145. // Create a new instance state with the monitor - everything called
  146. // by the rule will have access to the current monitor.
  147. sinkIs := map[string]interface{}{
  148. "monitor": m,
  149. }
  150. err = sinkVS.SetValue("event", map[interface{}]interface{}{
  151. "name": e.Name(),
  152. "kind": strings.Join(e.Kind(), engine.RuleKindSeparator),
  153. "state": e.State(),
  154. })
  155. if err == nil {
  156. scope.SetParentOfScope(sinkVS, vs)
  157. if _, err = statements.Runtime.Eval(sinkVS, sinkIs); err != nil {
  158. if sre, ok := err.(*util.RuntimeErrorWithDetail); ok {
  159. sre.Environment = sinkVS
  160. } else {
  161. // Provide additional information for unexpected errors
  162. err = &util.RuntimeErrorWithDetail{
  163. RuntimeError: err.(*util.RuntimeError),
  164. Environment: sinkVS,
  165. Data: nil,
  166. }
  167. }
  168. }
  169. }
  170. return err
  171. },
  172. }
  173. if err = rt.erp.Processor.AddRule(rule); err != nil {
  174. err = rt.erp.NewRuntimeError(util.ErrInvalidState, err.Error(), rt.node)
  175. }
  176. }
  177. }
  178. return nil, err
  179. }
  180. // Sink child nodes
  181. // ================
  182. /*
  183. sinkDetailRuntime is the runtime for sink detail declarations.
  184. */
  185. type sinkDetailRuntime struct {
  186. *baseRuntime
  187. valType string
  188. }
  189. /*
  190. Eval evaluate this runtime component.
  191. */
  192. func (rt *sinkDetailRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  193. var ret interface{}
  194. _, err := rt.baseRuntime.Eval(vs, is)
  195. if err == nil {
  196. if ret, err = rt.node.Children[0].Runtime.Eval(vs, is); err == nil {
  197. // Check value is of expected type
  198. if rt.valType == "list" {
  199. if _, ok := ret.([]interface{}); !ok {
  200. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  201. fmt.Sprintf("Expected a list as value"),
  202. rt.node)
  203. }
  204. } else if rt.valType == "map" {
  205. if _, ok := ret.(map[interface{}]interface{}); !ok {
  206. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  207. fmt.Sprintf("Expected a map as value"),
  208. rt.node)
  209. }
  210. } else if rt.valType == "int" {
  211. if _, ok := ret.(float64); !ok {
  212. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  213. fmt.Sprintf("Expected a number as value"),
  214. rt.node)
  215. }
  216. }
  217. }
  218. }
  219. return ret, err
  220. }
  221. /*
  222. kindMatchRuntimeInst returns a new runtime component instance.
  223. */
  224. func kindMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  225. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  226. }
  227. /*
  228. scopeMatchRuntimeInst returns a new runtime component instance.
  229. */
  230. func scopeMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  231. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  232. }
  233. /*
  234. stateMatchRuntimeInst returns a new runtime component instance.
  235. */
  236. func stateMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  237. return &sinkDetailRuntime{newBaseRuntime(erp, node), "map"}
  238. }
  239. /*
  240. priorityRuntimeInst returns a new runtime component instance.
  241. */
  242. func priorityRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  243. return &sinkDetailRuntime{newBaseRuntime(erp, node), "int"}
  244. }
  245. /*
  246. suppressesRuntimeInst returns a new runtime component instance.
  247. */
  248. func suppressesRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  249. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  250. }