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