]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import errno |
2 | ||
3 | from typing import Dict | |
4 | ||
92f5a8d4 TL |
5 | from ..exception import OpSmException |
6 | ||
7 | class OpSm(object): | |
8 | INIT_STATE_KEY = 'init' | |
9 | ||
10 | FAILED_STATE = 'failed' | |
11 | FINAL_STATE = 'complete' | |
9f95a23c | 12 | CANCEL_STATE = 'canceled' |
92f5a8d4 TL |
13 | |
14 | OP_SM_SUBVOLUME = { | |
15 | INIT_STATE_KEY : FINAL_STATE, | |
16 | } | |
17 | ||
18 | OP_SM_CLONE = { | |
19 | INIT_STATE_KEY : 'pending', | |
9f95a23c TL |
20 | 'pending' : ('in-progress', (FAILED_STATE, CANCEL_STATE)), |
21 | 'in-progress' : (FINAL_STATE, (FAILED_STATE, CANCEL_STATE)), | |
22 | } # type: Dict | |
92f5a8d4 TL |
23 | |
24 | STATE_MACHINES_TYPES = { | |
25 | "subvolume" : OP_SM_SUBVOLUME, | |
26 | "clone" : OP_SM_CLONE, | |
9f95a23c | 27 | } # type: Dict |
92f5a8d4 TL |
28 | |
29 | @staticmethod | |
30 | def is_final_state(state): | |
31 | return state == OpSm.FINAL_STATE | |
32 | ||
33 | @staticmethod | |
34 | def is_failed_state(state): | |
9f95a23c TL |
35 | return state == OpSm.FAILED_STATE or state == OpSm.CANCEL_STATE |
36 | ||
37 | @staticmethod | |
38 | def is_init_state(stm_type, state): | |
39 | stm = OpSm.STATE_MACHINES_TYPES.get(stm_type, None) | |
40 | if not stm: | |
41 | raise OpSmException(-errno.ENOENT, "state machine type '{0}' not found".format(stm_type)) | |
42 | init_state = stm.get(OpSm.INIT_STATE_KEY, None) | |
43 | return init_state == state | |
92f5a8d4 TL |
44 | |
45 | @staticmethod | |
46 | def get_init_state(stm_type): | |
47 | stm = OpSm.STATE_MACHINES_TYPES.get(stm_type, None) | |
48 | if not stm: | |
49 | raise OpSmException(-errno.ENOENT, "state machine type '{0}' not found".format(stm_type)) | |
50 | init_state = stm.get(OpSm.INIT_STATE_KEY, None) | |
51 | if not init_state: | |
52 | raise OpSmException(-errno.ENOENT, "initial state unavailable for state machine '{0}'".format(stm_type)) | |
53 | return init_state | |
54 | ||
55 | @staticmethod | |
56 | def get_next_state(stm_type, current_state, ret): | |
57 | stm = OpSm.STATE_MACHINES_TYPES.get(stm_type, None) | |
58 | if not stm: | |
59 | raise OpSmException(-errno.ENOENT, "state machine type '{0}' not found".format(stm_type)) | |
60 | next_state = stm.get(current_state, None) | |
61 | if not next_state: | |
62 | raise OpSmException(-errno.EINVAL, "invalid current state '{0}'".format(current_state)) | |
9f95a23c TL |
63 | if ret == 0: |
64 | return next_state[0] | |
65 | elif ret == -errno.EINTR: | |
66 | return next_state[1][1] | |
67 | else: | |
68 | return next_state[1][0] |