import util_findhost
from util_connection import SSHConnection
import time
import os
import subprocess
import json

import paramiko
import re
import logging
import logging.handlers
from subprocess import call
from netmiko.ssh_exception import NetMikoTimeoutException
import re
import os.path
import sys

def exec_sys_command(cmd, cwd=""):
    if cwd != "":
	process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)
    else:
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    out, err = process.communicate()
    if out:
        return out
    if err:
        return err

def verify_dev_services(service_name):
    status = True
    sh_cmd = "openstack service list"
    #print sh_cmd
    output0 = exec_sys_command(sh_cmd)
    #print output0
    #print output0.find(service_name)
    if output0.find(service_name) != -1:
        print "Openstack service %s exists" %service_name
        cmd1 = "openstack service show %s" %service_name
        #print "cmd1: ", cmd1
        new_cmd = cmd1 + " | awk 'NR==5 {print}' | awk '{print $4}'"
        out1 = exec_sys_command(new_cmd)
        #print "out1: " + out1
        if out1 == 'True\n':
            print "Openstack service %s is ACTIVE" %service_name
        else:
            print "Openstack service %s is NOT ACTIVE" %service_name
            return False
    else:
        print "openstack service %s doesn't exist" %service_name
        return False
    return status

def create_image(image_name, image_file):
    exec_sys_command("openstack image list")
    status = True
    cmd = "openstack image create --disk-format=qcow2  --file=%s %s " % (image_file, image_name)
    logging.info("command: %s" % cmd)
    out = exec_sys_command(cmd)
    if out.find(image_name) != -1:
        logging.info("Image - %s -- successfully created" %image_name)
    else:
        status = False
    return status

def delete_image(image_name):
    status = True
    cmd = "openstack image delete " + image_name
    print "Openstack command: ", cmd
    out = exec_sys_command(cmd)
    if not out:
        print "Image - " + image_name + " -- successfully deleted"
    else:
        status = False
    return status

def create_keypairs(key_name):
    exec_sys_command("sudo rm loginkey.pem")
    exec_sys_command("openstack keypair list")
    status = True
    out = exec_sys_command('openstack keypair create %s > %s.pem' %(key_name, key_name))
    exec_sys_command("sudo chown testpc:testpc %s.pem" % key_name)
    cmd2 = "openstack keypair list"
    out = exec_sys_command(cmd2)
    print ("out: ", out)
    x = out.find(key_name)
    print ("x = ", x)
    if out.find(key_name) != -1:
        logging.info("Key Pair - %s -- successfully created" %key_name)
    else:
        status = False
    return status

def delete_keypairs(key_name):
    status = True
    cmd = "openstack keypair delete " + key_name
    print "Openstack command: ", cmd
    out = exec_sys_command(cmd)
    if not out:
        print "Key Pair - " + key_name + " -- successfully deleted"
    else:
        status = False
    return status

def create_network(network_name, subnet):
    status = True
    cmd1 = "openstack network create --share --enable " + network_name
    cmd2 = "openstack subnet create " + network_name + " --network " + network_name + " --subnet-range " + subnet
    cmd3 = "openstack subnet show " + network_name
    print "cmd1: ", cmd1
    print "cmd2: ", cmd2
    output1 = exec_sys_command(cmd1)
    if output1.find(network_name) != -1:
        exec_sys_command(cmd2)
        out = exec_sys_command(cmd3)
        if out.find(network_name) != -1:
            print "Network - " + network_name + " -- successfully created"
        else:
            status = False
    else:
        status = False
    return status

def delete_network(network_name):
    status = True
    cmd = "openstack network delete " + network_name
    out = exec_sys_command(cmd)
    if not out:
        print "network - " + network_name + " -- successfully deleted"
    else:
        status = False
    return status

