blob: ea105dd26d217ef0a533c93604313fe832076743 [file] [log] [blame]
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301/*
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
17package device
18
19import (
20 "context"
khenaidood948f772021-08-11 17:49:24 -040021
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 Chawla2ba1c9c2020-10-07 13:19:03 +053025 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
27)
28
khenaidood948f772021-08-11 17:49:24 -040029func (agent *Agent) getTransientState() core.DeviceTransientState_Types {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053030 transientStateHandle := agent.transientStateLoader.Lock()
31 deviceTransientState := transientStateHandle.GetReadOnly()
32 transientStateHandle.UnLock()
33 return deviceTransientState
34}
35
khenaidood948f772021-08-11 17:49:24 -040036func (agent *Agent) matchTransientState(transientState core.DeviceTransientState_Types) bool {
Maninder0aabf0c2021-03-17 14:55:14 +053037 transientStateHandle := agent.transientStateLoader.Lock()
38 defer transientStateHandle.UnLock()
39 return transientState == transientStateHandle.GetReadOnly()
40}
41
khenaidood948f772021-08-11 17:49:24 -040042func (agent *Agent) updateTransientState(ctx context.Context, transientState core.DeviceTransientState_Types) error {
Maninder0aabf0c2021-03-17 14:55:14 +053043 // Already in same transientState
44 if transientState == agent.getTransientState() {
45 return nil
46 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053047 // 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
57func (agent *Agent) isDeletionInProgress() bool {
58 deviceTransientState := agent.getTransientState()
khenaidood948f772021-08-11 17:49:24 -040059 return deviceTransientState == core.DeviceTransientState_FORCE_DELETING ||
60 deviceTransientState == core.DeviceTransientState_DELETING_FROM_ADAPTER ||
61 deviceTransientState == core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053062}
63
khenaidood948f772021-08-11 17:49:24 -040064func (agent *Agent) isForceDeletingAllowed(deviceTransientState core.DeviceTransientState_Types, device *voltha.Device) bool {
65 return deviceTransientState != core.DeviceTransientState_FORCE_DELETING &&
66 (device.OperStatus != common.OperStatus_RECONCILING ||
67 !agent.matchTransientState(core.DeviceTransientState_RECONCILE_IN_PROGRESS))
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053068}
khenaidood948f772021-08-11 17:49:24 -040069
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053070func (agent *Agent) deleteTransientState(ctx context.Context) error {
71 transientStateHandle := agent.transientStateLoader.Lock()
72 if err := transientStateHandle.Delete(ctx); err != nil {
73 transientStateHandle.UnLock()
74 return status.Errorf(codes.Internal, "failed-delete-device-transient-state:%s: %s", agent.deviceID, err)
75 }
76 transientStateHandle.UnLock()
77 return nil
78}
Maninder0aabf0c2021-03-17 14:55:14 +053079
Maninder2195ccc2021-06-23 20:23:01 +053080func (agent *Agent) isInReconcileState(device *voltha.Device) bool {
Maninder581cf4b2021-06-16 22:42:07 +053081 return device.OperStatus == common.OperStatus_RECONCILING || device.OperStatus == common.OperStatus_RECONCILING_FAILED ||
khenaidood948f772021-08-11 17:49:24 -040082 agent.matchTransientState(core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +053083}