Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7a9af44 | 2024-01-03 19:26:36 -0500 | [diff] [blame] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 22 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v5/go/common" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 26 | ) |
| 27 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | func (agent *Agent) logDeviceUpdate(ctx context.Context, prevState, currState *common.AdminState_Types, status *common.OperationResp, err error, desc string) { |
| 29 | requestedBy := utils.GetEndpointMetadataFromContext(ctx) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 30 | |
| 31 | if requestedBy == "" { |
| 32 | requestedBy = "NB" |
| 33 | } |
| 34 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 35 | rpc := utils.GetRPCMetadataFromContext(ctx) |
| 36 | |
| 37 | fields := log.Fields{"rpc": rpc, "device-id": agent.deviceID, |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 38 | "requested-by": requestedBy, "state-change": agent.stateChangeString(prevState, currState), |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 39 | "status": status.GetCode().String(), "description": desc, "error": err} |
| 40 | |
| 41 | if err != nil { |
| 42 | logger.Errorw(ctx, "logDeviceUpdate-failed", fields) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | logger.Infow(ctx, "logDeviceUpdate-success", fields) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func (agent *Agent) stateChangeString(prevState *common.AdminState_Types, currState *common.AdminState_Types) string { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 50 | if prevState != nil && currState != nil && *prevState != *currState { |
khenaidoo | dd3324d | 2021-04-27 16:22:55 -0400 | [diff] [blame] | 51 | return fmt.Sprintf("%s->%s", *prevState, *currState) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 52 | } |
| 53 | return "" |
| 54 | } |