Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame^] | 2 | * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 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-protos/v5/go/common" |
| 23 | "github.com/opencord/voltha-protos/v5/go/core" |
| 24 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
| 27 | ) |
| 28 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 29 | func (agent *Agent) getTransientState() core.DeviceTransientState_Types { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 30 | transientStateHandle := agent.transientStateLoader.Lock() |
| 31 | deviceTransientState := transientStateHandle.GetReadOnly() |
| 32 | transientStateHandle.UnLock() |
| 33 | return deviceTransientState |
| 34 | } |
| 35 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 36 | func (agent *Agent) matchTransientState(transientState core.DeviceTransientState_Types) bool { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 37 | transientStateHandle := agent.transientStateLoader.Lock() |
| 38 | defer transientStateHandle.UnLock() |
| 39 | return transientState == transientStateHandle.GetReadOnly() |
| 40 | } |
| 41 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 42 | func (agent *Agent) updateTransientState(ctx context.Context, transientState core.DeviceTransientState_Types) error { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 43 | // Already in same transientState |
| 44 | if transientState == agent.getTransientState() { |
| 45 | return nil |
| 46 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 47 | // Update device transient state |
| 48 | transientStateHandle := agent.transientStateLoader.Lock() |
| 49 | if err := transientStateHandle.Update(ctx, transientState); err != nil { |
| 50 | transientStateHandle.UnLock() |
| 51 | return status.Errorf(codes.Internal, "failed-update-device-transient-state:%s: %s", agent.deviceID, err) |
| 52 | } |
| 53 | transientStateHandle.UnLock() |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func (agent *Agent) isDeletionInProgress() bool { |
| 58 | deviceTransientState := agent.getTransientState() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 59 | return deviceTransientState == core.DeviceTransientState_FORCE_DELETING || |
| 60 | deviceTransientState == core.DeviceTransientState_DELETING_FROM_ADAPTER || |
| 61 | deviceTransientState == core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 62 | } |
| 63 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 64 | func (agent *Agent) isForceDeletingAllowed(deviceTransientState core.DeviceTransientState_Types, device *voltha.Device) bool { |
Himani Chawla | eafdf5c | 2022-01-24 14:27:54 +0530 | [diff] [blame] | 65 | return deviceTransientState != core.DeviceTransientState_FORCE_DELETING |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 66 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 67 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 68 | func (agent *Agent) deleteTransientState(ctx context.Context) error { |
| 69 | transientStateHandle := agent.transientStateLoader.Lock() |
| 70 | if err := transientStateHandle.Delete(ctx); err != nil { |
| 71 | transientStateHandle.UnLock() |
| 72 | return status.Errorf(codes.Internal, "failed-delete-device-transient-state:%s: %s", agent.deviceID, err) |
| 73 | } |
| 74 | transientStateHandle.UnLock() |
| 75 | return nil |
| 76 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 77 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 78 | func (agent *Agent) isInReconcileState(device *voltha.Device) bool { |
Maninder | 581cf4b | 2021-06-16 22:42:07 +0530 | [diff] [blame] | 79 | return device.OperStatus == common.OperStatus_RECONCILING || device.OperStatus == common.OperStatus_RECONCILING_FAILED || |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 80 | agent.matchTransientState(core.DeviceTransientState_RECONCILE_IN_PROGRESS) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 81 | } |