61e97428baca847e78f525d9cae2c6bcafb4e2d6
[it/dep.git] / smo-install / test / pythonsdk / src / orantests / network_slicing / preparation / ns_simulators.py
1 #!/usr/bin/env python3
2 ###
3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 2022 AT&T Intellectual Property. All rights
7 #                             reserved.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END============================================
21 # ===================================================================
22 #
23 ###
24
25 """Network Slicing Simulators module."""
26 import logging
27 import logging.config
28 from subprocess import check_output, run
29 from onapsdk.configuration import settings
30 from waiting import wait
31
32 logging.config.dictConfig(settings.LOG_CONFIG)
33 logger = logging.getLogger("Network Slicing Simulators k8s")
34
35 class NsSimulators():
36     """Network Slicing simulators controls the simulators k8s deployment."""
37
38     @staticmethod
39     def start_ns_simulators():
40         """Start all simulators."""
41         logger.info("Start the network slicing simulators")
42         cmd = "kubectl create namespace nssimulators"
43         check_output(cmd, shell=True).decode('utf-8')
44         cmd = f"helm install --debug ns-simulators local/ns-simulators --namespace nssimulators"
45         check_output(cmd, shell=True).decode('utf-8')
46
47     def start_and_wait_ns_simulators(self):
48         """Start and wait for all simulators."""
49         logger.info("Start the network slicing simulators")
50         self.start_ns_simulators()
51         NsSimulators.wait_for_ns_simulators_to_be_running()
52
53     def stop_and_wait_ns_simulators(self):
54         """Stop and wait for all simulators."""
55         logger.info("Stop the network slicing simulators")
56         self.stop_ns_simulators()
57         NsSimulators.wait_for_ns_simulators_to_be_stopped()
58
59     @staticmethod
60     def stop_ns_simulators():
61         """Stop the simulators."""
62         logger.info("Clean up any network slicing simulators")
63         cmd = "kubectl delete namespace nssimulators"
64         run(cmd, shell=True, check=False)
65
66     @staticmethod
67     def is_ns_simulators_up() -> bool:
68         """Check if the network slicing simulators are up."""
69         cmd = "kubectl get pods --field-selector status.phase!=Running -n nssimulators"
70         result = check_output(cmd, shell=True).decode('utf-8')
71         logger.info("Checking if network slicing simulators is UP: %s", result)
72         if result == '':
73             logger.info("Network slicing sims is Up")
74             return True
75         logger.info("Network slicing sims is Down")
76         return False
77
78
79     @staticmethod
80     def wait_for_ns_simulators_to_be_running():
81         """Check and wait for the network slicing sims to be running."""
82         wait(lambda: NsSimulators.is_ns_simulators_up(), sleep_seconds=settings.NETWORK_SIMULATOR_CHECK_RETRY, timeout_seconds=settings.NETWORK_SIMULATOR_CHECK_TIMEOUT, waiting_for="Network slicing simulators to be ready")
83
84     @staticmethod
85     def is_ns_simulators_stopped() -> bool:
86         """Check if the network slicing simulators are stopped."""
87         cmd = "kubectl get ns | grep nssimulators | wc -l"
88         result = check_output(cmd, shell=True).decode('utf-8')
89         logger.info("Checking if network slicing simulators is stopped: %s", result)
90         if int(result) == 0:
91             logger.info("Network slicing sims are Down")
92             return True
93         logger.info("Network slicing sims is still Up")
94         return False
95
96     @staticmethod
97     def wait_for_ns_simulators_to_be_stopped():
98         """Check and wait for the network slicing sims to be stopped."""
99         wait(lambda: NsSimulators.is_ns_simulators_stopped(), sleep_seconds=settings.NETWORK_SIMULATOR_CHECK_RETRY, timeout_seconds=settings.NETWORK_SIMULATOR_CHECK_TIMEOUT, waiting_for="Network slicing simulators to be ready")