First proposal for the structure for pythonsdk based scenarios
[testsuite/pythonsdk-tests.git] / src / onaptests / steps / base.py
1 from abc import ABC, abstractmethod
2 from typing import List
3
4
5 class BaseStep(ABC):
6     """Base step class."""
7
8     def __init__(self, cleanup: bool = False) -> None:
9         """Step initialization.
10
11         Args:
12             cleanup(bool, optional): Determines if cleanup action should be called.
13
14         """
15         self._steps: List["BaseStep"] = []
16         self._cleanup: bool = cleanup
17         self._parent: "BaseStep" = None
18
19     def add_step(self, step: "BaseStep") -> None:
20         """Add substep.
21
22         Add substep and mark step as a substep parent.
23
24         Args:
25             step (BaseStep): Step object
26         """
27         self._steps.append(step)
28         step._parent: "BaseStep" = self
29
30     @property
31     def parent(self) -> "BaseStep":
32         """Step parent.
33
34         If parent is not set the step is a root one.
35         """
36         return self._parent
37
38     @property
39     def is_root(self) -> bool:
40         """Is a root step.
41
42         Step is a root if has no parent
43
44         Returns:
45             bool: True if step is a root step, False otherwise
46
47         """
48         return self._parent is None
49
50     def execute(self) -> None:
51         """Step's action.
52
53         Run all substeps action before it's own action.
54         Override this method and remember to call `super().action()` before.
55
56         """
57         for step in self._steps:
58             step.execute()
59
60     def cleanup(self) -> None:
61         """Step's cleanup.
62
63         Not all steps has to have cleanup method
64
65         """
66         if self._cleanup:
67             for step in self._steps:
68                 step.cleanup()
69
70
71 class YamlTemplateBaseStep(BaseStep, ABC):
72     """Base YAML template step."""
73
74     @property
75     @abstractmethod
76     def yaml_template(self) -> dict:
77         """YAML template abstract property.
78
79         Every YAML template step need to implement that property.
80
81         Returns:
82             dict: YAML template
83
84         """