forked from cgrates/cgrates
/
action.go
206 lines (177 loc) · 4.9 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sort"
)
/*
Structure to be filled for each tariff plan with the bonus value for received calls minutes.
*/
type Action struct {
Id string
ActionType string
BalanceId string
Direction string
ExtraParameters string
ExpirationString string
Weight float64
Balance *Balance
}
const (
LOG = "*log"
RESET_TRIGGERS = "*reset_triggers"
SET_POSTPAID = "*set_postpaid"
RESET_POSTPAID = "*reset_postpaid"
SET_PREPAID = "*set_prepaid"
RESET_PREPAID = "*reset_prepaid"
TOPUP_RESET = "*topup_reset"
TOPUP = "*topup"
DEBIT = "*debit"
RESET_COUNTER = "*reset_counter"
RESET_COUNTERS = "*reset_counters"
CALL_URL = "*call_url"
UNLIMITED = "*unlimited"
)
type actionTypeFunc func(*UserBalance, *Action) error
func getActionFunc(typ string) (actionTypeFunc, bool) {
switch typ {
case LOG:
return logAction, true
case RESET_TRIGGERS:
return resetTriggersAction, true
case SET_POSTPAID:
return setPostpaidAction, true
case RESET_POSTPAID:
return resetPostpaidAction, true
case SET_PREPAID:
return setPrepaidAction, true
case RESET_PREPAID:
return resetPrepaidAction, true
case TOPUP_RESET:
return topupResetAction, true
case TOPUP:
return topupAction, true
case DEBIT:
return debitAction, true
case RESET_COUNTER:
return resetCounterAction, true
case RESET_COUNTERS:
return resetCountersAction, true
case CALL_URL:
return callUrl, true
}
return nil, false
}
func logAction(ub *UserBalance, a *Action) (err error) {
ubMarshal,_ := json.Marshal(ub)
Logger.Info(fmt.Sprintf("Threshold reached, balance: %s", ubMarshal))
return
}
func resetTriggersAction(ub *UserBalance, a *Action) (err error) {
ub.resetActionTriggers(a)
return
}
func setPostpaidAction(ub *UserBalance, a *Action) (err error) {
ub.Type = UB_TYPE_POSTPAID
return
}
func resetPostpaidAction(ub *UserBalance, a *Action) (err error) {
genericReset(ub)
return setPostpaidAction(ub, a)
}
func setPrepaidAction(ub *UserBalance, a *Action) (err error) {
ub.Type = UB_TYPE_PREPAID
return
}
func resetPrepaidAction(ub *UserBalance, a *Action) (err error) {
genericReset(ub)
return setPrepaidAction(ub, a)
}
func topupResetAction(ub *UserBalance, a *Action) (err error) {
if ub.BalanceMap == nil { // Init the map since otherwise will get error if nil
ub.BalanceMap = make(map[string]BalanceChain, 0)
}
ub.BalanceMap[a.BalanceId+a.Direction] = BalanceChain{}
genericMakeNegative(a)
genericDebit(ub, a)
return
}
func topupAction(ub *UserBalance, a *Action) (err error) {
genericMakeNegative(a)
genericDebit(ub, a)
return
}
func debitAction(ub *UserBalance, a *Action) (err error) {
return genericDebit(ub, a)
}
func resetCounterAction(ub *UserBalance, a *Action) (err error) {
uc := ub.getUnitCounter(a)
if uc == nil {
uc = &UnitsCounter{BalanceId: a.BalanceId, Direction: a.Direction}
ub.UnitCounters = append(ub.UnitCounters, uc)
}
uc.initBalances(ub.ActionTriggers)
return
}
func resetCountersAction(ub *UserBalance, a *Action) (err error) {
ub.UnitCounters = make([]*UnitsCounter, 0)
ub.initCounters()
return
}
func genericMakeNegative(a *Action) {
if a.Balance != nil && a.Balance.Value > 0 { // only apply if not allready negative
a.Balance.Value = -a.Balance.Value
}
}
func genericDebit(ub *UserBalance, a *Action) (err error) {
if ub.BalanceMap == nil {
ub.BalanceMap = make(map[string]BalanceChain)
}
ub.debitBalanceAction(a)
return
}
func genericReset(ub *UserBalance) {
for k, _ := range ub.BalanceMap {
ub.BalanceMap[k] = BalanceChain{&Balance{Value: 0}}
}
ub.UnitCounters = make([]*UnitsCounter, 0)
ub.resetActionTriggers(nil)
}
func callUrl(ub *UserBalance, a *Action) error {
body, err := json.Marshal(ub)
if err != nil {
return err
}
_, err = http.Post(a.ExtraParameters, "application/json", bytes.NewBuffer(body))
return err
}
// Structure to store actions according to weight
type Actions []*Action
func (apl Actions) Len() int {
return len(apl)
}
func (apl Actions) Swap(i, j int) {
apl[i], apl[j] = apl[j], apl[i]
}
func (apl Actions) Less(i, j int) bool {
return apl[i].Weight < apl[j].Weight
}
func (apl Actions) Sort() {
sort.Sort(apl)
}