blob: 40c98aa1c3e8326941b65ad5f672847b250ac427 [file] [log] [blame]
Salman Siddiqui1cf95042020-11-19 00:42:56 +05301/*
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"
yasin sapli5458a1c2021-06-14 22:24:38 +000021 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Salman Siddiqui1cf95042020-11-19 00:42:56 +053022 "github.com/opencord/voltha-protos/v4/go/extension"
23 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25)
26
27type ExtensionManager struct {
28 DeviceManager *Manager
29}
30
31func GetNewExtensionManager(deviceManager *Manager) *ExtensionManager {
32 return &ExtensionManager{DeviceManager: deviceManager}
33}
34
35func (e ExtensionManager) GetExtValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
36 log.EnrichSpan(ctx, log.Fields{"device-id": request.TargetId})
37
38 logger.Debugw(ctx, "GetExtValue", log.Fields{"request": request})
39 agent := e.DeviceManager.getDeviceAgent(ctx, request.TargetId)
40 if agent == nil {
41 return nil, status.Errorf(codes.NotFound, "target-id %s", request.TargetId)
42 }
43
44 response, err := agent.getSingleValue(ctx, request)
45 if err != nil {
46 logger.Errorw(ctx, "Fail-to-get-single-value", log.Fields{"device-id": agent.deviceID, "error": err})
47 return nil, err
48 }
49
50 logger.Debugw(ctx, "GetExtValue response", log.Fields{"response": response})
51 return response, nil
52}
53
54func (e ExtensionManager) SetExtValue(ctx context.Context, request *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
55 log.EnrichSpan(ctx, log.Fields{"device-id": request.TargetId})
56
57 logger.Debugw(ctx, "SetExtValue", log.Fields{"request": request})
58 agent := e.DeviceManager.getDeviceAgent(ctx, request.TargetId)
59 if agent == nil {
60 return nil, status.Errorf(codes.NotFound, "target-id %s", request.TargetId)
61 }
62
63 response, err := agent.setSingleValue(ctx, request)
64 if err != nil {
65 logger.Errorw(ctx, "Fail-to-set-single-value", log.Fields{"device-id": agent.deviceID, "error": err})
66 return nil, err
67 }
68
69 logger.Debugw(ctx, "SetExtValue response", log.Fields{"response": response})
70 return response, nil
71}