def create_public_network(network_name, subnet, ip_alloc_pool_start, ip_alloc_pool_end):
    status = True
    cmd1 = "openstack network create --share --external --provider-physical-network public --provider-network-type flat " + network_name
    cmd2 = "openstack subnet create --network " + network_name + " --allocation-pool start=" + ip_alloc_pool_start + ",end=" + ip_alloc_pool_end + " --subnet-range " + subnet + " --no-dhcp public-subnet" 
    cmd3 = "openstack subnet show " + network_name
    print "cmd1: ", cmd1
    print "cmd2: ", cmd2
    output1 = exec_sys_command(cmd1)
    print output1
    if output1.find(network_name) != -1:
        exec_sys_command(cmd2)
        out = exec_sys_command(cmd3)
        print out
        if out.find(network_name) != -1:
            print "Public Network - " + network_name + " -- successfully created"
        else:
            status = False
    else:
        status = False
    return status

def create_router(router_name, public_net_name, mgmt_nw):
    status = True
    sh_cmd = "openstack router list --name " + router_name
    print sh_cmd
    output0 = exec_sys_command(sh_cmd)
    if output0.find(router_name) != -1:
        print "Router already exists"
        return False
    cmd1 = "openstack router create " + router_name
    print "cmd1: ", cmd1
    cmd2 = "openstack router add subnet " + router_name + " " + mgmt_nw
    print "cmd2: ", cmd2
    cmd3 = "openstack router set --external-gateway=" + public_net_name + " " + router_name
    print "cmd3: ", cmd3
    cmd4 = "openstack router show " + router_name
    print "cmd4: ", cmd4
    output1 = exec_sys_command(cmd1)
    if output1.find(router_name) != -1:
        exec_sys_command(cmd2)
        exec_sys_command(cmd3)
        out = exec_sys_command(cmd4)
        if out.find(router_name) != -1:
            print "Router - " + router_name + " -- successfully created"
        else:
            status = False
    else:
        status = False
    return status

def delete_router(router_name):
    status = True
    sh_cmd = "openstack router list --name " + router_name
    print sh_cmd
    output0 = exec_sys_command(sh_cmd)
    if output0.find(router_name) != -1:
        cmd01 = "openstack router delete " + router_name
        print cmd01
        exec_sys_command(cmd01)
        output01 = exec_sys_command(sh_cmd)
        print output01
        if output01.find(router_name) != -1:
            print "Existing router couldn't be deleted"
            return False
        else:
            print "Existing router is Deleted"
    return status

def create_sec_grp(sec_name):
    status = True
    sh_cmd = "openstack security group list"
    print sh_cmd
    output0 = exec_sys_command(sh_cmd)
    if output0.find(sec_name) != -1:
        print "Security Group already exists"
        return False
    cmd1 = "openstack security group create %s --description 'all ports'" %sec_name
    print "cmd1: ", cmd1
    exec_sys_command(cmd1)
    cmd2 = "openstack security group rule create --remote-group=%s --ingress --ethertype=IPv6 %s" %(sec_name, sec_name)
    print "cmd2: ", cmd2
    exec_sys_command(cmd2)
    cmd3 = "openstack security group rule create --remote-group=%s --ingress --ethertype=IPv4 %s" %(sec_name, sec_name)
    print "cmd3: ", cmd3
    exec_sys_command(cmd3)
    cmd4 = "openstack security group rule create --egress --ethertype=IPv6 %s" %sec_name
    print "cmd4: ", cmd4
    exec_sys_command(cmd4)
    cmd5 = "openstack security group rule create --egress --ethertype=IPv4 %s" %sec_name
    print "cmd5: ", cmd5
    exec_sys_command(cmd5)
    cmd6 = "openstack security group rule create --protocol=icmp --ingress %s" %sec_name
    print "cmd6: ", cmd6
    exec_sys_command(cmd6)
    cmd7 = "openstack security group rule create --protocol=icmp --egress %s" %sec_name
    print "cmd7: ", cmd7
    exec_sys_command(cmd7)
    cmd8 = "openstack security group rule create --protocol=udp --ingress %s" %sec_name
    print "cmd8: ", cmd8
    exec_sys_command(cmd8)
    cmd9 = "openstack security group rule create --protocol=udp --egress %s" %sec_name
    print "cmd9: ", cmd9
    exec_sys_command(cmd9)
    cmd10 = "openstack security group rule create --protocol=tcp --ingress %s" %sec_name
    print "cmd10: ", cmd10
    exec_sys_command(cmd10)
    cmd11 = "openstack security group rule create --protocol=tcp --ingress --dst-port=22 %s" %sec_name
    print "cmd11: ", cmd11
    exec_sys_command(cmd11)
    cmd12 = "openstack security group show %s" %sec_name
    print "cmd12: ", cmd12
    output1 = exec_sys_command(cmd12)
    if output1.find(sec_name) != -1:
        print "Security Group - " + sec_name + " -- successfully created"
    else:
        status = False
    return status

