blob: 2e355aa88e92b3a539e4137602d340141ae4354c [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()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070035 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Girish Gowdrad27a1902021-02-23 16:19:08 -080036
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070037 // Send the request to the adapter
Rohan Agrawalcf12f202020-08-03 04:42:01 +000038 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Girish Gowdrad27a1902021-02-23 16:19:08 -080039 defer cancel()
Himani Chawlab4c25912020-11-12 17:16:38 +053040 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
41
Girish Gowdrad27a1902021-02-23 16:19:08 -080042 ch, pmErr := agent.adapterProxy.UpdatePmConfigs(subCtx, cloned, pmConfigs)
43 if pmErr != nil {
44 return pmErr
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070045 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080046
47 var rpce *voltha.RPCEvent
48 defer func() {
49 if rpce != nil {
Himani Chawla606a4f02021-03-23 19:45:58 +053050 agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce,
51 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Girish Gowdrad27a1902021-02-23 16:19:08 -080052 }
53 }()
54 // We need to send the response for the PM Config Updates in a synchronous manner to the caller.
55 select {
56 case rpcResponse, ok := <-ch:
57 if !ok {
58 pmErr = fmt.Errorf("response-channel-closed")
59 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, pmErr.Error(), nil)
60 } else if rpcResponse.Err != nil {
61 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, rpcResponse.Err.Error(), nil)
62 pmErr = rpcResponse.Err
63 }
64 case <-ctx.Done():
65 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, ctx.Err().Error(), nil)
66 pmErr = ctx.Err()
67 }
68
69 // In case of no error for PM Config update, commit the new PM Config to DB.
70 if pmErr == nil {
71 // acquire lock for update the device to DB
72 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
73 return err
74 }
75 // the Device properties might have changed due to other concurrent transactions on the device, so get latest copy
76 cloned = agent.cloneDeviceWithoutLock()
77 // commit new pm config
78 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
79
80 // Store back the device to DB and release lock
81 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
82 logger.Errorw(ctx, "error-updating-device-context-to-db", log.Fields{"device-id": agent.deviceID})
83 rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil)
84 return err
85 }
86 }
87
88 return pmErr
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070089}
90
91func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
92 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
93 return err
94 }
Himani Chawlab4c25912020-11-12 17:16:38 +053095 logger.Debugw(ctx, "init-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070096
Kent Hagermanf6db9f12020-07-22 17:16:19 -040097 cloned := agent.cloneDeviceWithoutLock()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070098 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Kent Hagermanf6db9f12020-07-22 17:16:19 -040099 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700100}
101
102func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530103 logger.Debugw(ctx, "list-pm-configs", log.Fields{"device-id": agent.deviceID})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700104
Kent Hagermancba2f302020-07-28 13:37:36 -0400105 device, err := agent.getDeviceReadOnly(ctx)
106 if err != nil {
107 return nil, status.Errorf(codes.Aborted, "%s", err)
108 }
109 return device.PmConfigs, nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -0700110}