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