Ejemplo n.º 1
0
def termination(time_start, service_instance_uuid, sp_path, _exit):
	# Terminate the service
	ter_req = tnglib.service_terminate(service_instance_uuid)

	print("Termination request made")

	# Waiting for termination to finish
	counter = 0
	while counter < 12:
		time.sleep(10)
		request = tnglib.get_request(ter_req[1])
		print("Waiting for termination, current status: " + request[1]['status'])

		if request[1]['status'] == 'READY':
			print("termination finished successfully")
			break

		if request[1]['status'] == 'ERROR':
			print("termination finished in error mode")
			time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
			get_logging(time_start, time_finish, query="source:{} AND type:E".format(sp_path))
			_exit += 1
			break
		counter += 1

	if counter == 12:
		print("termination took longer than 2 minutes, aborting")
		time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
		get_logging(time_start, time_finish, query="source:{} AND type:E".format(sp_path))
		_exit += 1

	# clean all packages
	tnglib.remove_all_packages()
Ejemplo n.º 2
0
def dispatch(args):
    """
    post process the arguments and link them to specific actions
    """

    # Handle the verbose argument
    init_logger(args.verbose)

    # abort if no subcommand is provided
    if args.subparser_name is None:
        print("Missing subcommand. Type tng-cli -h")
        exit(1)

    # Handle --url argument and set environment
    if args.sp_url:
        tnglib.set_sp_path(args.sp_url)
    else:
        if 'SP_PATH' in os.environ:
            tnglib.set_sp_path(os.environ["SP_PATH"])
        else:
            print("Missing path to SP. Use env SP_PATH or tng-cli -u")
            exit(1)

    # Set timeout
    if 'TIMEOUT' in os.environ:
        tnglib.set_timeout(os.environ["TIMEOUT"])

    # Check if the SP is reachable
    if not tnglib.sp_health_check():
        print("Couldn't reach SP at \"" + tnglib.get_sp_path() + "\"")
        exit(1)

    # Check if token exists
    token = tnglib.get_token()

    if token[0]:
        if tnglib.is_token_valid():
            # pass token into headers
            tnglib.add_token_to_header(token[1])
        else:
            if args.subparser_name != 'login':
                print(
                    "Token is outdated. Obtain a new token through tng-cli login"
                )
                exit(1)

    # login subcommand
    if args.subparser_name == 'login':
        # login needs exactly one argument
        if args.username:
            pswd = getpass.getpass('Password:'******'package':
        # packages needs exactly one argument
        sel_args = [args.list, args.clean, args.upload, args.remove, args.get]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "Missing arguments for tng-cli package. " \
                  "Type tng-cli package -h"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Too many arguments for subcommand package. " \
                  "ype tng-cli package -h"
            print(msg)
            exit(1)

        if args.list:
            res, mes = tnglib.get_packages()
            order = ['package_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.upload:
            # Check if argument is a file
            if not args.upload.endswith('.tgo'):
                print("File or url does not point towards 5GTANTGO package.")
                exit(1)
            elif args.upload[:4] == 'http':
                res, mes = tnglib.upload_package(args.upload, url=True)
            elif not os.path.exists(args.upload):
                print("Input not a known file or url.")
                exit(1)
            else:
                res, mes = tnglib.upload_package(args.upload)
                print(mes)
                print(res)
                exit(not res)

        if args.remove:
            res, mes = tnglib.remove_package(args.remove)
            print(mes)
            exit(not res)

        if args.clean:
            res, mes = tnglib.remove_all_packages()
            for pkg in mes:
                print(pkg[1])
            exit(not res)

        if args.get:
            res, mes = tnglib.get_package(args.get)
            form_print(mes)
            exit(not res)

    # requests subcommand
    elif args.subparser_name == 'request':

        if bool(args.get):
            res, mes = tnglib.get_request(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_requests()
            order = [
                'request_uuid', 'request_type', 'status', 'created_at',
                'instance_uuid'
            ]
            form_print(mes, order)
            exit(not res)

    #monitor subcommand
    elif args.subparser_name == 'monitor':
        sel_args = [
            args.target_list, args.service_list, args.metric_list,
            args.vnf_uuid, args.vdu_uuid, args.metric_name, args.vnv_tests,
            args.test_uuid
        ]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "Missing arguments for tng-cli monitor. " \
                  "Type tng-cli monitor -h"
            print(msg)
            exit(1)

        if arg_sum > 3:
            msg = "Too many arguments for subcommand monitor. " \
                  "Type tng-cli monitor -h"
            print(msg)
            exit(1)

        if args.vnv_tests:
            if args.test_uuid:
                res, mes = tnglib.get_vnv_tests(args.test_uuid)
                for m in mes:
                    if 'data' in m:
                        cwd = os.getcwd()
                        fn = cwd + '/' + m['test_uuid'] + '.yaml'
                        m['datafile'] = m['test_uuid'] + '.yaml'
                        DataFile = open(fn, 'w')
                        DataFile.write(yaml.dump(m['data'], indent=4))
                        DataFile.close()
                order = [
                    'test_uuid', 'srv_uuid', 'started', 'terminated',
                    'datafile'
                ]
                form_print(mes, order)

            else:
                res, mes = tnglib.get_vnv_tests(None)
                order = ['test_uuid', 'srv_uuid', 'started', 'terminated']
                form_print(mes, order)

            exit(not res)

        if args.target_list:
            res, mes = tnglib.get_prometheus_targets()
            order = ['target', 'endpoint']
            form_print(mes, order)
            exit(not res)

        if args.service_list:
            res, mes = tnglib.get_services(args.service_list)
            order = ['vnf_uuid', 'vdu_uuid']
            form_print(mes, order)
            exit(not res)

        if args.metric_name:
            res, mes = tnglib.get_metric(args.metric_name)
            order = ['job', 'instance', 'value']
            form_print(mes, order)
            exit(not res)

        if args.metric_list:
            if not args.vnf_uuid:
                msg = "VNF uuid is missing " \
                      "Type tng-cli monitor -h"
                print(msg)
                exit(1)
            if not args.vdu_uuid:
                msg = "VDU uuid is missing " \
                      "Type tng-cli monitor -h"
                print(msg)
                exit(1)

            res, mes = tnglib.get_metrics(args.vnf_uuid, args.vdu_uuid)
            order = [
                'metric_name',
            ]
            form_print(mes, order)
            exit(not res)

    # services subcommand
    elif args.subparser_name == 'service':
        # services needs exactly one argument
        sel_args = [
            args.descriptor, args.instance, args.instantiate, args.terminate,
            args.scale_out, args.scale_in
        ]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "Missing arguments for subcommand service. " \
                  "Select either --descriptor, --instance, " \
                  "--instantiate or --terminate"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "To ao many rguments for subcommand service. " \
                  "Select either --descriptor, --instance, " \
                  "--instantiate or --terminate"
            print(msg)
            exit(1)

        sel_args = [args.instantiate, args.terminate, args.get]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 2:
            print("--get can't be used with --instantiate or --terminate")
            exit(1)

        if args.descriptor and bool(args.get):
            res, mes = tnglib.get_service_descriptor(args.get)
            form_print(mes)
            exit(not res)

        if args.descriptor:
            res, mes = tnglib.get_service_descriptors()
            order = ['descriptor_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.instance and bool(args.get):
            res, mes = tnglib.get_service_instance(args.get)
            form_print(mes)
            exit(not res)

        if args.instance:
            res, mes = tnglib.get_service_instances()
            order = ['instance_uuid', 'name', 'status', 'created_at']
            form_print(mes, order)
            exit(not res)

        if bool(args.instantiate):
            if bool(args.sla):
                res, mes = tnglib.service_instantiate(args.instantiate,
                                                      args.sla)
            else:
                res, mes = tnglib.service_instantiate(args.instantiate)

            form_print(mes)
            exit(not res)

        if bool(args.terminate):
            res, mes = tnglib.service_terminate(args.terminate)
            form_print(mes)
            exit(not res)

        if bool(args.scale_out):
            if not bool(args.vnfd_uuid):
                print(" --vnfd_uuid is needed with --scale_out")
                exit(1)

            res, mes = tnglib.service_scale_out(args.scale_out, args.vnfd_uuid,
                                                args.num_instances,
                                                args.vim_uuid)
            form_print(mes)
            exit(not res)

        if bool(args.scale_in):
            if not (bool(args.vnfd_uuid) or bool(args.vnf_uuid)):
                msg = " --either --vnfd_uuid or --vnf_uuid is " \
                      "needed with --scale_in"
                print(msg)
                exit(1)

            res, mes = tnglib.service_scale_in(args.scale_in, args.vnf_uuid,
                                               args.vnfd_uuid,
                                               args.num_instances)
            form_print(mes)
            exit(not res)

    # functions subcommand
    elif args.subparser_name == 'function':
        # functions needs exactly one argument
        arg_sum = args.descriptor + args.instance
        if arg_sum == 0:
            msg = "Missing arguments for subcommand function. " \
                  "Select either --descriptor or --instance"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Too many arguments for subcommand function. " \
                  "Select either --descriptor or --instance"
            print(msg)
            exit(1)

        if args.descriptor and bool(args.get):
            res, mes = tnglib.get_function_descriptor(args.get)
            form_print(mes)
            exit(not res)

        if args.descriptor:
            res, mes = tnglib.get_function_descriptors()
            order = ['descriptor_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.instance and bool(args.get):
            res, mes = tnglib.get_function_instance(args.get)
            form_print(mes)
            exit(not res)

        if args.instance:
            res, mes = tnglib.get_function_instances()
            order = ['instance_uuid', 'status', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

    # sla subcommand
    elif args.subparser_name == 'sla':
        # template, agreement or violation needs to be specified
        sel_args = [
            args.template, args.agreement, args.violation, args.guarantee
        ]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "One of --template, --agreement, --violation, " \
                  " --guarantee must be specified with sla subcommand."
            print(msg)
            exit(1)

        elif arg_sum > 1:
            msg = "Only one of --template, --agreement, --violation, " \
                  " --guarantee must be specified with sla subcommand."
            print(msg)
            exit(1)

        if args.guarantee:
            res, mes = tnglib.get_sla_guarantees()
            order = ['name', 'id', 'operator', 'value']
            form_print(mes, order)
            exit(not res)

        elif args.template:
            # agreement and violation specific arguments are not allowed

            # If no argument is provided, list all templates
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]

            arg_sum = len([x for x in sel_args if x])
            if arg_sum == 0:
                res, mes = tnglib.get_sla_templates()
                order = ['sla_uuid', 'name', 'service', 'created_at']
                form_print(mes, order)
                exit(not res)

            arg_sum = bool(args.create) + bool(args.remove) + bool(args.get)
            # If one of --create, --remove or --get is provided,
            # perform the action
            if arg_sum == 1:
                if bool(args.get):
                    res, mes = tnglib.get_sla_template(args.get)
                    form_print(mes)
                    exit(not res)

                if bool(args.create):
                    if not (bool(args.nsd)):
                        msg = " --service is required " \
                              "with --template --create <NAME>"
                        print(msg)
                        exit(1)
                    else:
                        date = '01/01/2025'
                        if bool(args.date):
                            date = args.date
                        guarantee_id = None
                        if bool(args.guarantee_id):
                            guarantee_id = args.guarantee_id
                        sl_type = 'public'
                        if bool(args.sl_type):
                            sl_type = args.sl_type
                        as_instances = '100'
                        if bool(args.as_instances):
                            as_instances = args.as_instances
                        sl_date = '01/01/2025'
                        if bool(args.sl_date):
                            sl_date = args.sl_date
                        initiator = 'admin'
                        if bool(args.initiator):
                            initiator = args.initiator
                        provider = 'default'
                        if bool(args.provider):
                            provider = args.provider
                        flavor = None
                        if bool(args.flavor):
                            flavor = args.flavor

                        res, mes = tnglib.create_sla_template(
                            args.create, args.nsd, date, guarantee_id, sl_type,
                            as_instances, sl_date, initiator, provider, flavor)
                        print(mes)
                        exit(not res)

                if bool(args.remove):
                    res, mes = tnglib.delete_sla_template(args.remove)
                    if res:
                        print(args.remove)
                    else:
                        print(mes)
                    exit(not res)

            # Error in any other case
            else:
                pass

        elif args.agreement:
            # template specific arguments are not allowed
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]
            arg_sum = len([x for x in sel_args if x])
            if arg_sum > 0:
                print("Only --nsi and --sla allowed with --agreement")
                exit(1)

            if bool(args.nsi) and bool(args.sla):
                res, mes = tnglib.get_detailed_agreement(args.sla, args.nsi)
                form_print(mes)
                exit(not res)

            elif bool(args.nsi):
                res, mes = tnglib.get_agreements(args.nsi)
                order = ['sla_uuid', 'nsi_uuid', 'sla_status']
                form_print(mes, order)
                exit(not res)

            elif bool(args.sla):
                print('--sla requires --nsi when used with --agreement')
                exit(1)

            else:
                res, mes = tnglib.get_agreements()
                order = [
                    'sla_name', 'sla_uuid', 'nsi_uuid', 'ns_name', 'sla_status'
                ]
                form_print(mes, order)
                exit(not res)

        elif args.violation:
            # template specific arguments are not allowed
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]
            arg_sum = len([x for x in sel_args if x])
            if arg_sum > 0:
                print("Only --nsi and --sla allowed with --violation")
                exit(1)

            if bool(args.nsi) and bool(args.sla):
                res, mes = tnglib.get_violations_per_nsi_sla(
                    args.sla, args.nsi)
                form_print(mes)
                exit(not res)

            elif bool(args.nsi):
                res, mes = tnglib.get_violations(args.nsi)
                order = [
                    'sla_uuid', 'nsi_uuid', 'violation_time', 'alert_state'
                ]
                form_print(mes, order)
                exit(not res)

            elif bool(args.sla):
                print('--sla requires --nsi when used with --violation')
                exit(1)

            else:
                res, mes = tnglib.get_violations()
                order = [
                    'sla_uuid', 'nsi_uuid', 'violation_time', 'alert_state'
                ]
                form_print(mes, order)
                exit(not res)

    elif args.subparser_name == 'slice':
        # Only one of create, remove, instantiate,
        # terminate, templates, instances can be active
        sel_args = [
            args.create, args.remove, args.instantiate, args.terminate,
            args.template, args.instance
        ]
        arg_sum = len([x for x in sel_args if x])

        if arg_sum == 0:
            msg = "One of --create, --remove, --terminate, --instantiate, " \
                  "--template or --instance needed with slice subcommand."
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Only one of --create, --remove, --terminate, " \
                  "--instantiate, --template or --instance needed " \
                  "with slice subcommand."
            print(msg)
            exit(1)

        if args.name and not args.instantiate:
            print("--name can only be combined with --instantiate.")
            exit(1)

        if args.description and not args.instantiate:
            print("--description can only be combined with --instantiate.")
            exit(1)

        order = None
        if args.template and not args.get:
            res, mes = tnglib.get_slice_templates()
            order = ['slice_uuid', 'name', 'version', 'created_at']

        elif args.template and args.get:
            res, mes = tnglib.get_slice_template(args.get)

        elif args.instance and not args.get:
            res, mes = tnglib.get_slice_instances()
            order = ['instance_uuid', 'name', 'template_uuid', 'created_at']

        elif args.instance and args.get:
            res, mes = tnglib.get_slice_instance(args.get)

        elif args.remove:
            res, mes = tnglib.delete_slice_template(args.remove)

        elif args.create:
            res, mes = tnglib.create_slice_template(args.create)

        elif args.instantiate:
            res, mes = tnglib.slice_instantiate(args.instantiate, args.name,
                                                args.description)

        elif args.terminate:
            res, mes = tnglib.slice_terminate(args.terminate)

        form_print(mes, order)
        exit(not res)

    elif args.subparser_name == 'policy':
        # --service and --sla can only appear with --attach
        if (args.service or args.sla) and not args.attach:
            print("--service and --sla can only be combined with --attach")
            exit(1)

        # Only one of --get, --create, --remove or --attach can be selected
        sel_args = [args.get, args.create, args.remove, args.attach]
        arg_sum = len([x for x in sel_args if x])

        if arg_sum > 1:
            msg = "Only one of --get, --create, --remove or " \
                  "--attach can be selected"
            print(msg)
            exit(1)

        if arg_sum == 0:
            res, mes = tnglib.get_policies()
            order = ['policy_uuid', 'name', 'service', 'created_at']
            form_print(mes, order)
            exit(not res)

        if bool(args.get):
            res, mes = tnglib.get_policy(args.get)
            form_print(mes)
            exit(not res)

        if bool(args.remove):
            res, mes = tnglib.delete_policy(args.remove)
            form_print(mes)
            exit(not res)

        if bool(args.create):
            res, mes = tnglib.create_policy(args.create)
            form_print(mes)
            exit(not res)

        if bool(args.attach):
            if not (bool(args.service) and bool(args.sla)):
                print("--attach requires both --service and --sla.")
            else:
                res, mes = tnglib.attach_policy(args.attach, args.service,
                                                args.sla)
                form_print(mes)
                exit(not res)

    # results subcommand
    elif args.subparser_name == 'result':

        if bool(args.get):
            res, mes = tnglib.get_test_result(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_results()
            order = [
                'uuid',
                'instance_uuid',
                'package_id',
                'service_uuid',
                'test_uuid',
                #'test_instance_uuid',
                'status',
                'created_at'
            ]
            form_print(mes, order)
            exit(not res)

    # plans subcommand
    elif args.subparser_name == 'plan':

        if bool(args.get):
            res, mes = tnglib.get_test_plan(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_plans()
            order = [
                'uuid', 'service_uuid', 'test_uuid', 'test_set_uuid', 'status',
                'test_result_uuid'
            ]
            form_print(mes, order)
            exit(not res)

    # tests subcommand
    elif args.subparser_name == 'test':

        if bool(args.get):
            res, mes = tnglib.get_test_descriptor(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_descriptors()
            order = [
                'uuid', 'name', 'vendor', 'version', 'platforms', 'status',
                'updated_at'
            ]
            form_print(mes, order)
            exit(not res)

    elif args.subparser_name:
        print("Subcommand " + args.subparser_name + " not support yet")
        exit(0)

    return
Ejemplo n.º 3
0
def instantiation(time_start, sp_path, _exit):
    # obtain package path/name
    pkg_loc = sys.argv[1]

    # clean all packages
    tnglib.remove_all_packages()

    # upload package
    upl_package = tnglib.upload_package(pkg_loc)

    # Evaluate upload
    if not upl_package[0]:
        print("Package upload failed")
        print(upl_package[1])
        _exit += 1

    print("Package upload succeeded: " + upl_package[1])

    # Obtain service uuid
    obt_serv_uuid = tnglib.map_package_on_service(upl_package[1])

    # Evaluate obtaining serv_uuid
    if not obt_serv_uuid[0]:
        print("Couldn't obtain service uuid")
        print(obt_serv_uuid[1])
        time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        get_logging(time_start,
                    time_finish,
                    query="source:{} AND type:E".format(sp_path))
        _exit += 1

    print("Service uuid obtained: " + obt_serv_uuid[1])

    time.sleep(2)

    # Instantiate the service
    req_inst = tnglib.service_instantiate(obt_serv_uuid[1])

    # Evaluate instantiation request
    if not req_inst[0]:
        print("Couldn't obtain service uuid")
        print(req_inst[1])
        time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        get_logging(time_start,
                    time_finish,
                    query="source:{} AND type:E".format(sp_path))
        _exit += 1

    print("Instantiation request made")

    # Waiting for instantiation to finish
    counter = 0
    while counter < 60:
        time.sleep(10)
        request = tnglib.get_request(req_inst[1])
        print("Waiting for instantiation, current status: " +
              request[1]['status'])

        if request[1]['status'] == 'READY':
            print("instantiation finished successfully")
            break

        if request[1]['status'] == 'ERROR':
            print("instantiation finished in error mode")
            time_finish = datetime.utcnow().strftime(
                '%Y-%m-%d %H:%M:%S.%f')[:-3]
            get_logging(time_start,
                        time_finish,
                        query="source:{} AND type:E".format(sp_path))
            _exit += 1
        counter += 1

    if counter == 60:
        print("instantiation took longer than 10 minutes, aborting")
        time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        get_logging(time_start,
                    time_finish,
                    query="source:{} AND type:E".format(sp_path))
        _exit += 1

    service_instance_uuid = request[1]['instance_uuid']
    return service_instance_uuid
Ejemplo n.º 4
0
def dispatch(args):
    """
    post process the arguments and link them to specific actions
    """

    # Handle the verbose argument
    init_logger(args.verbose)

    # abort if no subcommand is provided
    if args.subparser_name is None:
        print("Missing subcommand. Type tng-cli -h")
        exit(1)

    # Handle --url argument and set environment
    if args.sp_url:
        tnglib.set_sp_path(args.sp_url)
    else:
        if 'SP_PATH' in os.environ:
            tnglib.set_sp_path(os.environ["SP_PATH"])
        else:
            print("Missing path to SP. Use env SP_PATH or tng-cli -u")
            exit(1)

    # Set timeout
    if 'TIMEOUT' in os.environ:
        tnglib.set_timeout(os.environ["TIMEOUT"])

    # Check if the SP is reachable
    if not tnglib.sp_health_check():
        print("Couldn't reach SP at \"" + tnglib.get_sp_path() + "\"")
        exit(1)

    # packages subcommand
    if args.subparser_name == 'package':
        # packages needs exactly one argument
        sel_args = [args.list, args.clean, args.upload, args.remove, args.get]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "Missing arguments for tng-cli package. " \
                  "Type tng-cli package -h"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Too many arguments for subcommand package. " \
                  "ype tng-cli package -h"
            print(msg)
            exit(1)

        if args.list:
            res, mes = tnglib.get_packages()
            order = ['package_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.upload:
            # Check if argument is a file
            if not args.upload.endswith('.tgo'):
                print("File or url does not point towards 5GTANTGO package.")
                exit(1)
            elif args.upload[:4] == 'http':
                res, mes = tnglib.upload_package(args.upload, url=True)
            elif not os.path.exists(args.upload):
                print("Input not a known file or url.")
                exit(1)
            else:
                res, mes = tnglib.upload_package(args.upload)
                print(mes)
                print(res)
                exit(not res)

        if args.remove:
            res, mes = tnglib.remove_package(args.remove)
            print(mes)
            exit(not res)

        if args.clean:
            res, mes = tnglib.remove_all_packages()
            for pkg in mes:
                print(pkg[1])
            exit(not res)

        if args.get:
            res, mes = tnglib.get_package(args.get)
            form_print(mes)
            exit(not res)

    # requests subcommand
    elif args.subparser_name == 'request':

        if bool(args.get):
            res, mes = tnglib.get_request(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_requests()
            order = [
                'request_uuid', 'request_type', 'status', 'created_at',
                'instance_uuid'
            ]
            form_print(mes, order)
            exit(not res)

    # services subcommand
    elif args.subparser_name == 'service':
        # services needs exactly one argument
        sel_args = [
            args.descriptor, args.instance, args.instantiate, args.terminate
        ]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "Missing arguments for subcommand service. " \
                  "Select either --descriptor, --instance, " \
                  "--instantiate or --terminate"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "To ao many rguments for subcommand service. " \
                  "Select either --descriptor, --instance, " \
                  "--instantiate or --terminate"
            print(msg)
            exit(1)

        sel_args = [args.instantiate, args.terminate, args.get]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 2:
            print("--get can't be used with --instantiate or --terminate")
            exit(1)

        if args.descriptor and bool(args.get):
            res, mes = tnglib.get_service_descriptor(args.get)
            form_print(mes)
            exit(not res)

        if args.descriptor:
            res, mes = tnglib.get_service_descriptors()
            order = ['descriptor_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.instance and bool(args.get):
            res, mes = tnglib.get_service_instance(args.get)
            form_print(mes)
            exit(not res)

        if args.instance:
            res, mes = tnglib.get_service_instances()
            order = ['instance_uuid', 'name', 'status', 'created_at']
            form_print(mes, order)
            exit(not res)

        if bool(args.instantiate):
            if bool(args.sla):
                res, mes = tnglib.service_instantiate(args.instantiate,
                                                      args.sla)
            else:
                res, mes = tnglib.service_instantiate(args.instantiate)

            form_print(mes)
            exit(not res)

        if bool(args.terminate):
            res, mes = tnglib.service_terminate(args.terminate)
            form_print(mes)
            exit(not res)

    # functions subcommand
    elif args.subparser_name == 'function':
        # functions needs exactly one argument
        arg_sum = args.descriptor + args.instance
        if arg_sum == 0:
            msg = "Missing arguments for subcommand function. " \
                  "Select either --descriptor or --instance"
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Too many arguments for subcommand function. " \
                  "Select either --descriptor or --instance"
            print(msg)
            exit(1)

        if args.descriptor and bool(args.get):
            res, mes = tnglib.get_function_descriptor(args.get)
            form_print(mes)
            exit(not res)

        if args.descriptor:
            res, mes = tnglib.get_function_descriptors()
            order = ['descriptor_uuid', 'name', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

        if args.instance and bool(args.get):
            res, mes = tnglib.get_function_instance(args.get)
            form_print(mes)
            exit(not res)

        if args.instance:
            res, mes = tnglib.get_function_instances()
            order = ['instance_uuid', 'status', 'version', 'created_at']
            form_print(mes, order)
            exit(not res)

    # sla subcommand
    elif args.subparser_name == 'sla':
        # template, agreement or violation needs to be specified
        sel_args = [
            args.template, args.agreement, args.violation, args.guarantee
        ]
        arg_sum = len([x for x in sel_args if x])
        if arg_sum == 0:
            msg = "One of --template, --agreement, --violation, " \
                  " --guarantee must be specified with sla subcommand."
            print(msg)
            exit(1)

        elif arg_sum > 1:
            msg = "Only one of --template, --agreement, --violation, " \
                  " --guarantee must be specified with sla subcommand."
            print(msg)
            exit(1)

        if args.guarantee:
            res, mes = tnglib.get_sla_guarantees()
            order = ['name', 'id', 'operator', 'value']
            form_print(mes, order)
            exit(not res)

        elif args.template:
            # agreement and violation specific arguments are not allowed

            # If no argument is provided, list all templates
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]
            arg_sum = len([x for x in sel_args if x])
            if arg_sum == 0:
                res, mes = tnglib.get_sla_templates()
                order = ['sla_uuid', 'name', 'service', 'created_at']
                form_print(mes, order)
                exit(not res)

            arg_sum = bool(args.create) + bool(args.remove) + bool(args.get)
            # If one of --create, --remove or --get is provided,
            # perform the action
            if arg_sum == 1:
                if bool(args.get):
                    res, mes = tnglib.get_sla_template(args.get)
                    form_print(mes)
                    exit(not res)

                if bool(args.create):
                    if not (bool(args.nsd) and bool(args.guarantee_id)):
                        msg = "Both --service and --guarantee are required " \
                              "with --template --create <NAME>"
                        print(msg)
                        exit(1)
                    else:
                        date = '01/01/2025'
                        if bool(args.date):
                            date = args.date
                        guarantee = args.guarantee_id
                        res, mes = tnglib.create_sla_template(
                            args.create, args.nsd, date, guarantee)
                        print(mes)
                        exit(not res)

                if bool(args.remove):
                    res, mes = tnglib.delete_sla_template(args.remove)
                    if res:
                        print(args.remove)
                    else:
                        print(mes)
                    exit(not res)

            # Error in any other case
            else:
                pass

        elif args.agreement:
            # template specific arguments are not allowed
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]
            arg_sum = len([x for x in sel_args if x])
            if arg_sum > 0:
                print("Only --nsi and --sla allowed with --agreement")
                exit(1)

            if bool(args.nsi) and bool(args.sla):
                res, mes = tnglib.get_detailed_agreement(args.sla, args.nsi)
                form_print(mes)
                exit(not res)

            elif bool(args.nsi):
                res, mes = tnglib.get_agreements(args.nsi)
                order = ['sla_uuid', 'nsi_uuid', 'sla_status']
                form_print(mes, order)
                exit(not res)

            elif bool(args.sla):
                print('--sla requires --nsi when used with --agreement')
                exit(1)

            else:
                res, mes = tnglib.get_agreements()
                order = [
                    'sla_name', 'sla_uuid', 'nsi_uuid', 'ns_name', 'sla_status'
                ]
                form_print(mes, order)
                exit(not res)

        elif args.violation:
            # template specific arguments are not allowed
            sel_args = [
                args.create, args.remove, args.get, args.nsd,
                args.guarantee_id, args.date
            ]
            arg_sum = len([x for x in sel_args if x])
            if arg_sum > 0:
                print("Only --nsi and --sla allowed with --violation")
                exit(1)

            if bool(args.nsi) and bool(args.sla):
                res, mes = tnglib.get_violations_per_nsi_sla(
                    args.sla, args.nsi)
                form_print(mes)
                exit(not res)

            elif bool(args.nsi):
                res, mes = tnglib.get_violations(args.nsi)
                order = [
                    'sla_uuid', 'nsi_uuid', 'violation_time', 'alert_state'
                ]
                form_print(mes, order)
                exit(not res)

            elif bool(args.sla):
                print('--sla requires --nsi when used with --violation')
                exit(1)

            else:
                res, mes = tnglib.get_violations()
                order = [
                    'sla_uuid', 'nsi_uuid', 'violation_time', 'alert_state'
                ]
                form_print(mes, order)
                exit(not res)

    elif args.subparser_name == 'slice':
        # Only one of create, remove, instantiate,
        # terminate, templates, instances can be active
        sel_args = [
            args.create, args.remove, args.instantiate, args.terminate,
            args.template, args.instance
        ]
        arg_sum = len([x for x in sel_args if x])

        if arg_sum == 0:
            msg = "One of --create, --remove, --terminate, --instantiate, " \
                  "--template or --instance needed with slice subcommand."
            print(msg)
            exit(1)

        if arg_sum > 1:
            msg = "Only one of --create, --remove, --terminate, " \
                  "--instantiate, --template or --instance needed " \
                  "with slice subcommand."
            print(msg)
            exit(1)

        if args.name and not args.instantiate:
            print("--name can only be combined with --instantiate.")
            exit(1)

        if args.description and not args.instantiate:
            print("--description can only be combined with --instantiate.")
            exit(1)

        order = None
        if args.template and not args.get:
            res, mes = tnglib.get_slice_templates()
            order = ['slice_uuid', 'name', 'version', 'created_at']

        elif args.template and args.get:
            res, mes = tnglib.get_slice_template(args.get)

        elif args.instance and not args.get:
            res, mes = tnglib.get_slice_instances()
            order = ['instance_uuid', 'name', 'template_uuid', 'created_at']

        elif args.instance and args.get:
            res, mes = tnglib.get_slice_instance(args.get)

        elif args.remove:
            res, mes = tnglib.delete_slice_template(args.remove)

        elif args.create:
            res, mes = tnglib.create_slice_template(args.create)

        elif args.instantiate:
            res, mes = tnglib.slice_instantiate(args.instantiate, args.name,
                                                args.description)

        elif args.terminate:
            res, mes = tnglib.slice_terminate(args.terminate)

        form_print(mes, order)
        exit(not res)

    elif args.subparser_name == 'policy':
        # --service and --sla can only appear with --attach
        if (args.service or args.sla) and not args.attach:
            print("--service and --sla can only be combined with --attach")
            exit(1)

        # Only one of --get, --create, --remove or --attach can be selected
        sel_args = [args.get, args.create, args.remove, args.attach]
        arg_sum = len([x for x in sel_args if x])

        if arg_sum > 1:
            msg = "Only one of --get, --create, --remove or " \
                  "--attach can be selected"
            print(msg)
            exit(1)

        if arg_sum == 0:
            res, mes = tnglib.get_policies()
            order = ['policy_uuid', 'name', 'service', 'created_at']
            form_print(mes, order)
            exit(not res)

        if bool(args.get):
            res, mes = tnglib.get_policy(args.get)
            form_print(mes)
            exit(not res)

        if bool(args.remove):
            res, mes = tnglib.delete_policy(args.remove)
            form_print(mes)
            exit(not res)

        if bool(args.create):
            res, mes = tnglib.create_policy(args.create)
            form_print(mes)
            exit(not res)

        if bool(args.attach):
            if not (bool(args.service) and bool(args.sla)):
                print("--attach requires both --service and --sla.")
            else:
                res, mes = tnglib.attach_policy(args.attach, args.service,
                                                args.sla)
                form_print(mes)
                exit(not res)

    # results subcommand
    elif args.subparser_name == 'result':

        if bool(args.get):
            res, mes = tnglib.get_test_result(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_results()
            order = [
                'uuid',
                'instance_uuid',
                'package_id',
                'service_uuid',
                'test_uuid',
                #'test_instance_uuid',
                'status',
                'created_at'
            ]
            form_print(mes, order)
            exit(not res)

    # plans subcommand
    elif args.subparser_name == 'plan':

        if bool(args.get):
            res, mes = tnglib.get_test_plan(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_plans()
            order = [
                'uuid', 'service_uuid', 'test_uuid', 'test_set_uuid', 'status',
                'test_result_uuid'
            ]
            form_print(mes, order)
            exit(not res)

    # tests subcommand
    elif args.subparser_name == 'test':

        if bool(args.get):
            res, mes = tnglib.get_test_descriptor(args.get)
            form_print(mes)
            exit(not res)
        else:
            res, mes = tnglib.get_test_descriptors()
            order = [
                'uuid', 'name', 'vendor', 'version', 'platforms', 'status',
                'updated_at'
            ]
            form_print(mes, order)
            exit(not res)

    elif args.subparser_name:
        print("Subcommand " + args.subparser_name + " not support yet")
        exit(0)

    return
Ejemplo n.º 5
0
if not req_inst[0]:
    print("Couldn't obtain service uuid")
    print(req_inst[1])
    time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    get_logging(time_start,
                time_finish,
                query="source:{} AND type:E".format(sp_path))
    exit(1)

print("Instantiation request made")

# Waiting for instantiation to finish
counter = 0
while counter < 60:
    time.sleep(10)
    request = tnglib.get_request(req_inst[1])
    print("Waiting for instantiation, current status: " + request[1]['status'])

    if request[1]['status'] == 'READY':
        print("instantiation finished successfully")
        break

    if request[1]['status'] == 'ERROR':
        print("instantiation finished in error mode")
        time_finish = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        get_logging(time_start,
                    time_finish,
                    query="source:{} AND type:E".format(sp_path))
        exit(1)

    counter += 1