blob: 776d60d2b94fbcca1bfb9f60bd5e3938f9d2b33a [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"
21 "github.com/opencord/voltha-protos/v4/go/voltha"
22 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
24)
25
26func (agent *Agent) getTransientState() voltha.DeviceTransientState_Types {
27 transientStateHandle := agent.transientStateLoader.Lock()
28 deviceTransientState := transientStateHandle.GetReadOnly()
29 transientStateHandle.UnLock()
30 return deviceTransientState
31}
32
33func (agent *Agent) updateTransientState(ctx context.Context, transientState voltha.DeviceTransientState_Types) error {
34 // Update device transient state
35 transientStateHandle := agent.transientStateLoader.Lock()
36 if err := transientStateHandle.Update(ctx, transientState); err != nil {
37 transientStateHandle.UnLock()
38 return status.Errorf(codes.Internal, "failed-update-device-transient-state:%s: %s", agent.deviceID, err)
39 }
40 transientStateHandle.UnLock()
41 return nil
42}
43
44func (agent *Agent) isDeletionInProgress() bool {
45 deviceTransientState := agent.getTransientState()
46 return deviceTransientState == voltha.DeviceTransientState_FORCE_DELETING ||
47 deviceTransientState == voltha.DeviceTransientState_DELETING_FROM_ADAPTER ||
48 deviceTransientState == voltha.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE
49}
50
51func (agent *Agent) isStateDeleting(deviceTransientState voltha.DeviceTransientState_Types) bool {
52 return deviceTransientState == voltha.DeviceTransientState_FORCE_DELETING ||
53 deviceTransientState == voltha.DeviceTransientState_DELETING_FROM_ADAPTER ||
54 deviceTransientState == voltha.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE
55}
56func (agent *Agent) deleteTransientState(ctx context.Context) error {
57 transientStateHandle := agent.transientStateLoader.Lock()
58 if err := transientStateHandle.Delete(ctx); err != nil {
59 transientStateHandle.UnLock()
60 return status.Errorf(codes.Internal, "failed-delete-device-transient-state:%s: %s", agent.deviceID, err)
61 }
62 transientStateHandle.UnLock()
63 return nil
64}