blob: d72611579d0f18754e4d6e22d5a6441bc70ecb03 [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"
21
22 "github.com/gogo/protobuf/proto"
Himani Chawlab4c25912020-11-12 17:16:38 +053023 coreutils "github.com/opencord/voltha-go/rw_core/utils"
Maninderdfadc982020-10-28 14:04:33 +053024 "github.com/opencord/voltha-lib-go/v4/pkg/log"
25 "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"
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070028)
29
30func (agent *Agent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
31 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
32 return err
33 }
Himani Chawlab4c25912020-11-12 17:16:38 +053034 logger.Debugw(ctx, "update-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070035
Kent Hagermanf6db9f12020-07-22 17:16:19 -040036 cloned := agent.cloneDeviceWithoutLock()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070037 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
38 // Store the device
Kent Hagermanf6db9f12020-07-22 17:16:19 -040039 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070040 return err
41 }
42 // 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)
Himani Chawlab4c25912020-11-12 17:16:38 +053044 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
45
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070046 ch, err := agent.adapterProxy.UpdatePmConfigs(subCtx, cloned, pmConfigs)
47 if err != nil {
48 cancel()
49 return err
50 }
51 go agent.waitForAdapterResponse(subCtx, cancel, "updatePmConfigs", ch, agent.onSuccess, agent.onFailure)
52 return nil
53}
54
55func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
56 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
57 return err
58 }
Himani Chawlab4c25912020-11-12 17:16:38 +053059 logger.Debugw(ctx, "init-pm-configs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070060
Kent Hagermanf6db9f12020-07-22 17:16:19 -040061 cloned := agent.cloneDeviceWithoutLock()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070062 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Kent Hagermanf6db9f12020-07-22 17:16:19 -040063 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070064}
65
66func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +053067 logger.Debugw(ctx, "list-pm-configs", log.Fields{"device-id": agent.deviceID})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070068
Kent Hagermancba2f302020-07-28 13:37:36 -040069 device, err := agent.getDeviceReadOnly(ctx)
70 if err != nil {
71 return nil, status.Errorf(codes.Aborted, "%s", err)
72 }
73 return device.PmConfigs, nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070074}