4 from robot.api import logger
7 XNF_SIMULATOR_NAME = "xNF Simulator"
8 HV_VES_COLLECTOR_NAMESPACE="onap"
9 HV_VES_GROUP_ID="org.onap.dcaegen2.collectors.hv-ves"
10 SIMULATOR_IMAGE_NAME = HV_VES_COLLECTOR_NAMESPACE + "/" + HV_VES_GROUP_ID + ".hv-collector-xnf-simulator"
11 SIMULATOR_IMAGE_FULL_NAME = os.getenv("DOCKER_REGISTRY_PREFIX") + SIMULATOR_IMAGE_NAME + ":latest"
12 WORKSPACE_ENV = os.getenv("WORKSPACE")
13 certificates_dir_path = WORKSPACE_ENV + "/plans/dcaegen2-collectors-hv-ves/testsuites/ssl/"
14 collector_certs_lookup_dir = "/etc/ves-hv/"
15 ONE_SECOND_IN_NANOS = 10 ** 9
18 class XnfSimulatorLibrary:
20 def start_xnf_simulators(self, list_of_ports,
21 should_use_valid_certs=True,
22 should_disable_ssl=False,
23 should_connect_to_unencrypted_hv_ves=False):
24 logger.info("Creating " + str(len(list_of_ports)) + " xNF Simulator containers")
25 dockerClient = docker.from_env()
27 self.pullImageIfAbsent(dockerClient)
28 logger.info("Using image: " + SIMULATOR_IMAGE_FULL_NAME)
30 simulators_addresses = self.create_containers(dockerClient,
32 should_use_valid_certs,
34 should_connect_to_unencrypted_hv_ves)
36 self.assert_containers_startup_was_successful(dockerClient)
38 return simulators_addresses
40 def pullImageIfAbsent(self, dockerClient):
42 dockerClient.images.get(SIMULATOR_IMAGE_FULL_NAME)
44 logger.console("Image " + SIMULATOR_IMAGE_FULL_NAME + " will be pulled from repository. "
45 "This can take a while.")
46 dockerClient.images.pull(SIMULATOR_IMAGE_FULL_NAME)
48 def create_containers(self,
51 should_use_valid_certs,
53 should_connect_to_unencrypted_hv_ves):
54 simulators_addresses = []
55 for port in list_of_ports:
56 xnf = XnfSimulator(port, should_use_valid_certs, should_disable_ssl, should_connect_to_unencrypted_hv_ves)
57 container = self.run_simulator(dockerClient, xnf)
58 logger.info("Started container: " + container.name + " " + container.id)
59 simulators_addresses.append(container.name + ":" + xnf.port)
60 return simulators_addresses
62 def run_simulator(self, dockerClient, xnf):
63 xNF_startup_command = xnf.get_startup_command()
64 xNF_healthcheck_command = xnf.get_healthcheck_command()
66 logger.info("Startup command: " + str(xNF_startup_command))
67 logger.info("Healthcheck command: " + str(xNF_healthcheck_command))
68 return dockerClient.containers.run(SIMULATOR_IMAGE_FULL_NAME,
69 command=xNF_startup_command,
70 healthcheck=xNF_healthcheck_command,
72 network="ves-hv-default",
73 ports={port + "/tcp": port},
74 volumes=self.container_volumes(),
75 name=xnf.container_name_prefix + port)
77 def container_volumes(self):
78 return {certificates_dir_path: {"bind": collector_certs_lookup_dir, "mode": 'rw'}}
80 def assert_containers_startup_was_successful(self, dockerClient):
82 check_interval_in_seconds = 5
83 for _ in range(checks_amount):
84 sleep(check_interval_in_seconds)
85 all_containers_healthy = True
86 for container in self.get_simulators_list(dockerClient):
87 all_containers_healthy = all_containers_healthy and self.is_container_healthy(container)
88 if (all_containers_healthy):
90 raise ContainerException("One of xNF simulators containers did not pass the healthcheck.")
92 def is_container_healthy(self, container):
93 container_health = container.attrs['State']['Health']['Status']
94 return container_health == 'healthy' and container.status == 'running'
96 def stop_and_remove_all_xnf_simulators(self, suite_name):
97 dockerClient = docker.from_env()
98 for container in self.get_simulators_list(dockerClient):
99 logger.info("Stopping and removing container: " + container.id)
100 log_filename = WORKSPACE_ENV + "/archives/containers_logs/" + \
101 suite_name.split(".")[-1] + "_" + container.name + ".log"
102 file = open(log_filename, "w+")
103 file.write(container.logs())
109 def get_simulators_list(self, dockerClient):
110 return dockerClient.containers.list(filters={"ancestor": SIMULATOR_IMAGE_FULL_NAME}, all=True)
112 def send_messages(self, simulator_url, message_filepath):
113 logger.info("Reading message to simulator from: " + message_filepath)
115 file = open(message_filepath, "rb")
119 logger.info("POST at: " + simulator_url)
120 resp = HttpRequests.session_without_env().post(simulator_url, data=data, timeout=5)
121 HttpRequests.checkStatusCode(resp.status_code, XNF_SIMULATOR_NAME)
125 container_name_prefix = "ves-hv-collector-xnf-simulator"
129 should_use_valid_certs,
131 should_connect_to_unencrypted_hv_ves):
133 cert_name_prefix = "" if should_use_valid_certs else "untrusted"
134 certificates_path_with_file_prefix = collector_certs_lookup_dir + cert_name_prefix
135 self.key_store_path = certificates_path_with_file_prefix + "client.p12"
136 self.trust_store_path = certificates_path_with_file_prefix + "trust.p12"
137 self.sec_store_passwd = "onaponap"
138 self.disable_ssl = should_disable_ssl
139 self.hv_collector_host = "unencrypted-ves-hv-collector" \
140 if should_connect_to_unencrypted_hv_ves else "ves-hv-collector"
142 def get_startup_command(self):
143 startup_command = ["--listen-port", self.port,
144 "--ves-host", self.hv_collector_host,
145 "--ves-port", "6061",
146 "--key-store", self.key_store_path,
147 "--trust-store", self.trust_store_path,
148 "--key-store-password", self.sec_store_passwd,
149 "--trust-store-password", self.sec_store_passwd
152 startup_command.append("--ssl-disable")
153 return startup_command
155 def get_healthcheck_command(self):
157 "interval": 5 * ONE_SECOND_IN_NANOS,
158 "timeout": 3 * ONE_SECOND_IN_NANOS,
160 "test": ["CMD", "curl", "--request", "GET",
161 "--fail", "--silent", "--show-error",
162 "localhost:" + self.port + "/healthcheck"]
166 class ContainerException(Exception):
167 def __init__(self, message):
168 super(ContainerException, self).__init__(message)