rt_sink.go 7.4 KB

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