def delete_sec_grp(sec_name):
    status = True
    sh_cmd = "openstack security group list"
    print sh_cmd
    output0 = exec_sys_command(sh_cmd)
    if output0.find(sec_name) != -1:
        try:
            cmd1 = "openstack security group delete " + sec_name
            exec_sys_command(cmd1)
            output1 = exec_sys_command(sh_cmd)
            if output1.find(sec_name) != -1:
                print "Security Group couldn't be Deleted"
                return False
            else:
                print "Security Group is Deleted"
        except:
            print "Security Group couldn't be Deleted"
            return False
    else:
        print "Security Group Doesn't exist"
        return False
    return status

def create_stack(stack_name, yaml_path, heat_tmplt, mgmt_nw, image, flavor, key, sec_name, network_id = None, network_id2 = None):
    ost_cmd = 'openstack stack create -t %s%s %s ' % (yaml_path, heat_tmplt, stack_name)
    if mgmt_nw or image or flavor or key:
        ost_cmd = ost_cmd + ' --parameter "'
        if mgmt_nw:
            ost_cmd = ost_cmd + 'NetID=%s;' % mgmt_nw
        if network_id:
            ost_cmd = ost_cmd + 'NetID2=%s;' % network_id
        if network_id2:
            ost_cmd = ost_cmd + 'NetID3=%s;' % network_id2
        if image:
            ost_cmd = ost_cmd + 'IMAGE=%s;' % image
        if flavor:
            ost_cmd = ost_cmd + 'FLAVOR=%s;' % flavor
        if key:
            ost_cmd = ost_cmd + 'KEY=%s;' % key
        ost_cmd = ost_cmd[:-1] + '"'

    logging.info("create vm command: %s" % ost_cmd)
    output = exec_sys_command(ost_cmd)
    time.sleep(20)
    #print output.find(stack_name)
    if output.find(stack_name) == -1:
        return False
    else:
        if attach_sec_gr(stack_name, sec_name):
            return True
        else:
            return False    

def upgrade_stack(stack_name, yaml_path, heat_tmplt, image, flavor):
    ost_cmd = 'openstack stack update -t %s%s %s ' % (yaml_path, heat_tmplt, stack_name)
    if image or flavor:
        ost_cmd = ost_cmd + ' --parameter "'
        if image != None:
            ost_cmd = ost_cmd + 'IMAGE=%s;' % image
        if flavor:
            ost_cmd = ost_cmd + 'FLAVOR=%s;' % flavor
        ost_cmd = ost_cmd[:-1] + '"'

    logging.info("Upgrade vm command: %s" % ost_cmd)
    output = exec_sys_command(ost_cmd)
    return False if output.find(stack_name) == -1 else True

def downgrade_stack(stack_name, yaml_path, heat_tmplt, image, flavor):
    ost_cmd = 'openstack stack update -t %s%s %s ' % (yaml_path, heat_tmplt, stack_name)
    if image or flavor:
        ost_cmd = ost_cmd + ' --parameter "'
        if image != None:
            ost_cmd = ost_cmd + 'IMAGE=%s;' % image
        if flavor:
            ost_cmd = ost_cmd + 'FLAVOR=%s;' % flavor
        ost_cmd = ost_cmd[:-1] + '"'

    logging.info("Downgrade vm command: %s" % ost_cmd)
    output = exec_sys_command(ost_cmd)
    return False if output.find(stack_name) == -1 else True

