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