-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_library.go
176 lines (167 loc) · 4.7 KB
/
response_library.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
package odl
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
// VtnList ... data structure for holding vtn list from odl
type VtnList struct {
Vtns struct {
Vtn []struct {
Name string `json:"name"`
Vbridge []struct {
BridgeStatus struct {
PathFaults int `json:"path-faults"`
State string `json:"state"`
} `json:"bridge-status"`
Name string `json:"name"`
VbridgeConfig struct {
AgeInterval int `json:"age-interval"`
} `json:"vbridge-config"`
VInterface []struct {
Name string `json:"name"`
VInterfaceConfig struct {
Description string `json:"description"`
Enabled bool `json:"enabled"`
} `json:"vinterface-config"`
VInterfaceStatus struct {
EntityState string `json:"entity-state"`
State string `json:"state"`
} `json:"vinterface-status"`
} `json:"vinterface"`
} `json:"vbridge"`
VtenantConfig struct {
HardTimeout int `json:"hard-timeout"`
IdleTimeout int `json:"idle-timeout"`
} `json:"vtenant-config"`
} `json:"vtn"`
} `json:"vtns"`
}
// ErrorCase ... data structure of error case from odl
type ErrorCase struct {
Errors struct {
Error []struct {
AppTag string `json:"error-app-tag"`
Info string `json:"error-info"`
Message string `json:"error-message"`
Tag string `json:"error-tag"`
Type string `json:"error-type"`
} `json:"error"`
} `json:"errors"`
}
// OutputCase ... data structure of output case from odl
type OutputCase struct {
Output struct {
Status string `json:"status"`
} `json:"output"`
}
// Status ... Parses output of odl
func Status(response *http.Response) (bool, *OutputCase, *ErrorCase, error) {
respString, err := getResponseAsString(response)
if err != nil {
return false, nil, nil, err
}
if strings.Contains(respString, "output") {
data := &OutputCase{}
err := json.Unmarshal([]byte(respString), data)
if err != nil {
return false, nil, nil, err
}
return true, data, nil, nil
}
if strings.Contains(respString, "error-message") {
data := &ErrorCase{}
err := json.Unmarshal([]byte(respString), data)
if err != nil {
return false, nil, nil, err
}
return false, nil, data, nil
}
return false, nil, nil, nil
}
//CheckResponseVirtualTenantNetworkExists ... checks response if vtn exists
func CheckResponseVirtualTenantNetworkExists(response *http.Response, name string) (bool, error) {
respString, err := getResponseAsString(response)
data := &VtnList{}
err = json.Unmarshal([]byte(respString), data)
if err != nil {
return false, err
}
if data.Vtns.Vtn != nil {
for i := range data.Vtns.Vtn {
if data.Vtns.Vtn[i].Name == name {
return true, nil
}
}
}
return false, nil
}
//CheckResponseVirtualBridgeExists ... checks response if vtn exists
func CheckResponseVirtualBridgeExists(response *http.Response, tenantName, bridgeName string) (bool, error) {
respString, err := getResponseAsString(response)
data := &VtnList{}
err = json.Unmarshal([]byte(respString), data)
if err != nil {
return false, err
}
if data.Vtns.Vtn != nil {
for i := range data.Vtns.Vtn {
if data.Vtns.Vtn[i].Name == tenantName {
if data.Vtns.Vtn[i].Vbridge != nil {
for j := range data.Vtns.Vtn[i].Vbridge {
if data.Vtns.Vtn[i].Vbridge[j].Name == bridgeName {
return true, nil
}
}
}
return false, nil
}
}
}
return false, nil
}
//CheckResponseVirtualInterfaceExists ... checks response if vInterface exists
func CheckResponseVirtualInterfaceExists(response *http.Response, tenantName, bridgeName, interfaceName string) (bool, error) {
respString, err := getResponseAsString(response)
data := &VtnList{}
err = json.Unmarshal([]byte(respString), data)
if err != nil {
return false, err
}
if data.Vtns.Vtn != nil {
for i := range data.Vtns.Vtn {
if data.Vtns.Vtn[i].Name == tenantName {
if data.Vtns.Vtn[i].Vbridge != nil {
for j := range data.Vtns.Vtn[i].Vbridge {
if data.Vtns.Vtn[i].Vbridge[j].Name == bridgeName {
if data.Vtns.Vtn[i].Vbridge[j].VInterface != nil {
for k := range data.Vtns.Vtn[i].Vbridge[j].VInterface {
if data.Vtns.Vtn[i].Vbridge[j].VInterface[k].Name == interfaceName {
return true, nil
}
}
}
return false, nil
}
}
}
return false, nil
}
}
}
return false, nil
}
func getResponseAsString(response *http.Response) (string, error) {
if response.Status != "200 OK" {
return "", fmt.Errorf("[ERROR] %s ", response.Status)
}
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("[ERROR] Reading body %s", err.Error())
return "", fmt.Errorf("[ERROR] Reading body %s", err.Error())
}
return string(buf), nil
}