-
Notifications
You must be signed in to change notification settings - Fork 0
/
axl-user-device-line.go
151 lines (136 loc) · 4.94 KB
/
axl-user-device-line.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
package main
import (
"encoding/xml"
"fmt"
log "github.com/sirupsen/logrus"
"strings"
)
type UserDeviceLineList struct {
XMLName xml.Name `xml:"return"`
Rows []UserDeviceLine `xml:"row"`
}
type UserDeviceLine struct {
XMLName xml.Name `xml:"row"`
UserPKID string `xml:"user_pkid" json:"user_pkid"`
DevicePKID string `xml:"device_pkid" json:"device_pkid"`
LinePKID string `xml:"line_pkid" json:"line_pkid"`
FirstName string `xml:"firstname" json:"firstname"`
MiddleName string `xml:"middlename" json:"middlename"`
LastName string `xml:"lastname" json:"lastname"`
UserId string `xml:"userid" json:"userid"`
Department string `xml:"department" json:"department"`
Status int `xml:"status" json:"status"`
IsLocalUser bool `xml:"islocaluser" json:"islocaluser"`
Uccx bool `xml:"uccx" json:"uccx"`
DirectoryUri string `xml:"directoryuri" json:"directoryuri"`
MailId string `xml:"mailid" json:"mailid"`
DeviceName string `xml:"devicename" json:"devicename"`
DeviceDescription string `xml:"devicedescrition" json:"devicedescrition"`
LineNumber string `xml:"dnorpattern" json:"dnorpattern"`
LineAlertingName string `xml:"alertingnameascii" json:"alertingnameascii"`
LineDescription string `xml:"line_description" json:"line_description"`
ClusterName string `xml:"cluster_name" json:"cluster_name"`
}
func NewUserDeviceLineList(response string) (*UserDeviceLineList, error) {
var data UserDeviceLineList
d := []byte(response)
err := xml.Unmarshal(d, &data)
if err != nil {
log.WithField("error", err).Errorf("problem unmarshal data from response")
data = UserDeviceLineList{Rows: []UserDeviceLine{}}
} else {
log.WithField("data", response).Tracef("use this list of data")
if !config.Processing.CoexistCcxImporter {
for i, _ := range data.Rows {
data.Rows[i].Uccx = false
}
log.Tracef("remove UCCX Integration value")
}
}
return &data, err
}
func (u *UserDeviceLineList) GetDuplicateDevices() *Duplicates {
data := Duplicates{
device: make(map[string]*UniqueList),
line: make(map[string]*UniqueList),
user: make(map[string]*UniqueList),
errors: []string{},
}
for _, r := range u.Rows {
if _, ok := data.device[r.DevicePKID]; ok {
data.device[r.DevicePKID].Add(r.UserPKID)
} else {
data.device[r.DevicePKID] = NewUniqueList(r.DeviceName, r.DeviceDescription, r.UserPKID)
}
if _, ok := data.line[r.LinePKID]; ok {
data.line[r.LinePKID].Add(r.UserPKID)
} else {
data.line[r.LinePKID] = NewUniqueList(r.LineNumber, r.LineAlertingName, r.UserPKID)
}
if _, ok := data.user[r.UserPKID]; ok {
} else {
data.user[r.UserPKID] = NewUniqueList(r.UserId, fmt.Sprintf("%s %s", r.FirstName, r.LastName), "x")
}
}
data.GenerateErrors()
return &data
}
func (u *UserDeviceLineList) cleanDeviceLineList() []UserDeviceLine {
log.WithField("rows", len(u.Rows)).Debugf("from AXL select %d rows combination user/device/line", len(u.Rows))
duplicates := u.GetDuplicateDevices()
if len(duplicates.errors) > 0 {
log.Error("all duplicate association remove from source data")
for _, d := range duplicates.errors {
log.Error(d)
}
}
ret := u.removeDuplicates(duplicates)
return ret
}
func (u *UserDeviceLine) inDuplicates(dup *Duplicates) bool {
if _, ok := dup.device[u.DevicePKID]; ok {
return true
}
if _, ok := dup.line[u.LinePKID]; ok {
return true
} else {
return false
}
}
func (u *UserDeviceLineList) removeDuplicates(dup *Duplicates) []UserDeviceLine {
var response []UserDeviceLine
for idx := 0; idx < len(u.Rows); idx++ {
if !u.Rows[idx].inDuplicates(dup) {
response = append(response, u.Rows[idx])
}
}
log.WithField("removedRows", len(u.Rows)-len(response)).Infof("From source AXL table remove %d rows", len(u.Rows)-len(response))
return response
}
func (s *Connection) GetUserDeviceLineList() *UserDeviceLineList {
log.WithField("id", s.id).Trace("get table with user/device/line details from AXL")
var jtapi []string
users := strings.Join(config.Zqm.JtapiUser, "','")
jtapi = append(jtapi, strings.ToLower(users))
sql := NewUserDeviceLineSql(jtapi)
if !sql.IsParametersValid() {
log.WithField("id", s.id).Errorf("Not valid request parameters")
return nil
}
request := NewRequest(s.client, s)
log.WithFields(log.Fields{"id": s.id, "sql": sql.ToString()}).Debugf("Request for %s", strings.Join(config.Zqm.JtapiUser, ","))
response := request.SqlRequest(sql.ToString())
msg, err := response.ResponseError()
if err != nil {
response.Close()
log.WithField("id", s.id).Errorf("%s. HTTP Status [%s]", msg, response.statusMessage)
return nil
}
data, err := NewUserDeviceLineList(response.GetResponseBody())
if err != nil {
log.WithField("id", s.id).Errorf("Can't convert returned data to Version structure")
return nil
}
log.WithField("id", s.id).Trace("Success read DB version from AXL")
return data
}