blob: 176a12653630cbff6b0be502abaddf4be6dd006a [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"
Girish Gowdrad27a1902021-02-23 16:19:08 -080021 "fmt"
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070022 "github.com/gogo/protobuf/proto"
Himani Chawlab4c25912020-11-12 17:16:38 +053023 coreutils "github.com/opencord/voltha-go/rw_core/utils"
yasin sapli5458a1c2021-06-14 22:24:38 +000024 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Maninderdfadc982020-10-28 14:04:33 +053025 "github.com/opencord/voltha-protos/v4/go/voltha"
Kent Hagermancba2f302020-07-28 13:37:36 -040026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
Girish Gowdrad27a1902021-02-23 16:19:08 -080028 "time"
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
Kent Hagermanf6db9f12020-07-22 17:16:19 -040034 cloned := agent.cloneDeviceWithoutLock()
Maninder2195ccc2021-06-23 20:23:01 +053035
36 if !agent.proceedWithRequest(cloned) {
37 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id)
38 }
39
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070040 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Girish Gowdrad27a1902021-02-23 16:19:08 -080041
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070042 // Send the request to the adapter
Rohan Agrawalcf12f202020-08-03 04:42:01 +000043 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Girish Gowdrad27a1902021-02-23 16:19:08 -080044 defer cancel()
Himani Chawlab4c25912020-11-12 17:16:38 +053045 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
46
Girish Gowdrad27a1902021-02-23 16:19:08 -080047 ch, pmErr := agent.adapterProxy.UpdatePmConfigs(subCtx, cloned, pmConfigs)
48 if pmErr != nil {
49 return pmErr
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070050 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080051
52 var rpce *voltha.RPCEvent
53 defer func() {
54 if rpce != nil {
Himani Chawla606a4f02021-03-23 19:45:58 +053055 agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce,
56 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Girish Gowdrad27a1902021-02-23 16:19:08 -080057 }
58 }()
59 // We need to send the response for the PM Config Updates in a synchronous manner to the caller.
60 select {
61 case rpcResponse, ok := <-ch:
62 if !ok {
63 pmErr = fmt.Errorf("response-channel-closed")
64 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, pmErr.Error(), nil)
65 } else if rpcResponse.Err != nil {
66 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, rpcResponse.Err.Error(), nil)
67 pmErr = rpcResponse.Err
68 }
69 case <-ctx.Done():
70 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, ctx.Err().Error(), nil)
71 pmErr = ctx.Err()
72 }
73
74 // In case of no error for PM Config update, commit the new PM Config to DB.
75 if pmErr == nil {
76 // acquire lock for update the device to DB
77 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
78 return err
79 }
80 // the Device properties might have changed due to other concurrent transactions on the device, so get latest copy
81 cloned = agent.cloneDeviceWithoutLock()
82 // commit new pm config
83 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
84
85 // Store back the device to DB and release lock
86 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
87 logger.Errorw(ctx, "error-updating-device-context-to-db", log.Fields{"device-id": agent.deviceID})
88 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil)
89 return err
90 }
91 }
92
93 return pmErr
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070094}
95
96func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
97 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
98 return err
99 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530100 logger.Debugw(ctx, "init-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700101
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400102 cloned := agent.cloneDeviceWithoutLock()
Maninder2195ccc2021-06-23 20:23:01 +0530103
104 if !agent.proceedWithRequest(cloned) {
105 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id)
106 }
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700107 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400108 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700109}
110
111func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530112 logger.Debugw(ctx, "list-pm-configs", log.Fields{"device-id": agent.deviceID})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700113
Kent Hagermancba2f302020-07-28 13:37:36 -0400114 device, err := agent.getDeviceReadOnly(ctx)
115 if err != nil {
116 return nil, status.Errorf(codes.Aborted, "%s", err)
117 }
118 return device.PmConfigs, nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700119}