X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fnsd%2Fping_pong_ns%2Fscripts%2Fstart_traffic.py;h=87c309fdb3b3699edc0a57f943957c110b83e7d6;hb=c5a79f06449a144464e46cd35c9c0d3883ec2f8d;hp=bc1003d620319a5e651d0154d3a192643947ab26;hpb=207a026bfa7d705a6c5426346d6b31c5d7be94c0;p=osm%2Fdevops.git diff --git a/src/nsd/ping_pong_ns/scripts/start_traffic.py b/src/nsd/ping_pong_ns/scripts/start_traffic.py index bc1003d6..87c309fd 100755 --- a/src/nsd/ping_pong_ns/scripts/start_traffic.py +++ b/src/nsd/ping_pong_ns/scripts/start_traffic.py @@ -19,6 +19,7 @@ import argparse import logging +import paramiko import os import subprocess import sys @@ -27,42 +28,107 @@ import time import yaml +def ssh(cmd, host, user, password): + """ Run an arbitrary command over SSH. """ + + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + client.connect(host, port=22, username=user, password=password) + + stdin, stdout, stderr = client.exec_command(cmd, get_pty=True) + retcode = stdout.channel.recv_exit_status() + client.close() + + return ( + retcode, + stdout.read().decode('utf-8').strip(), + stderr.read().decode('utf-8').strip() + ) + + def start_traffic(yaml_cfg, logger): '''Use curl and set admin status to enable on pong and ping vnfs''' - def enable_service(mgmt_ip, port, vnf_type): - curl_cmd = 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \ + curl_fmt = 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \ '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \ - '-X POST -d "{{\\"enable\\":true}}" http://{mgmt_ip}:' \ - '{mgmt_port}/api/v1/{vnf_type}/adminstatus/state'. \ - format( - mgmt_ip=mgmt_ip, - mgmt_port=port, - vnf_type=vnf_type) + '-X POST -d "{{ {data} }}" http://127.0.0.1:' \ + '{mgmt_port}/api/v1/{vnf_type}/{url}' + + # Get userid and password for the VNF + user = yaml_cfg['parameter']['ssh-username'] + passwd = yaml_cfg['parameter']['ssh-password'] + + # Get port from user parameter + service_port = yaml_cfg['parameter']['port'] + + service_ip = None + + def exec_cmd(vnf_type, mgmt_ip, port, url, data): + curl_cmd = curl_fmt.format( + mgmt_port=port, + vnf_type=vnf_type, + data=data, + url=url + ) logger.debug("Executing cmd: %s", curl_cmd) - proc = subprocess.run(curl_cmd, shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + rc, out, err = ssh(curl_cmd, mgmt_ip, user, passwd) - logger.debug("Process: {}".format(proc)) + if rc != 0: + logger.error("cmd={}, rc={}, stderr={}, stdout={}". + format(curl_cmd, rc, err, out)) + else: + logger.debug("cmd={}, rc={}, stderr={}, stdout={}". + format(curl_cmd, rc, err, out)) - return proc.returncode + return rc + + def setup_service(mgmt_ip, port, vnf_type): + data = '\\"ip\\":\\"{}\\", \\"port\\":5555'.format(service_ip) + return exec_cmd(vnf_type, mgmt_ip, port, 'server', data) + + def enable_service(mgmt_ip, port, vnf_type): + data='\\"enable\\":true' + url='adminstatus/state' + return exec_cmd(vnf_type, mgmt_ip, port, url, data) # Enable pong service first for index, vnfr in yaml_cfg['vnfr'].items(): logger.debug("VNFR {}: {}".format(index, vnfr)) + def get_cp_ip(cp_name): + for cp in vnfr['connection_point']: + if cp['name'].endswith(cp_name): + return cp['ip_address'] + # Check if it is pong vnf if 'pong_vnf' in vnfr['name']: vnf_type = 'pong' - port = 18889 - rc = enable_service(vnfr['mgmt_ip_address'], port, vnf_type) + mgmt_ip = vnfr['mgmt_ip_address'] + port = vnfr['mgmt_port'] + service_ip = get_cp_ip('cp1') + + max_tries = 60 + tries = 0 + while tries < max_tries: + rc = setup_service(mgmt_ip, port, vnf_type) + tries += 1 + if rc != 0: + logger.error("Setup service for pong failed ({}): {}". + format(tries, rc)) + if rc != 7: + return rc + else: + time.sleep(1) # Sleep for 1 seconds + else: + break + + rc = enable_service(mgmt_ip, port, vnf_type) if rc != 0: logger.error("Enable service for pong failed: {}". format(rc)) return rc - break # Add a delay to provide pong port to come up time.sleep(1) @@ -74,9 +140,31 @@ def start_traffic(yaml_cfg, logger): # Check if it is pong vnf if 'ping_vnf' in vnfr['name']: vnf_type = 'ping' - port = 18888 - rc = enable_service(vnfr['mgmt_ip_address'], port, vnf_type) - break + mgmt_ip = vnfr['mgmt_ip_address'] + port = vnfr['mgmt_port'] + if service_ip is None: + logger.error("Did not find pong ip!!") + return 1 + + max_tries = 30 + tries = 0 + while tries < max_tries: + rc = setup_service(mgmt_ip, port, vnf_type) + tries += 1 + if rc != 0: + logger.error("Setup service for ping failed ({}): {}". + format(tries, rc)) + if rc != 7: + return rc + else: + time.sleep(1) # Sleep for 1 seconds + else: + break + + rc = enable_service(mgmt_ip, port, vnf_type) + if rc != 0: + logger.error("Enable service for ping failed: {}". + format(rc)) return rc