blob: db4ae514e14c03267141973ce99dccf3f69b5ea3 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16// Package errorcodes provides constants that are commonly used by RWcore and adapters.
17package errorcodes
18
19import (
20 "net/http"
vinokuma926cb3e2023-03-29 11:41:06 +053021
Naveen Sampath04696f72022-06-13 15:19:14 +053022 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
24)
25
26// NBErrorCode represents the error code for the error.
27type NBErrorCode int
28
29const (
vinokuma926cb3e2023-03-29 11:41:06 +053030 // VolthaErrorMessageFormat represents the format in which the Voltha accepts the errors.
31 VolthaErrorMessageFormat = "code = %d, desc = %s"
Naveen Sampath04696f72022-06-13 15:19:14 +053032)
33
34// List of error messages returned to Voltha.
35var (
36 // ErrUnimplementedRPC is returned when the RPC is not implemented
37 ErrUnimplementedRPC = status.Errorf(codes.Unimplemented, VolthaErrorMessageFormat, UnsupportedOperation, "Operation not implemented")
38 // ErrOperationNotSupported is returned when the operation is not supported
39 ErrOperationNotSupported = status.Errorf(codes.Unimplemented, VolthaErrorMessageFormat, UnsupportedOperation, "Operation not supported")
40
41 // ErrFailedRequest is returned when the component fails to send any request to other component
42 ErrFailedRequest = status.Errorf(codes.Internal, VolthaErrorMessageFormat, UnsuccessfulOperation, "Failed to send request")
43
44 // ErrFailedToEncodeConfig is returned when the data json marshal fails
45 ErrFailedToEncodeConfig = status.Errorf(codes.Internal, VolthaErrorMessageFormat, MessageEncodeFailed, "Failed to encode data")
46 // ErrFailedToDecodeConfig is returned when the data json unmarshal fails
47 ErrFailedToDecodeConfig = status.Errorf(codes.Internal, VolthaErrorMessageFormat, MessageDecodeFailed, "Failed to decode data")
48
49 // ErrFailedToUpdateDB is returned when update of data in KV store fails
50 ErrFailedToUpdateDB = status.Errorf(codes.Internal, VolthaErrorMessageFormat, DBOperationFailed, "Failed to update DB")
51 // ErrFailedToGetFromDB is returned when get data from KV store fails
52 ErrFailedToGetFromDB = status.Errorf(codes.Internal, VolthaErrorMessageFormat, DBOperationFailed, "Failed to fetch from DB")
53 // ErrFailedToDeleteFromDB is returned when delete data from KV store fails
54 ErrFailedToDeleteFromDB = status.Errorf(codes.Internal, VolthaErrorMessageFormat, DBOperationFailed, "Failed to delete from DB")
55
56 // ErrDeviceNotFound is returned when the handler for the device is not present in VOLTHA
57 ErrDeviceNotFound = status.Errorf(codes.NotFound, VolthaErrorMessageFormat, ResourceNotFound, "Device not found")
58 // ErrDeviceNotReachable is returned when the connection between adapter and agent is broken
59 ErrDeviceNotReachable = status.Errorf(codes.Unavailable, VolthaErrorMessageFormat, DeviceUnreachable, "Device is not reachable")
60 // ErrWrongDevice is returned when the received request has wrong device (parent/child)
61 ErrWrongDevice = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, PrerequisiteNotMet, "Wrong device in request")
62 // ErrDeviceNotEnabledAndUp is returned when the state of the device is neither enabled nor active
63 ErrDeviceNotEnabledAndUp = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, ResourceInImproperState, "Device is not enabled and up")
64 // ErrDeviceDeleted is returned when the state of the device is DELETED
65 ErrDeviceDeleted = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, ResourceInImproperState, "Device is deleted")
66
67 // ErrPortNotFound is returned when the port is not present in VOLTHA
68 ErrPortNotFound = status.Errorf(codes.NotFound, VolthaErrorMessageFormat, ResourceNotFound, "Port not found")
69 // ErrPortIsInInvalidState is returned when the port is in an invalid state
70 ErrPortIsInInvalidState = status.Errorf(codes.Internal, VolthaErrorMessageFormat, ResourceInImproperState, "Port is in an invalid state")
71
72 // ErrInvalidParamInRequest is returned when the request contains invalid configuration
73 ErrInvalidParamInRequest = status.Errorf(codes.InvalidArgument, VolthaErrorMessageFormat, InvalidArgument, "Received invalid configuration in request")
74
75 // ErrImageNotRegistered is returned when the image is not registered
76 ErrImageNotRegistered = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, PrerequisiteNotMet, "Image is not registered")
77 // ErrImageDownloadInProgress is returned when the image download is in progress
78 ErrImageDownloadInProgress = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, MethodNotAllowed, "Image download is in progress")
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053079
80 // ErrServiceNotFound is returned when the Service is not present in VOLTHA
81 ErrServiceNotFound = status.Errorf(codes.NotFound, VolthaErrorMessageFormat, ResourceNotFound, "Service not found")
Naveen Sampath04696f72022-06-13 15:19:14 +053082)
83
84// ConvertToVolthaErrorFormat converts the error to Voltha error format
85func ConvertToVolthaErrorFormat(err error) error {
86 st, ok := status.FromError(err)
87 if !ok {
88 return err
89 }
90 return status.Errorf(st.Code(), VolthaErrorMessageFormat, GrpcToVolthaErrorCodeMap[st.Code()], st.Message())
91}
92
93const (
vinokuma926cb3e2023-03-29 11:41:06 +053094 //Success is returned when there is no error - 0
95 Success NBErrorCode = iota
96 //InvalidURL is returned when the URL specified for the request is invalid - 1
97 InvalidURL
98 //MissingArgument is returned when the mandatory/conditionally mandatory argument is missing - 2
99 MissingArgument
100 //RequestTimeout is returned when the request timed out. - 3
101 RequestTimeout
102 //ResourceAlreadyExists is returned when the resource already exists and create for the same is not allowed - 4
103 ResourceAlreadyExists
104 //ResourceInImproperState is returned when the resource is in improper state to process the request. - 5
105 ResourceInImproperState
106 //DeviceUnreachable is returned when the device is not reachable - 6
107 DeviceUnreachable
108 //OperationAlreadyInProgress is returned when the requested operation is already in progress - 7
109 OperationAlreadyInProgress
110 //InvalidConfig is returned when the configuration provided is invalid - 8
111 InvalidConfig
112 //ResourceNotFound is returned when the resource is not found - 9
113 ResourceNotFound
114 //MethodNotAllowed is returned when the requested method is not allowed - 10
115 MethodNotAllowed
116 //ResourceInUse is returned when the resource is in use, the delete of the resource is not allowed when in use - 11
117 ResourceInUse
118 //JobIDNotFound is returned when the Job ID not found - 12
119 JobIDNotFound
120 //JobIDAlreadyInUse is returned when the Job ID already in use - 13
121 JobIDAlreadyInUse
122 //PeerUnreachable is returned when the peer is unreachable -14
123 PeerUnreachable
124 //InvalidPatchOperation is returned when the parameter(s) mentioned in the patch operation are invalid - 15
125 InvalidPatchOperation
126 //OLTUnreachable is returned when the OLT is not reachable - 16
127 OLTUnreachable
128 //PrerequisiteNotMet is returned when the required prerequisite is not met to execute the requested procedure - 17
129 PrerequisiteNotMet
130 //MessageEncodeFailed is returned when Message encoding failed - 18
131 MessageEncodeFailed
132 //MessageDecodeFailed is returned when Message decoding failed - 19
133 MessageDecodeFailed
134 //ONTInternalError is returned when Internal error is reported by the ONT - 20
135 ONTInternalError
136 //OLTInternalError is returned when Internal error is reported by the OLT - 21
137 OLTInternalError
138 //VolthaInternalError is returned when Internal error occurred at Voltha - 22
139 VolthaInternalError
140 //ConfigMismatch is returned when the configuration does not match - 23
141 ConfigMismatch
142 //DBOperationFailed is returned when the database operation failed for the key - 24
143 DBOperationFailed
144 //ResourceLimitExceeded is returned when the resource limit exceeded the allowed limit - 25
145 ResourceLimitExceeded
146 //UndefinedEnv is returned when the required environment variable is not defined - 26
147 UndefinedEnv
148 //InvalidArgument is returned when the argument provided is invalid - 27
149 InvalidArgument
150 //InvalidPayload is returned when the configuration payload is invalid - 28
151 InvalidPayload
152 //DuplicateKey is returned when the duplicate entry for the key - 29
153 DuplicateKey
154 //DuplicateValue is returned when the duplicate entry for the value - 30
155 DuplicateValue
156 //UnsupportedOperation is returned when the request operation is not supported - 31
157 UnsupportedOperation
158 //UserUnauthorized is returned when the user is unauthorized to perform the requested operation - 32
159 UserUnauthorized
160 //LiveKPISubscriptionExists is returned when the live KPI subscription exists already for the requested resource - 33
161 LiveKPISubscriptionExists
162 //UnsuccessfulOperation is returned when the requested operation is unsuccessful - 34
163 UnsuccessfulOperation
164 //ResourceInDisabledStateAlready is returned when the resource is in disabled state already - 35
165 ResourceInDisabledStateAlready
166 //ResourceInEnabledStateAlready is returned when the resource is in enabled state already - 36
167 ResourceInEnabledStateAlready
168 //ResourceNotDiscoveredYet is returned when the resource is not discovered yet - 37
169 ResourceNotDiscoveredYet
170 //HighDiskUtilization is returned when the disk utilization is high, consider the disk cleanup. - 38
171 HighDiskUtilization
172 //KafkaError is returned when there is a kafka error - 39
173 KafkaError
174 //ResourceBusy is returned when the component/resource is busy. - 40
175 ResourceBusy
176 // UnsupportedParameter is returned when un supported field is provided in request. -41
177 UnsupportedParameter
178 //JobIDAlreadyExists is returned when the Job ID is already there in DB. -42
179 JobIDAlreadyExists
180 //LiveKPISubscriptionNotFound is returned when the live KPI subscription not found for the requested resource. -42
181 LiveKPISubscriptionNotFound
182 // HostUnreachable is returned when failed to establish the SFTP connection. -44
183 HostUnreachable
184 // DHCPServerUnreachable is returned when dhcp server is unreachable. -45
185 DHCPServerUnreachable
186 // SessionExpired is returned when user session is expired/timeout - 46
187 SessionExpired
188 // AccessDenied is returned when user operation is forbidden - 47
189 AccessDenied
190 // PasswordUpdateRequired is returned when password for the user is about to expire - 48
191 PasswordUpdateRequired
192 // InvalidMessageHeader is returned when token in security request is invalid/nil - 49
193 InvalidMessageHeader
194 // UserAccountBlocked is returned when user account gets blocked after multiple invalid attempts - 50
195 UserAccountBlocked
196 // UserAccountExpired is returned when user account gets expired - 51
197 UserAccountExpired
198 // UserAccountDormant is returned when user account gets dormant - 52
199 UserAccountDormant
200 // InvalidCredentials is returned when credentials are invalid in login request - 53
201 InvalidCredentials
202 // ConcurrentAccessFromMultipleIPs when multiple sessions gets established from same ip - 54
203 ConcurrentAccessFromMultipleIPs
204 // KPIThresholdCrossed when KPI threshold is crossed - 55
205 KPIThresholdCrossed
206 // ONTUnreachable is returned when the ONT is not reachable - 56
207 ONTUnreachable
208 // ResourceUnreachable is returned when the resource is not reachable -57
209 ResourceUnreachable
210 // ONTProcessingError is returned when onu returns processing error for omci message - 58
211 ONTProcessingError
212 // ONTResourceBusy is returned when onu returns device busy error for omci message - 59
213 ONTResourceBusy
214 // ONTMEInstanceExists is returned when onu returns OMCI ME instance exists error for omci message - 60
215 ONTMEInstanceExists
216 // ONTUnknownMEInstance is returned when onu returns OMCI ME Unknown Instance error for omci message - 61
217 ONTUnknownMEInstance
218 // JoinUnsuccessful is returned when an IGMP Join request is unsuccessful - 62
219 JoinUnsuccessful
220 // QueryExpired is returned when there is no response to IGMP Queries from the controller - 63
221 QueryExpired
222 // AvailableBwValidationErr is returned when requested bandwidth is not available on the pon port - 64
223 AvailableBwValidationErr
Naveen Sampath04696f72022-06-13 15:19:14 +0530224)
225
vinokuma926cb3e2023-03-29 11:41:06 +0530226// NBErrorCodeMap converts error code to error description string
Naveen Sampath04696f72022-06-13 15:19:14 +0530227var NBErrorCodeMap = map[NBErrorCode]string{
vinokuma926cb3e2023-03-29 11:41:06 +0530228 Success: "Success",
229 InvalidURL: "INVALID_URL",
230 RequestTimeout: "REQUEST_TIMEOUT",
231 MissingArgument: "MISSING_ARGUMENT",
232 ResourceAlreadyExists: "RESOURCE_ALREADY_EXISTS",
233 ResourceInImproperState: "RESOURCE_IN_IMPROPER_STATE",
234 DeviceUnreachable: "DEVICE_UNREACHABLE",
235 OperationAlreadyInProgress: "OPERATION_ALREADY_IN_PROGRESS",
236 InvalidConfig: "INVALID_CONFIG",
237 ResourceNotFound: "RESOURCE_NOT_FOUND",
238 MethodNotAllowed: "METHOD_NOT_ALLOWED",
239 ResourceInUse: "RESOURCE_IN_USE",
240 JobIDNotFound: "JOB_ID_NOT_FOUND",
241 JobIDAlreadyInUse: "JOB_ID_ALREADY_IN_USE",
242 PeerUnreachable: "PEER_UNREACHABLE",
243 InvalidPatchOperation: "INVALID_PATCH_OPERATION",
244 OLTUnreachable: "OLT_UNREACHABLE",
245 PrerequisiteNotMet: "PREREQUISITE_NOT_MET",
246 MessageEncodeFailed: "MESSAGE_ENCODE_FAILED",
247 MessageDecodeFailed: "MESSAGE_DECODE_FAILED",
248 ONTInternalError: "ONT_INTERNAL_ERROR",
249 OLTInternalError: "OLT_INTERNAL_ERROR",
250 VolthaInternalError: "Voltha_INTERNAL_ERROR",
251 ConfigMismatch: "CONFIG_MISMATCH",
252 DBOperationFailed: "DB_OPERATION_FAILED",
253 ResourceLimitExceeded: "RESOURCE_LIMIT_EXCEEDED",
254 UndefinedEnv: "UNDEFINED_ENV",
255 InvalidArgument: "INVALID_ARGUMENT",
256 InvalidPayload: "INVALID_PAYLOAD",
257 DuplicateKey: "DUPLICATE_KEY",
258 DuplicateValue: "DUPLICATE_VALUE",
259 UnsupportedOperation: "UNSUPPORTED_OPERATION",
260 UserUnauthorized: "USER_UNAUTHORIZED",
261 LiveKPISubscriptionExists: "LIVE_KPI_SUBSCRIPTION_EXISTS",
262 UnsuccessfulOperation: "UNSUCCESSFUL_OPERATION",
263 ResourceInDisabledStateAlready: "RESOURCE_IN_DISABLED_STATE_ALREADY",
264 ResourceInEnabledStateAlready: "RESOURCE_IN_ENABLED_STATE_ALREADY",
265 ResourceNotDiscoveredYet: "RESOURCE_NOT_DISCOVERED_YET",
266 HighDiskUtilization: "HIGH_DISK_UTILIZATION",
267 KafkaError: "KAFKA_ERROR",
268 LiveKPISubscriptionNotFound: "LIVE_KPI_SUBSCRIPTION_NOT_FOUND",
269 ResourceBusy: "RESOURCE_BUSY",
270 UnsupportedParameter: "UNSUPPORTED_PARAMETER",
271 JobIDAlreadyExists: "JOB_ID_ALREADY_EXISTS",
272 HostUnreachable: "HOST_UNREACHABLE",
273 DHCPServerUnreachable: "DHCP_SERVER_UNREACHABLE",
274 InvalidMessageHeader: "INVALID_MESSAGE_HEADER",
275 SessionExpired: "SESSION_EXPIRED",
276 AccessDenied: "ACCESS_DENIED",
277 PasswordUpdateRequired: "PASSWORD_UPDATE_REQUIRED",
278 InvalidCredentials: "INVALID_CREDENTIALS",
279 UserAccountBlocked: "USER_ACCOUNT_BLOCKED",
280 UserAccountExpired: "USER_ACCOUNT_EXPIRED",
281 ConcurrentAccessFromMultipleIPs: "CONCURRENT_ACCESS_FROM_MULTIPLE_IPS",
282 KPIThresholdCrossed: "KPI_THRESHOLD_CROSSED",
283 ONTUnreachable: "ONT_UNREACHABLE",
284 ONTProcessingError: "ONT_PROCESSING_ERROR",
285 ONTResourceBusy: "ONT_RESOURCE_BUSY",
286 ONTMEInstanceExists: "ONT_ME_INSTANCE_ALREADY_EXISTS",
287 ONTUnknownMEInstance: "ONT_UNKNOWN_ME_INSTANCE",
288 JoinUnsuccessful: "JOIN_UNSUCCESSFUL",
289 QueryExpired: "QUERY_EXPIRED",
Naveen Sampath04696f72022-06-13 15:19:14 +0530290}
291
292// GrpcToVolthaErrorCodeMap contains mapping of grpc error code coming from OpenOLT-Agent to Voltha error codes.
293var GrpcToVolthaErrorCodeMap = map[codes.Code]NBErrorCode{
vinokuma926cb3e2023-03-29 11:41:06 +0530294 codes.OK: Success,
295 codes.Canceled: UnsuccessfulOperation,
296 codes.Unknown: OLTInternalError,
297 codes.InvalidArgument: InvalidArgument,
298 codes.DeadlineExceeded: RequestTimeout,
299 codes.NotFound: ResourceNotFound,
300 codes.AlreadyExists: ResourceAlreadyExists,
301 codes.PermissionDenied: UserUnauthorized,
302 codes.ResourceExhausted: ResourceLimitExceeded,
303 codes.FailedPrecondition: PrerequisiteNotMet,
304 codes.Aborted: UnsuccessfulOperation,
305 codes.OutOfRange: InvalidArgument,
306 codes.Unimplemented: UnsupportedOperation,
307 codes.Internal: OLTInternalError,
308 codes.Unavailable: ResourceBusy,
309 codes.DataLoss: OLTInternalError,
310 codes.Unauthenticated: UserUnauthorized,
Naveen Sampath04696f72022-06-13 15:19:14 +0530311}
312
313// HTTPStatusCodeToVolthaErrorCodeMap contains mapping of http status code coming from VGC to Voltha error codes.
314var HTTPStatusCodeToVolthaErrorCodeMap = map[int]NBErrorCode{
vinokuma926cb3e2023-03-29 11:41:06 +0530315 http.StatusOK: Success,
316 http.StatusCreated: Success,
317 http.StatusAccepted: Success,
318 http.StatusBadRequest: InvalidPayload,
319 http.StatusConflict: ResourceInImproperState,
320 http.StatusInternalServerError: VolthaInternalError,
Naveen Sampath04696f72022-06-13 15:19:14 +0530321}
322
323// GetErrorInfo - parses the error details from err structure response from voltha
324// Return statusCode (uint32) - Error code [0 - Success]
vinokuma926cb3e2023-03-29 11:41:06 +0530325// status Msg (string) - Error Msg
Naveen Sampath04696f72022-06-13 15:19:14 +0530326func GetErrorInfo(err error) (uint32, string) {
vinokuma926cb3e2023-03-29 11:41:06 +0530327 var statusCode uint32
328 var statusMsg string
329 if status, _ := status.FromError(err); status != nil {
330 statusCode = uint32(status.Code())
331 statusMsg = status.Message()
332 } else {
333 statusCode = 0
334 }
335 return statusCode, statusMsg
Naveen Sampath04696f72022-06-13 15:19:14 +0530336}