3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 2022 AT&T Intellectual Property. All rights
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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 # ===================================================================
25 """Network Slicing Simulators module."""
28 from subprocess import check_output, run
29 from onapsdk.configuration import settings
30 from waiting import wait
32 logging.config.dictConfig(settings.LOG_CONFIG)
33 logger = logging.getLogger("Network Slicing Simulators k8s")
36 """Network Slicing simulators controls the simulators k8s deployment."""
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')
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()
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()
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)
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)
73 logger.info("Network slicing sims is Up")
75 logger.info("Network slicing sims is Down")
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")
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)
91 logger.info("Network slicing sims are Down")
93 logger.info("Network slicing sims is still Up")
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")