blob: ba1005ae431f1fc1c73d174851a1df88e2d2a100 [file] [log] [blame]
Mahir Gunyelfa6ea272020-06-10 17:03:51 -07001/*
2 * Copyright 2018-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"
khenaidood948f772021-08-11 17:49:24 -040021 "time"
22
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070023 "github.com/gogo/protobuf/proto"
khenaidood948f772021-08-11 17:49:24 -040024 "github.com/opencord/voltha-lib-go/v7/pkg/log"
25 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
26 "github.com/opencord/voltha-protos/v5/go/voltha"
Kent Hagermancba2f302020-07-28 13:37:36 -040027 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070029)
30
31func (agent *Agent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
Himani Chawlab4c25912020-11-12 17:16:38 +053032 logger.Debugw(ctx, "update-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070033
khenaidood948f772021-08-11 17:49:24 -040034 var rpce *voltha.RPCEvent
35 defer func() {
36 if rpce != nil {
37 go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce,
38 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
39 }
40 }()
41
42 cloned, err := agent.getDeviceReadOnly(ctx)
43 if err != nil {
44 return err
45 }
Maninder2195ccc2021-06-23 20:23:01 +053046
47 if !agent.proceedWithRequest(cloned) {
48 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id)
49 }
50
khenaidood948f772021-08-11 17:49:24 -040051 // We need to send the response for the PM Config Updates in a synchronous manner to the adapter.
52 client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint)
53 if err != nil {
54 logger.Errorw(ctx, "grpc-client-nil",
55 log.Fields{
56 "error": err,
57 "device-id": agent.deviceID,
58 "device-type": agent.deviceType,
59 "adapter-endpoint": agent.adapterEndpoint,
60 })
61 return err
62 }
63 _, pmErr := client.UpdatePmConfig(ctx, &ic.PmConfigsInfo{
64 DeviceId: agent.deviceID,
65 PmConfigs: pmConfigs,
66 })
Girish Gowdrad27a1902021-02-23 16:19:08 -080067
Girish Gowdrad27a1902021-02-23 16:19:08 -080068 if pmErr != nil {
khenaidood948f772021-08-11 17:49:24 -040069 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, pmErr.Error(), nil)
Girish Gowdrad27a1902021-02-23 16:19:08 -080070 return pmErr
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070071 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080072 // In case of no error for PM Config update, commit the new PM Config to DB.
khenaidood948f772021-08-11 17:49:24 -040073 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
74 return err
Girish Gowdrad27a1902021-02-23 16:19:08 -080075 }
khenaidood948f772021-08-11 17:49:24 -040076 // the Device properties might have changed due to other concurrent transactions on the device, so get latest copy
77 cloned = agent.cloneDeviceWithoutLock()
78 // commit new pm config
79 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Girish Gowdrad27a1902021-02-23 16:19:08 -080080
khenaidood948f772021-08-11 17:49:24 -040081 // Store back the device to DB and release lock
82 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
83 logger.Errorw(ctx, "error-updating-device-context-to-db", log.Fields{"device-id": agent.deviceID})
84 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil)
85 return err
86 }
87 return nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070088}
89
90func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
91 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
92 return err
93 }
Himani Chawlab4c25912020-11-12 17:16:38 +053094 logger.Debugw(ctx, "init-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070095
Kent Hagermanf6db9f12020-07-22 17:16:19 -040096 cloned := agent.cloneDeviceWithoutLock()
Maninder2195ccc2021-06-23 20:23:01 +053097
98 if !agent.proceedWithRequest(cloned) {
99 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id)
100 }
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700101 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400102 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700103}
104
105func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530106 logger.Debugw(ctx, "list-pm-configs", log.Fields{"device-id": agent.deviceID})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700107
Kent Hagermancba2f302020-07-28 13:37:36 -0400108 device, err := agent.getDeviceReadOnly(ctx)
109 if err != nil {
110 return nil, status.Errorf(codes.Aborted, "%s", err)
111 }
112 return device.PmConfigs, nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700113}