-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonus.go
220 lines (200 loc) · 6.04 KB
/
sonus.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package sonus
import (
"context"
"crypto/tls"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"reflect"
"time"
log "github.com/ringsq/go-logger"
)
// Sonus URLs
const (
// Gets the SBC system information
systemInfoPath = "/sonusSystem:system/admin"
serverInfoPath = "/sonusSystem:system/serverStatus"
contextListPath = "/sonusAddressContext:addressContext"
zoneStatusPath = "/sonusAddressContext:addressContext=%s/sonusZone:zone"
ipInterfaceGroupPath = "/operational/addressContext/%s/ipInterfaceGroup/"
sipStatsPath = "/operational/addressContext/%s/zone/%s/sipCurrentStatistics/"
fanStatusPath = "/sonusSystem:system/fanStatus/"
powerSupplyPath = "/sonusSystem:system/powerSupplyStatus/"
dspStatusPath = "/sonusSystem:system/sonusDrmDspStatus:dspStatus" // possibly with /dspUsage appended
tgStatusPath = "/operational/global/globalTrunkGroupStatus/"
tgConfigPath = "/config/addressContext/%s/zone/%s/sipTrunkGroup/"
callStatusPath = "/operational/addressContext/%s/zone/%s/callCurrentStatistics/"
)
type system struct {
Admin admin `json:"admin" xml:"admin"`
ServerAdmin []admin `json:"serverAdmin" xml:"serverAdmin"`
}
type admin struct {
Name string `json:"name" xml:"name"`
ActualSystemName string `json:"actualSystemName" xml:"actualSystemName"`
}
type AddressContexts struct {
AddressContext []AddressContext `xml:"addressContext"`
}
type AddressContext struct {
Name string `xml:"name"`
DnsGroup struct {
Name string `xml:"name"`
} `xml:"dnsGroup"`
IpInterfaceGroup []struct {
Name string `xml:"name"`
} `xml:"ipInterfaceGroup"`
Zone []struct {
Name string `xml:"name"`
} `xml:"zone"`
}
// SBC represents a single Sonus session border controller
type SBC struct {
target string
user string
password string
client *http.Client
System string
AddressContexts *AddressContexts
}
// NewSBC instantiates an SBC from the provided credentials
func NewSBC(address, user, password string) *SBC {
ac := &AddressContexts{}
sbc := &SBC{
target: address,
user: user,
password: password,
AddressContexts: ac,
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
sbc.client = &http.Client{Transport: tr}
sys := &system{}
ctx := context.Background()
err := sbc.GetAndParse(ctx, sys, systemInfoPath)
if err != nil {
log.Errorf("Error calling SBC (%s): %v", systemInfoPath, err)
return nil
}
sbc.System = sys.Admin.Name
defaultContext := AddressContext{
Name: "default",
}
// Get the address contexts
// err = sbc.GetAndParse(ctx, ac, contextListPath)
// if err != nil {
// log.Errorf("Error getting context list: %v", err)
// return nil
// }
ac.AddressContext = append(ac.AddressContext, defaultContext)
sbc.AddressContexts = ac
return sbc
}
// buildURL takes the given path, adds the base to the beginning, and applies any
// formatting arguments
func (s *SBC) buildURL(path string, args ...any) string {
url := fmt.Sprintf("https://%s/restconf/data%s", s.target, path)
return fmt.Sprintf(url, args...)
}
// GetAndParse builds the URL, does a GET against the SBC, and parses the XML response.
// Any errors are returned in error.
func (s *SBC) GetAndParse(ctx context.Context, response any, path string, args ...any) error {
url := s.buildURL(path, args...)
resp, err := s.callSBC(ctx, http.MethodGet, url, nil)
if err != nil {
log.Errorf("Error calling SBC (%s): %v", url, err)
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Errorf("Error reading response body: %v", err)
return err
}
err = xml.Unmarshal(body, response)
if err != nil {
log.Errorf("BODY: %v", body)
log.Errorf("Failed to deserialize %s into %v: %v", url, reflect.TypeOf(response), err)
return err
}
return nil
}
// callSBC is responsible for building the request object, sending it to the SBC, and checking the response status.
func (s *SBC) callSBC(ctx context.Context, method string, url string, body io.Reader) (*http.Response, error) {
// startTime := time.Now()
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
log.Errorf("Error creating request to %s %s: %v", method, url, err)
return nil, err
}
req.SetBasicAuth(s.user, s.password)
req.Header.Add("Accept", "application/vnd.yang.collection+xml")
var resp *http.Response
for retries := 3; retries > 0; retries-- {
resp, err = s.client.Do(req)
if err != nil {
log.Errorf("Error with SBC call %s %s: %v", method, url, err)
return nil, err
}
if prob := checkResponse(resp); prob != nil {
log.Errorf("Error response from %s %s: %v", method, url, prob)
return nil, prob
}
if resp.StatusCode == 204 {
sleepTime := rand.Intn(1000)
log.Warnf("%s received for %s, retry %d in %dms...", resp.Status, url, 3-retries+1, sleepTime)
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
continue
}
break
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Invalid response received: %s", resp.Status)
}
return resp, nil
}
// checkResponse inspects the response status codes and creates an appropriate problem. If
// no issues are found `nil` is returned
func checkResponse(resp *http.Response) error {
status := resp.StatusCode
if status < 300 {
return nil
}
if status < 400 {
return fmt.Errorf("Redirect received from Sonus: %v", resp.Status)
}
if status < 500 {
prob := errors.New(getError(resp))
log.Warn(prob)
return prob
}
if status < 600 {
prob := errors.New(getError(resp))
log.Error(prob)
return prob
}
return nil
}
// getError reads the error message from the Sonus response
func getError(resp *http.Response) string {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp.Status
}
if len(body) == 0 {
return resp.Status
}
sbcErr := Errors{}
err = xml.Unmarshal(body, &sbcErr)
if err != nil {
return resp.Status
}
return sbcErr.Error()
}