Statistics
| Branch: | Tag: | Revision:

one / src / flow / lib / grammar.treetop @ 56fbcea8

History | View | Annotate | Download (6.58 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2010-2015, C12G Labs S.L.                                        #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# 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
grammar ElasticityGrammar
18

    
19
    rule expression
20
        space exp:(boolean_exp / logic_cond) space {
21
            def result(role)
22
                return exp.result(role)
23
            end
24
        }
25
    end
26

    
27
    rule boolean_exp
28
        left:logic_cond space op:('&&' / '&') space right:expression {
29
            def result(role)
30
                l_val, l_st = left.result(role)
31
                r_val, r_st = right.result(role)
32

    
33
                st = "#{l_st} #{op.text_value} #{r_st}"
34
                val = l_val && r_val
35

    
36
                return [val, st]
37
            end
38
        }
39
        /
40
        left:logic_cond space op:('||' / '|') space right:expression {
41
            def result(role)
42
                l_val, l_st = left.result(role)
43
                r_val, r_st = right.result(role)
44

    
45
                st = "#{l_st} #{op.text_value} #{r_st}"
46
                val = l_val || r_val
47

    
48
                return [val, st]
49
            end
50
        }
51
    end
52

    
53
    rule logic_cond
54
#        'true' {
55
#            def result(role)
56
#                return true
57
#            end
58
#        }
59
#        /
60
#        'false' {
61
#            def result(role)
62
#                return false
63
#            end
64
#        }
65
#        /
66
        left:operand space comp_op space right:operand {
67
            def result(role)
68
                l_val, l_st = left.result(role)
69
                r_val, r_st = right.result(role)
70

    
71
                st = "#{l_st} #{comp_op.text_value} #{r_st}"
72

    
73
                if l_val.nil? || r_val.nil?
74
                    # An attribute was not found, we return false instead
75
                    # of assuming a value of 0
76

    
77
                    val = false
78
                else
79
                    val = comp_op.apply(l_val, r_val)
80
                end
81

    
82
                return [val, st]
83
            end
84
        }
85
        /
86
        '!' space expression {
87
            def result(role)
88
                e_val, e_st = expression.result(role)
89

    
90
                val = !e_val
91
                st = "!#{e_st}"
92

    
93
                return [val, st]
94
            end
95
        }
96
        /
97
        '(' space expression space ')' {
98
            def result(role)
99
                e_val, e_st = expression.result(role)
100

    
101
                st = "(#{e_st})"
102

    
103
                return [e_val, st]
104
            end
105
        }
106
    end
107

    
108
    rule comp_op
109
        ('==' / '=') {
110
            def apply(a,b)
111
                a == b
112
            end
113
        }
114
        /
115
        ('!=' / '<>') {
116
            def apply(a,b)
117
                a != b
118
            end
119
        }
120
        /
121
        '>=' {
122
            def apply(a,b)
123
                a >= b
124
            end
125
        }
126
        /
127
        '>' {
128
            def apply(a,b)
129
                a > b
130
            end
131
        }
132
        /
133
        '<=' {
134
            def apply(a,b)
135
                a <= b
136
            end
137
        }
138
        /
139
        '<' {
140
            def apply(a,b)
141
                a < b
142
            end
143
        }
144
    end
145

    
146
    rule operand
147
        ( number ) {
148
            def result
149
                number.result(role)
150
            end
151
        }
152
        /
153
        ( variable ) {
154
            def result
155
                variable.result(role)
156
            end
157
        }
158
    end
159

    
160
    rule number
161
        '-'? [0-9]+ '.' [0-9]+ {
162
            def result(role)
163
                val = text_value.to_f
164
                st = val.to_s
165

    
166
                return [val, st]
167
            end
168
        }
169
        /
170
        '-'? [0-9]+ {
171
            def result(role)
172
                val = text_value.to_i
173
                st = val.to_s
174

    
175
                return [val, st]
176
            end
177
        }
178
    end
179

    
180
    rule variable
181
        ( '"' [a-zA-Z] [0-9a-zA-Z_]* '"'
182
        / '\'' [a-zA-Z] [0-9a-zA-Z_]* '\''
183
        / [a-zA-Z] [0-9a-zA-Z_]*
184
        ) {
185

    
186
            def result(role)
187
                nodes = role.get_nodes
188
                total = 0
189
                n_nodes = 0
190
                att = text_value.upcase
191

    
192
                nodes.each { |node|
193
                    if node && node['vm_info']
194

    
195
                        vm_state = node['vm_info']['VM']['STATE']
196
                        lcm_state = node['vm_info']['VM']['LCM_STATE']
197

    
198
                        # Use values from VMs in RUNNING only
199

    
200
                        if vm_state != '3' || lcm_state != '3'
201
                            next
202
                        end
203

    
204
                        if node['vm_info']['VM']['USER_TEMPLATE'][att]
205
                            total += (node['vm_info']['VM']['USER_TEMPLATE'][att]).to_f
206
                            n_nodes += 1
207
                        elsif node['vm_info']['VM']['MONITORING'][att]
208
                            total += (node['vm_info']['VM']['MONITORING'][att]).to_f
209
                            n_nodes += 1
210
                        elsif node['vm_info']['VM']['TEMPLATE'][att]
211
                            total += (node['vm_info']['VM']['TEMPLATE'][att]).to_f
212
                            n_nodes += 1
213
                        elsif node['vm_info']['VM'][att]
214
                            total += (node['vm_info']['VM'][att]).to_f
215
                            n_nodes += 1
216
                        end
217
                    end
218
                }
219

    
220
                # The attribute wasn't found for any of the nodes
221
                if n_nodes == 0
222
                    val = nil
223
                    st = "#{att}[--]"
224
                else
225
                    val = ((total / n_nodes)*100).round/100.0
226
                    st = "#{att}[#{val.to_s}]"
227
                end
228

    
229
                return [val, st]
230
            end
231
        }
232
    end
233

    
234
    rule space
235
      ' '*
236
    end
237
end