blob: d9df9685707dd10661ade3650b392c1685613094 [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"
23 "github.com/opencord/voltha-lib-go/v3/pkg/log"
24 "github.com/opencord/voltha-protos/v3/go/voltha"
Kent Hagermancba2f302020-07-28 13:37:36 -040025 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070027)
28
29func (agent *Agent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
30 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
31 return err
32 }
Rohan Agrawal31f21802020-06-12 05:38:46 +000033 logger.Debugw(ctx, "updatePmConfigs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070034
Kent Hagermanf6db9f12020-07-22 17:16:19 -040035 cloned := agent.cloneDeviceWithoutLock()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070036 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
37 // Store the device
Kent Hagermanf6db9f12020-07-22 17:16:19 -040038 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070039 return err
40 }
41 // Send the request to the adapter
Rohan Agrawalcf12f202020-08-03 04:42:01 +000042 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070043 ch, err := agent.adapterProxy.UpdatePmConfigs(subCtx, cloned, pmConfigs)
44 if err != nil {
45 cancel()
46 return err
47 }
48 go agent.waitForAdapterResponse(subCtx, cancel, "updatePmConfigs", ch, agent.onSuccess, agent.onFailure)
49 return nil
50}
51
52func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
53 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
54 return err
55 }
Rohan Agrawal31f21802020-06-12 05:38:46 +000056 logger.Debugw(ctx, "initPmConfigs", log.Fields{"device-id": pmConfigs.Id})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070057
Kent Hagermanf6db9f12020-07-22 17:16:19 -040058 cloned := agent.cloneDeviceWithoutLock()
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070059 cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs)
Kent Hagermanf6db9f12020-07-22 17:16:19 -040060 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070061}
62
63func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +000064 logger.Debugw(ctx, "listPmConfigs", log.Fields{"device-id": agent.deviceID})
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070065
Kent Hagermancba2f302020-07-28 13:37:36 -040066 device, err := agent.getDeviceReadOnly(ctx)
67 if err != nil {
68 return nil, status.Errorf(codes.Aborted, "%s", err)
69 }
70 return device.PmConfigs, nil
Mahir Gunyelfa6ea272020-06-10 17:03:51 -070071}