def parse_json(cmd, stack_name, key=None):
    if cmd == 'stackShow':
        ost_cmd = "openstack stack show " + stack_name + " -f json"
    if cmd == 'serverShow':
        ost_cmd = "openstack server show " + stack_name + " -f json"
    if cmd == 'imageShow':
        ost_cmd = "openstack image show " + stack_name + " -f json"
    print "show_stack_command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    if key == "noStack":
        return output
    data = json.loads(output)
    if key == "server_name":
        return data["outputs"][0]["output_value"]
    elif key != None:
        return data[key]
    else:
        return data
	    
def delete_stack(stack_name):
    ost_cmd = "openstack stack delete --yes " + stack_name
    print "Delete stack command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output 

def delete_server(server_name):
    ost_cmd = "openstack server delete --wait " + server_name
    print "Delete server command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output

def reboot_server(server_name, boot_type='soft'):
    if boot_type == 'soft':
        ost_cmd = "openstack server reboot --soft --wait " + server_name
    elif boot_type == 'hard':
        ost_cmd = "openstack server reboot --hard --wait " + server_name
    else:
        print "Invalid boot type"
        return False
    output = exec_sys_command(ost_cmd)
    return output

def create_snapshot(server_name, snapshot_name):
    ost_cmd = "openstack server list | grep %s" % server_name
    output = exec_sys_command(ost_cmd)
    exact_server_name = output.split('|')[2].split()[0]
    ost_cmd = "openstack server image create %s --name %s" %(exact_server_name, snapshot_name)
    print "Create snapshot command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    #time.sleep(180)
    time.sleep(10)
    return False if output.find(snapshot_name) == -1 else True

def delete_snapshot(snapshot_name):
    ost_cmd = "openstack image delete " + snapshot_name
    print "Revert snapshot command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output 

def pause_server(server_name):
    ost_cmd = "openstack server pause " + server_name
    print "Server pause command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output

def unpause_server(server_name):
    ost_cmd = "openstack server unpause " + server_name
    print "Server unpause command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output

def suspend_server(server_name):
    ost_cmd = "openstack server suspend " + server_name
    print "Server suspend command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output 

def resume_server(server_name):
    ost_cmd = "openstack server resume " + server_name
    print "Server resume command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return output

def migrate_server(server_name):
    ost_cmd = "openstack server migrate --shared-migration --wait " + server_name
    print "Server migrate command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return False if output.find(server_name) == -1 else True

def live_migrate_server(server_name):
    ost_cmd = "openstack server migrate --live cirros32 --wait " + server_name
    print "Server migrate command: ", ost_cmd
    output = exec_sys_command(ost_cmd)
    return False if output.find(server_name) == -1 else True

def get_float_ip(multi_nw_name):
    ost_cmd = "openstack server list | grep %s-server" % multi_nw_name
    print "Command: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    try:
        float_ip = output.split('|')[4].split(',')[1].split(';')[0].split()[0]
        print "Floatting IP: %s " % float_ip
        return float_ip
    except:
        print "Please check stack creation. No floating IP found"
        return False

def get_multi_float_ip(multi_stack_name):
    float_ip = []
    ost_cmd = "openstack server list | grep %s-server1" % multi_stack_name
    print "Command1: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        print "Floatting IP: %s " % float_ip[0]
    except:
        print "Please check stack creation. No floating IP found"
        return False

    ost_cmd = "openstack server list | grep %s-server2" % multi_stack_name
    print "Command2: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)

    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        print "Floatting IP: %s " % float_ip[1]
        return float_ip
    except:
        print "Please check stack creation. one or more floating IP NOT found"
        return False

