-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstatus.go
executable file
·252 lines (237 loc) · 9.18 KB
/
status.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* Copyright 2013-2016 Seagate Technology LLC.
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at
* https://mozilla.org/MP:/2.0/.
*
* This program is distributed in the hope that it will be useful,
* but is provided AS-IS, WITHOUT ANY WARRANTY; including without
* the implied warranty of MERCHANTABILITY, NON-INFRINGEMENT or
* FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public
* License for more details.
*
* See www.openkinetic.org for more project information
*/
package kinetic
import (
"strconv"
kproto "github.com/Kinetic/kinetic-go/proto"
)
// StatusCode for kinetic message.
// Including status code get from device, or client internal error code.
type StatusCode int32
// StatusCode code value
const (
RemoteNotAttempted StatusCode = iota
OK StatusCode = iota
ClientIOError StatusCode = iota
ClientShutdown StatusCode = iota
ClientInternalError StatusCode = iota
ClientResponseHMACError StatusCode = iota
RemoteHMACError StatusCode = iota
RemoteNotAuthorized StatusCode = iota
RemoteClusterVersionMismatch StatusCode = iota
RemoteInvalidRequest StatusCode = iota
RemoteInternalError StatusCode = iota
RemoteHeaderRequired StatusCode = iota
RemoteNotFound StatusCode = iota
RemoteVersionMismatch StatusCode = iota
RemoteServiceBusy StatusCode = iota
RemoteExpired StatusCode = iota
RemoteDataError StatusCode = iota
RemotePermDataError StatusCode = iota
RemoteConnectionError StatusCode = iota
RemoteNoSpace StatusCode = iota
RemoteNoSuchHMACAlgorithm StatusCode = iota
RemoteOtherError StatusCode = iota
ProtocolErrorResponseNoAckSequence StatusCode = iota
RemoteNestedOperationErrors StatusCode = iota
RemoteDeviceLocked StatusCode = iota
RemoteDeviceAlreadyUnlocked StatusCode = iota
RemoteConnectionTerminated StatusCode = iota
RemoteInvalidBatch StatusCode = iota
RemoteInvalidExecute StatusCode = iota
RemoteExecuteComplete StatusCode = iota
RemoteHibernate StatusCode = iota
RemoteShutdown StatusCode = iota
)
var statusName = map[StatusCode]string{
RemoteNotAttempted: "REMOTE_NOT_ATTEMPTED",
OK: "OK",
ClientIOError: "CLIENT_IO_ERROR",
ClientShutdown: "CLIENT_SHUTDOWN",
ClientInternalError: "CLIENT_INTERNAL_ERROR",
ClientResponseHMACError: "CLIENT_RESPONSE_HMAC_VERIFICATION_ERROR",
RemoteHMACError: "REMOTE_HMAC_ERROR",
RemoteNotAuthorized: "REMOTE_NOT_AUTHORIZED",
RemoteClusterVersionMismatch: "REMOTE_CLUSTER_VERSION_MISMATCH",
RemoteInvalidRequest: "REMOTE_INVALID_REQUEST",
RemoteInternalError: "REMOTE_INTERNAL_ERROR",
RemoteHeaderRequired: "REMOTE_HEADER_REQUIRED",
RemoteNotFound: "REMOTE_NOT_FOUND",
RemoteVersionMismatch: "REMOTE_VERSION_MISMATCH",
RemoteServiceBusy: "REMOTE_SERVICE_BUSY",
RemoteExpired: "REMOTE_EXPIRED",
RemoteDataError: "REMOTE_DATA_ERROR",
RemotePermDataError: "REMOTE_PERM_DATA_ERROR",
RemoteConnectionError: "REMOTE_CONNECTION_ERROR",
RemoteNoSpace: "REMOTE_NO_SPACE",
RemoteNoSuchHMACAlgorithm: "REMOTE_NO_SUCH_HMAC_ALGORITHM",
RemoteOtherError: "REMOTE_OTHER_ERROR",
ProtocolErrorResponseNoAckSequence: "PROTOCOL_ERROR_RESPONSE_NO_ACKSEQUENCE ",
RemoteNestedOperationErrors: "REMOTE_NESTED_OPERATION_ERRORS",
RemoteDeviceLocked: "REMOTE_DEVICE_LOCKED",
RemoteDeviceAlreadyUnlocked: "REMOTE_DEVICE_ALREADY_UNLOCKED",
RemoteConnectionTerminated: "REMOTE_CONNECTION_TERMINATED",
RemoteInvalidBatch: "REMOTE_INVALID_BATCH",
RemoteInvalidExecute: "REMOTE_INVALID_EXECUTE",
RemoteExecuteComplete: "REMOTE_EXECUTE_COMPLETE",
RemoteHibernate: "REMOTE_HIBERNATE",
RemoteShutdown: "REMOTE_SHUTDOWN",
}
// String returns string value of StatusCode.
func (c StatusCode) String() string {
str, ok := statusName[c]
if ok {
return str
}
return "Unknown Status"
}
// Status for each kinetic message.
// Code is the status code and ErrorMsg is the detail message
type Status struct {
Code StatusCode
ErrorMsg string
ExpectedClusterVersion int64
}
// Error returns the detail status message if Status.Code != OK
func (s Status) Error() string {
return s.ErrorMsg
}
func (s Status) String() string {
ret := "Unknown Status"
str, ok := statusName[s.Code]
if ok {
ret = str + " : " + s.ErrorMsg
if s.Code == RemoteClusterVersionMismatch {
ret = ret + ", Expected cluster version =" + strconv.Itoa(int(s.ExpectedClusterVersion))
}
}
return ret
}
func convertStatusCodeToProto(s StatusCode) kproto.Command_Status_StatusCode {
ret := kproto.Command_Status_INVALID_STATUS_CODE
switch s {
case RemoteNotAttempted:
ret = kproto.Command_Status_NOT_ATTEMPTED
case OK:
ret = kproto.Command_Status_SUCCESS
case RemoteHMACError:
ret = kproto.Command_Status_HMAC_FAILURE
case RemoteNotAuthorized:
ret = kproto.Command_Status_NOT_AUTHORIZED
case RemoteClusterVersionMismatch:
ret = kproto.Command_Status_VERSION_FAILURE
case RemoteInternalError:
ret = kproto.Command_Status_INTERNAL_ERROR
case RemoteHeaderRequired:
ret = kproto.Command_Status_HEADER_REQUIRED
case RemoteNotFound:
ret = kproto.Command_Status_NOT_FOUND
case RemoteVersionMismatch:
ret = kproto.Command_Status_VERSION_MISMATCH
case RemoteServiceBusy:
ret = kproto.Command_Status_SERVICE_BUSY
case RemoteExpired:
ret = kproto.Command_Status_EXPIRED
case RemoteDataError:
ret = kproto.Command_Status_DATA_ERROR
case RemotePermDataError:
ret = kproto.Command_Status_PERM_DATA_ERROR
case RemoteConnectionError:
ret = kproto.Command_Status_REMOTE_CONNECTION_ERROR
case RemoteNoSpace:
ret = kproto.Command_Status_NO_SPACE
case RemoteNoSuchHMACAlgorithm:
ret = kproto.Command_Status_NO_SUCH_HMAC_ALGORITHM
case RemoteInvalidRequest:
ret = kproto.Command_Status_INVALID_REQUEST
case RemoteNestedOperationErrors:
ret = kproto.Command_Status_NESTED_OPERATION_ERRORS
case RemoteDeviceLocked:
ret = kproto.Command_Status_DEVICE_LOCKED
case RemoteDeviceAlreadyUnlocked:
ret = kproto.Command_Status_DEVICE_ALREADY_UNLOCKED
case RemoteConnectionTerminated:
ret = kproto.Command_Status_CONNECTION_TERMINATED
case RemoteInvalidBatch:
ret = kproto.Command_Status_INVALID_BATCH
case RemoteHibernate:
ret = kproto.Command_Status_HIBERNATE
case RemoteShutdown:
ret = kproto.Command_Status_SHUTDOWN
}
return ret
}
func convertStatusCodeFromProto(s kproto.Command_Status_StatusCode) StatusCode {
ret := RemoteOtherError
switch s {
case kproto.Command_Status_NOT_ATTEMPTED:
ret = RemoteNotAttempted
case kproto.Command_Status_SUCCESS:
ret = OK
case kproto.Command_Status_HMAC_FAILURE:
ret = RemoteHMACError
case kproto.Command_Status_NOT_AUTHORIZED:
ret = RemoteNotAuthorized
case kproto.Command_Status_VERSION_FAILURE:
ret = RemoteClusterVersionMismatch
case kproto.Command_Status_INTERNAL_ERROR:
ret = RemoteInternalError
case kproto.Command_Status_HEADER_REQUIRED:
ret = RemoteHeaderRequired
case kproto.Command_Status_NOT_FOUND:
ret = RemoteNotFound
case kproto.Command_Status_VERSION_MISMATCH:
ret = RemoteVersionMismatch
case kproto.Command_Status_SERVICE_BUSY:
ret = RemoteServiceBusy
case kproto.Command_Status_EXPIRED:
ret = RemoteExpired
case kproto.Command_Status_DATA_ERROR:
ret = RemoteDataError
case kproto.Command_Status_PERM_DATA_ERROR:
ret = RemotePermDataError
case kproto.Command_Status_REMOTE_CONNECTION_ERROR:
ret = RemoteConnectionError
case kproto.Command_Status_NO_SPACE:
ret = RemoteNoSpace
case kproto.Command_Status_NO_SUCH_HMAC_ALGORITHM:
ret = RemoteNoSuchHMACAlgorithm
case kproto.Command_Status_INVALID_REQUEST:
ret = RemoteInvalidRequest
case kproto.Command_Status_NESTED_OPERATION_ERRORS:
ret = RemoteNestedOperationErrors
case kproto.Command_Status_DEVICE_LOCKED:
ret = RemoteDeviceLocked
case kproto.Command_Status_DEVICE_ALREADY_UNLOCKED:
ret = RemoteDeviceAlreadyUnlocked
case kproto.Command_Status_CONNECTION_TERMINATED:
ret = RemoteConnectionTerminated
case kproto.Command_Status_INVALID_BATCH:
ret = RemoteInvalidBatch
case kproto.Command_Status_HIBERNATE:
ret = RemoteHibernate
case kproto.Command_Status_SHUTDOWN:
ret = RemoteShutdown
}
return ret
}
func getStatusFromProto(cmd *kproto.Command) Status {
code := convertStatusCodeFromProto(cmd.GetStatus().GetCode())
msg := cmd.GetStatus().GetStatusMessage()
version := cmd.GetHeader().GetClusterVersion()
return Status{code, msg, version}
}