blob: 599c2cffb602981f35b56a2e3db90178cb3045c3 [file] [log] [blame]
Maninder9a1bc0d2020-10-26 11:34:02 +05301/*
2 * Copyright 2021-present Open Networking Foundation
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
17package device
18
19import (
20 "context"
21 "fmt"
khenaidood948f772021-08-11 17:49:24 -040022
Maninder9a1bc0d2020-10-26 11:34:02 +053023 "github.com/opencord/voltha-go/rw_core/utils"
khenaidood948f772021-08-11 17:49:24 -040024 "github.com/opencord/voltha-lib-go/v7/pkg/log"
25 "github.com/opencord/voltha-protos/v5/go/common"
Maninder9a1bc0d2020-10-26 11:34:02 +053026)
27
khenaidood948f772021-08-11 17:49:24 -040028func (agent *Agent) logDeviceUpdate(ctx context.Context, prevState, currState *common.AdminState_Types, status *common.OperationResp, err error, desc string) {
29 requestedBy := utils.GetEndpointMetadataFromContext(ctx)
Maninder9a1bc0d2020-10-26 11:34:02 +053030
31 if requestedBy == "" {
32 requestedBy = "NB"
33 }
34
khenaidood948f772021-08-11 17:49:24 -040035 rpc := utils.GetRPCMetadataFromContext(ctx)
36
37 fields := log.Fields{"rpc": rpc, "device-id": agent.deviceID,
Maninder9a1bc0d2020-10-26 11:34:02 +053038 "requested-by": requestedBy, "state-change": agent.stateChangeString(prevState, currState),
khenaidood948f772021-08-11 17:49:24 -040039 "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)
Maninder9a1bc0d2020-10-26 11:34:02 +053047}
48
49func (agent *Agent) stateChangeString(prevState *common.AdminState_Types, currState *common.AdminState_Types) string {
Maninder9a1bc0d2020-10-26 11:34:02 +053050 if prevState != nil && currState != nil && *prevState != *currState {
khenaidoodd3324d2021-04-27 16:22:55 -040051 return fmt.Sprintf("%s->%s", *prevState, *currState)
Maninder9a1bc0d2020-10-26 11:34:02 +053052 }
53 return ""
54}