def get_multi_ip_functional(multi_stack_name):
    float_ip = []
    net_mngt = []
    pvt_ip = []
    ost_cmd = "openstack server list | grep %s-server1" % multi_stack_name
    #print "Command1: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        net_mngt.append(output.split('|')[4].split(',')[0].split(';')[0].split()[0].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[1].split('=')[1].split(' ')[0])
        print "Floatting IP: %s " % float_ip[0]
    except:
        print "Please check stack creation. No floating IP found"
        return False

    ost_cmd = "openstack server list | grep %s-server2" % multi_stack_name
    #print "Command2: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)

    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        net_mngt.append(output.split('|')[4].split(',')[0].split(';')[0].split()[0].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[1].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[2].split('=')[1].split(' ')[0])
        print "Floatting IP: %s " % float_ip[1]
    except:
        print "Please check stack creation. one or more floating IP NOT found"
        return False

    ost_cmd = "openstack server list | grep %s-server3" % multi_stack_name
    #print "Command3: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)

    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        net_mngt.append(output.split('|')[4].split(',')[0].split(';')[0].split()[0].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[1].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[2].split('=')[1].split(' ')[0])
        print "Floatting IP: %s " % float_ip[2]
    except:
        print "Please check stack creation. one or more floating IP NOT found"
        return False

    ost_cmd = "openstack server list | grep %s-server4" % multi_stack_name
    #print "Command4: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)

    try:
        float_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])
        net_mngt.append(output.split('|')[4].split(',')[0].split(';')[0].split()[0].split('=')[1])
        pvt_ip.append(output.split('|')[4].split(';')[1].split('=')[1].split(' ')[0])
        print "Floatting IP: %s " % float_ip[3]
    except:
        print "Please check stack creation. one or more floating IP NOT found"
        return False

    #print "Floating IP: %s" % float_ip
    return float_ip, net_mngt, pvt_ip

def validate_ip_vm_multinw(multi_nw_name, usrname, key, password):
    ip_float = get_float_ip(multi_nw_name)
    if ip_float:
        print "key: ", key
        k = paramiko.RSAKey.from_private_key_file(key)
        H = paramiko.SSHClient()
        H.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        for n in range(20):
            try:
                try:
                    H.connect(hostname = ip_float, username = usrname, pkey = k)
                except:
                    H.connect(hostname = ip_float, username = usrname, password=password)
                print "SSH Connection Successful"
                return True
            except:
                print "SSH Connection Failed"
                print "Sleep for 20 seconds"
                time.sleep(20)
                if n <= 20:
                    continue
                else:
                    print "IP verification Failed"
                    return False

def attach_sec_gr(stack_name, sec_name):
    status = True
    cmd1 = "openstack server list | awk '{print $4}' | grep " + stack_name
    out = exec_sys_command(cmd1)
    print out
    out = out.split('\n')
    for server in out:
        if server != '':
            print "Server Name: " + server
            cmd2 = "openstack server add security group %s %s" %(server, sec_name)
            try:
                exec_sys_command(cmd2)
                print "Security Group %s is attached to %s Server PASSED" %(sec_name, server)
            except:
                print "Security Group %s is attached to %s Server FAILED" %(sec_name, server)
                return False
    return status

def validate_ip_vm_multistack(multi_stack_name, usrname, key, password):
    login_ip = []
    print "Sleep for 30 sec"
    time.sleep(30)
    ost_cmd = "openstack server list | grep %s-server1" % multi_stack_name
    #print "Command1: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    exact_server_name1 = output.split('|')[2].split()[0]
    login_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])

    ost_cmd = "openstack server list | grep %s-server2" % multi_stack_name
    #print "Command2: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    exact_server_name2 = output.split('|')[2].split()[0]
    login_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])

    exact_server_name = [exact_server_name1, exact_server_name2]
    #print "Login IP: %s" %login_ip
    print "Sleep for 60 sec"
    time.sleep(60)

    k = paramiko.RSAKey.from_private_key_file(key)
    H = paramiko.SSHClient()
    H.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    for idx, server_name in enumerate(exact_server_name):
        ip_float = login_ip[idx]
        print "IP Float: %s " % ip_float
        for n in range(20):
            try:
                try:
                    H.connect(hostname = ip_float, username = usrname, password=password)
                except:
                    H.connect(hostname = ip_float, username = usrname, pkey = k)
                print "SSH Connection Successful for ", ip_float
                break
            except:
                print "SSH Connection Failed"
                print "Sleep for 20 seconds"
                time.sleep(20)
                if n <= 20:
                    continue
                else:
                    print "IP verification Failed"
                    return False
    return login_ip

