blob: 0adf57d5e75c8c89e9ca466702a13771e82376a1 [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"
Maninder0aabf0c2021-03-17 14:55:14 +053021 "github.com/opencord/voltha-protos/v4/go/common"
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053022 "github.com/opencord/voltha-protos/v4/go/voltha"
23 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25)
26
27func (agent *Agent) getTransientState() voltha.DeviceTransientState_Types {
28 transientStateHandle := agent.transientStateLoader.Lock()
29 deviceTransientState := transientStateHandle.GetReadOnly()
30 transientStateHandle.UnLock()
31 return deviceTransientState
32}
33
Maninder0aabf0c2021-03-17 14:55:14 +053034func (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 Chawla2ba1c9c2020-10-07 13:19:03 +053040func (agent *Agent) updateTransientState(ctx context.Context, transientState voltha.DeviceTransientState_Types) error {
Maninder0aabf0c2021-03-17 14:55:14 +053041 // Already in same transientState
42 if transientState == agent.getTransientState() {
43 return nil
44 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053045 // 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
55func (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
62func (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}
67func (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}
Maninder0aabf0c2021-03-17 14:55:14 +053076
Maninder2195ccc2021-06-23 20:23:01 +053077func (agent *Agent) isInReconcileState(device *voltha.Device) bool {
Maninder581cf4b2021-06-16 22:42:07 +053078 return device.OperStatus == common.OperStatus_RECONCILING || device.OperStatus == common.OperStatus_RECONCILING_FAILED ||
Maninder0aabf0c2021-03-17 14:55:14 +053079 agent.matchTransientState(voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
80}