Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [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 | "time" |
| 22 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 23 | "github.com/gogo/protobuf/proto" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 25 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func (agent *Agent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 32 | logger.Debugw(ctx, "update-pm-configs", log.Fields{"device-id": pmConfigs.Id}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 33 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 34 | var rpce *voltha.RPCEvent |
| 35 | defer func() { |
| 36 | if rpce != nil { |
| 37 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 38 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
| 39 | } |
| 40 | }() |
| 41 | |
| 42 | cloned, err := agent.getDeviceReadOnly(ctx) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 46 | |
| 47 | if !agent.proceedWithRequest(cloned) { |
| 48 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id) |
| 49 | } |
| 50 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 51 | // We need to send the response for the PM Config Updates in a synchronous manner to the adapter. |
| 52 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 53 | if err != nil { |
| 54 | logger.Errorw(ctx, "grpc-client-nil", |
| 55 | log.Fields{ |
| 56 | "error": err, |
| 57 | "device-id": agent.deviceID, |
| 58 | "device-type": agent.deviceType, |
| 59 | "adapter-endpoint": agent.adapterEndpoint, |
| 60 | }) |
| 61 | return err |
| 62 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 63 | _, pmErr := client.UpdatePmConfig(ctx, &ca.PmConfigsInfo{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 64 | DeviceId: agent.deviceID, |
| 65 | PmConfigs: pmConfigs, |
| 66 | }) |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 67 | |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 68 | if pmErr != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 69 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, pmErr.Error(), nil) |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 70 | return pmErr |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 71 | } |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 72 | // In case of no error for PM Config update, commit the new PM Config to DB. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 73 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 74 | return err |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 75 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 76 | // the Device properties might have changed due to other concurrent transactions on the device, so get latest copy |
| 77 | cloned = agent.cloneDeviceWithoutLock() |
| 78 | // commit new pm config |
| 79 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
Girish Gowdra | d27a190 | 2021-02-23 16:19:08 -0800 | [diff] [blame] | 80 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 81 | // Store back the device to DB and release lock |
| 82 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
| 83 | logger.Errorw(ctx, "error-updating-device-context-to-db", log.Fields{"device-id": agent.deviceID}) |
| 84 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 85 | return err |
| 86 | } |
| 87 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func (agent *Agent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 91 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 92 | return err |
| 93 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 94 | logger.Debugw(ctx, "init-pm-configs", log.Fields{"device-id": pmConfigs.Id}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 95 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 96 | cloned := agent.cloneDeviceWithoutLock() |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 97 | |
| 98 | if !agent.proceedWithRequest(cloned) { |
| 99 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", cloned.Id) |
| 100 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 101 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 102 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func (agent *Agent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 106 | logger.Debugw(ctx, "list-pm-configs", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 107 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 108 | device, err := agent.getDeviceReadOnly(ctx) |
| 109 | if err != nil { |
| 110 | return nil, status.Errorf(codes.Aborted, "%s", err) |
| 111 | } |
| 112 | return device.PmConfigs, nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 113 | } |