def get_ip_vm_multistack(multi_stack_name, usrname, key, password):
    login_ip = []
    print "Sleep for 30 sec"
    time.sleep(30)
    ost_cmd = "openstack server list | grep %s-server1" % multi_stack_name
    #print "Command1: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    exact_server_name1 = output.split('|')[2].split()[0]
    login_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])

    ost_cmd = "openstack server list | grep %s-server2" % multi_stack_name
    #print "Command2: %s" % ost_cmd
    output = exec_sys_command(ost_cmd)
    exact_server_name2 = output.split('|')[2].split()[0]
    login_ip.append(output.split('|')[4].split(',')[1].split(';')[0].split()[0])

    exact_server_name = [exact_server_name1, exact_server_name2]
    print "Login IP: %s" %login_ip

    return login_ip

def run_check_cmd(handle, cmd, pattern):
    handle.execute_command(cmd)
    if cmd.find("ping") == -1:
        time.sleep(15)
    else:
        time.sleep(2)
    if handle.conn.rc.before.find(pattern) != -1:
        return True
    else:
        return  False

def intf_ip(stack_name):
    ost_cmd5 = "openstack server show " + stack_name + " --format json | grep addresses"
    print "show_server_command for intf ip: ", ost_cmd5
    output = exec_sys_command(ost_cmd5).strip()
    ipset = util_findhost.get_ip_set(output)
    return ipset

def run_nt_check_cmd(handle, cmd, pattern):
    handle.execute_command(cmd)
    if cmd.find("ping") == -1:
        time.sleep(15)
    else:
        time.sleep(2)
    if handle.conn.rc.before.find(pattern) == -1:
        return True
    else:
        return False

def setup_vnf_vm(handle):
    handle.execute_command("sudo chmod 777 /etc/resolv.conf")
    handle.execute_command("sudo echo 'nameserver 8.8.8.8' >> /etc/resolv.conf")
    time.sleep(5)
    handle.execute_command("ping -c 3 www.google.com")
    if handle.conn.rc.before.find("0% packet loss") != -1:
        print "check internet 1: ", handle.conn.rc.before
        print("VM1 - Connected to Google.com")

def show_stack(stack_name):
    ost_cmd2 = "openstack stack show " + stack_name + " | grep 'output_value: " + stack_name + "' | awk ' { print $4 }'"
    print "show_stack_command: ", ost_cmd2
    output = exec_sys_command(ost_cmd2).strip()
    return output

def route_add(handle, cmd_rt_add):
    handle.execute_command(cmd_rt_add)

def test_complete():
    pass

def create_stack_functional(stack_name, yaml_path, heat_tmplt, sec_name, network_id = None, network_id2 = None, network_id3 = None):
    mgmt_nw = None
    flavor = None
    key = None
    image = 'ubuntu.16.04'
    ost_cmd = 'openstack stack create -t %s%s %s ' % (yaml_path, heat_tmplt, stack_name)
    if mgmt_nw or image or flavor or key:
        ost_cmd = ost_cmd + ' --parameter "'
        if mgmt_nw:
            ost_cmd = ost_cmd + 'NetID=%s;' % mgmt_nw
        if network_id:
            ost_cmd = ost_cmd + 'NetID2=%s;' % network_id
        if network_id2:
            ost_cmd = ost_cmd + 'NetID3=%s;' % network_id2
        if network_id3:
            ost_cmd = ost_cmd + 'NetID4=%s;' % network_id3
        if image:
            ost_cmd = ost_cmd + 'IMAGE=%s;' % image
        if flavor:
            ost_cmd = ost_cmd + 'FLAVOR=%s;' % flavor
        if key:
            ost_cmd = ost_cmd + 'KEY=%s;' % key
        ost_cmd = ost_cmd[:-1] + '"'

    logging.info("create vm command: %s" % ost_cmd)
    output = exec_sys_command(ost_cmd)
    time.sleep(20)
    #print output.find(stack_name)
    if output.find(stack_name) == -1:
        return False
    else:
        if attach_sec_gr(stack_name, sec_name):
            return True
        else:
            return False

