host_ip = '192.168.120.15'

    # ------------------
    # Retrieve lists:
    # ------------------
    ports = client.list_ports(host_ip)
    logging.info('Get ports for %s: %s', host_ip, ports)

    interfaces = client.list_interfaces(host_ip)
    logging.info('Get interfaces for %s: %s', host_ip, interfaces)

    # ------------------
    # Test setting QoS:
    # ------------------
    host_ip = '192.168.120.15'
    ovs_port = 'qvo641e9d9d-a5'
    min_rate = 100000
    max_rate = 1000000

    queue = client.create_queue(host_ip, min_rate, max_rate)
    logging.info(
        'Create queue on ovs port %s, with min-rate: %d, max-rate: %d = %s',
        ovs_port, min_rate, max_rate, queue)

    qos = client.create_qos(host_ip, queue)
    logging.info('Create QoS for queue: %s = %s', queue, qos)

    setport = client.set_port(host_ip, ovs_port, 'qos', qos)
    logging.info('Set QoS: %s on port: %s ! Result = %s', qos, ovs_port,
                 setport)
class Host(object):
    def __init__(self, hypervisor):
        self.hypervisor = hypervisor
        self.ovsclient = OVSClient()
        self.cloud = Cloud()

    def list_interfaces_hypervisor(self, hypervisors):
        interfaces = {}
        for k, v in hypervisors.items():
            if k == self.hypervisor:
                for k_inner, v_inner in v.items():
                    if k_inner == 'ip':
                        ip = v_inner
                        interfaces = self.ovsclient.list_interfaces(ip)
        logging.info('Getting OVS interfaces %s :', interfaces)
        return interfaces

    """
    Format:
    {"data":[["em1",["set",[]],["set",[]]],["vxlan-c0a8292c",["set",[]],["set",[]]],["tapab31b81d-9f",["set",[]],1],["br-int",["set",[]],["set",[]]],["br-ex",["set",[]],["set",[]]],["br-tun",["set",[]],["set",[]]],["patch-tun",["set",[]],["set",[]]],["patch-int",["set",[]],["set",[]]],["qg-f2e6bee1-e7",["set",[]],["set",[]]],["qr-23d740b1-74",["set",[]],1],["qvob3f1896b-c8",["uuid","f807a246-4064-4577-806e-76dfe623dda0"],1],["qvo365ba3c3-14",["uuid","9cd461b3-f9b6-49b9-bac4-c71c19d1d2d0"],1]],"headings":["name","qos","tag"]}
    """

    def list_ports_hypervisor(self, hypervisor_ip):
        ports = self.ovsclient.list_ports(hypervisor_ip)
        logging.info('Getting OVS ports: %s for Hypervisor IP: %s', ports,
                     hypervisor_ip)
        return ports

    def list_qos_hypervisor(self, hypervisor_ip):
        qos_raw = json.loads(self.ovsclient.list_qoss(hypervisor_ip))
        hypervisor_qos = []

        for q in qos_raw.get('data'):
            hypervisor_qos.append(q)

        logging.info('Getting final OVS QoS %s for IP %s', hypervisor_qos,
                     hypervisor_ip)
        return hypervisor_qos

    def list_port_desc_hypervisor(self, hypervisor_ip):
        port_desc_raw = self.ovsclient.list_port_desc(hypervisor_ip)
        logging.info('Getting final Port Desc %s for IP %s', port_desc_raw,
                     hypervisor_ip)
        return port_desc_raw

    def list_queues_hypervisor(self, hypervisor_ip):
        queues = []
        queues_raw = json.loads(self.ovsclient.list_queues(hypervisor_ip))
        logging.info('Queues for comparison for %s : %s ... ', hypervisor_ip,
                     queues_raw)

        for q in queues_raw.get('data'):
            queues.append(q)
        logging.info('Queues from queue list: %s', queues)
        return queues

    def get_port_qos(self, hypervisor_ip, ovs_port):
        ports_raw = json.loads(self.list_ports_hypervisor(hypervisor_ip))
        logging.info('Ports for %s: %s', hypervisor_ip, ports_raw)
        qos_id = []
        for port in ports_raw.get('data'):
            isvm = 0
            for port_key in port:
                if type(port_key) == unicode and port_key == ovs_port:
                    isvm = 1
                if type(port_key) == list and isvm:
                    isvm = 0
                    for pv in port_key:
                        if pv != 'uuid':
                            qos_id = pv
        logging.info('QoS ID for port: %s is: %s', ovs_port, qos_id)
        return qos_id

    def set_qos_vm(self, server_interface, interfaces, vm_id):
        qos_status = {}
        queues = {}
        queue_string = "queues="
        found_match = 0
        queue_cfgs = ""
        server_ips = self.cloud.get_server_ips()
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)

        for ks, vs in server_ips.items():
            if ks == vm_id:
                for ip in vs:
                    if ip == server_interface.get('ip'):
                        found_match = 1
                        neutron_port = self.cloud.get_neutron_port(ip)
                        ovs_port_id = self.cloud.get_port_id(
                            interfaces, neutron_port)[0]
                        logging.info('OVS port ID for Server %s: %s', ks,
                                     ovs_port_id)
                        qos_tmp = server_interface.get('qos')
                        for rate in qos_tmp.get('queues'):
                            internal_rate = rate.get('rates')
                            logging.info(
                                'Server %s gets Min-rate %s Max-rate %s', ks,
                                rate.get('min-rate'), rate.get('max-rate'))
                            #queues[index] = self.ovsclient.create_queue(hypervisor_ip, int(qos_rate.get('min-rate')), int(qos_rate.get('max-rate')))
                            queue_cfgs += "-- --id=@q" + rate.get('id') + " create queue other-config:min-rate=" + internal_rate.get('min-rate') +\
                                          " other-config:max-rate=" + internal_rate.get('max-rate') + " "
                            queues[rate.get('id')] = "q" + rate.get('id')
                            logging.info('Queue ID for Server %s: %s', ks,
                                         queue_cfgs)
                        for idx, qid in queues.items():
                            queue_string += "%s=@" % idx + "%s," % qid

                        logging.info('Queue String for server %s: %s%s', ks,
                                     queue_string, queue_cfgs)
                        return_ids = self.ovsclient.create_qos(
                            hypervisor_ip, queue_string, queue_cfgs)
                        logging.info('QoS ID %s', return_ids.split('\n'))
                        qos_id = return_ids.split('\n')
                        qos_status = self.ovsclient.set_port(
                            hypervisor_ip, ovs_port_id, 'qos', qos_id[0])

                        qos_tmp['qos_uuid'] = qos_id[0]

                        # logging.debug("Current qoses_ids %s",qos_id)

                        for num_id, ids in enumerate(qos_id):
                            for rate in qos_tmp.get('queues'):
                                if int(rate.get('id')) == num_id:
                                    rate['queue_uuid'] = ids

                        server_interface['qos'] = qos_tmp

                        if not qos_status:
                            qos_status = server_interface
                        logging.info('QoS status for Server %s: %s', ks,
                                     qos_status)
        if not found_match:
            return qos_status
        else:
            return qos_status

    def dell_qos(self, qos_id):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        resp = {}
        resp['queues'] = []
        queues = self.get_queues(qos_id)
        logging.debug("Queues found: %s", queues)

        logging.info('OVS destroy all queue on : %s and deleteting qos %s',
                     hypervisor_ip, qos_id)

        if queues:
            for current_queue in queues:
                queue_del = {}
                queue_del['status'] = self.del_queue(
                    current_queue.get('number'), current_queue.get('id'),
                    qos_id)
                queue_del['number'] = current_queue.get('number')

                resp['queues'].append(queue_del)

        qos_del = self.ovsclient.del_qos(hypervisor_ip, qos_id)

        resp['qos_id'] = qos_id
        if qos_del == None:
            resp['status'] = "success"
        else:
            resp['status'] = qos_del

        return resp

    def get_queues(self, qos_id):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        queue_json = self.ovsclient.list_qosandqueue(hypervisor_ip, qos_id)
        queues = json.loads(queue_json)
        logging.debug("Queues %s", queues)
        val = []

        for item in queues.get('data'):
            logging.debug("Item %s", item)
            for inner in item:
                logging.debug("Inner %s", inner)
                if type(inner) == list:
                    for li in inner:
                        logging.debug("Li %s", li)
                        if type(li) == list:
                            if not li:
                                return val

                            if len(li) > 1:
                                for lu in li:
                                    queue = {}
                                    logging.debug("LU[0] %s", lu[0])
                                    queue['number'] = lu[0]
                                    logging.debug("LU[1] %s", lu[1][1])
                                    queue['id'] = lu[1][1]
                                    val.append(queue)
                            else:
                                single_queue = {}
                                logging.debug("Li[0] %s", li[0])
                                single_queue['number'] = li[0][0]
                                logging.debug("Li[0][1][1] %s", li[0][1][1])
                                single_queue['id'] = li[0][1][1]
                                val.append(single_queue)
        return val

    def del_queue(self, queue_number, queue_id, qos_id):
        logging.info('OVS destroy queue number %s with id %s', queue_number,
                     queue_id)
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        ret = {}
        val = self.ovsclient.del_queue(hypervisor_ip, qos_id, queue_id,
                                       queue_number)

        # queues = self.get_queues(qos_id)
        # ret['qos_uuid'] = qos_id
        # ret['queues'] = []
        #
        # for queue in queues:
        #     ret_queue = {}
        #     ret_queue['queue_uuid'] = queue.get('id')
        #     ret_queue['id'] = queue.get('number')
        #     ret_queue['rates'] = self.get_queue_rates(queue.get('id'))
        #     ret['queues'].append(ret_queue)

        ret['queue_uuid'] = queue_id
        ret['queue_id'] = queue_number
        ret['qos_uuid'] = qos_id

        if val is 0:
            ret['status'] = 'success'
        else:
            ret['status'] = val

        return ret

    def set_flow_vm(self, flow_config):
        flow_status = []

        logging.info('Flow config %s', flow_config)

        for qv in flow_config:

            current_flow = {}
            logging.info('Setting flow on Queue: ip %s', qv.get("src_ipv4"))
            hypervisor_ip = self.cloud.get_hypervisor_ip(qv.get("dest_hyp"))
            current_flow = self.ovsclient.add_flow_to_queue(
                hypervisor_ip, qv.get('src_ipv4'), qv.get("dest_ipv4"),
                qv.get("protocol"), qv.get('priority'),
                qv.get('ovs_port_number'), qv.get("queue_number"))

            flow_status.append(current_flow)

        return flow_status

    def remove_flow(self, ip, protocol):

        result = {}
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        logging.debug("REMOVE FLOW hypervisor ip %s", hypervisor_ip)
        self.ovsclient.remove_flow_dest(hypervisor_ip, ip, protocol)

        current_flows = self.ovsclient.dump_flows()
        logging.debug("REMOVE FLOW current flows %s", current_flows)
        if ("nw_dst=" + ip in current_flows) == True:
            result = "failed"
        else:
            self.ovsclient.remove_flow_src(hypervisor_ip, ip, protocol)
            current_flows = self.ovsclient.dump_flows()
            if ("nw_src=" + ip in current_flows) == True:
                result = "failed"
            else:
                result = "success"

        return result

    def create_queue(self, min_rate, max_rate):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        queue_id = self.ovsclient.create_queue(hypervisor_ip, min_rate,
                                               max_rate)
        return queue_id

    def link_queue_to_qos(self, qos_id, queue_id, queue_number):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)

        ret = self.ovsclient.set_qos_ovs(hypervisor_ip, qos_id, queue_id,
                                         queue_number)
        return ret
    client = Client()

    host_ip = '192.168.120.15'

    # ------------------
    # Retrieve lists:
    # ------------------
    ports = client.list_ports(host_ip)
    logging.info('Get ports for %s: %s', host_ip, ports)

    interfaces = client.list_interfaces(host_ip)
    logging.info('Get interfaces for %s: %s', host_ip, interfaces)

    # ------------------
    # Test setting QoS:
    # ------------------
    host_ip = '192.168.120.15'
    ovs_port = 'qvo641e9d9d-a5'
    min_rate = 100000
    max_rate = 1000000

    queue = client.create_queue(host_ip, min_rate, max_rate)
    logging.info('Create queue on ovs port %s, with min-rate: %d, max-rate: %d = %s', ovs_port, min_rate, max_rate, queue)

    qos = client.create_qos(host_ip, queue)
    logging.info('Create QoS for queue: %s = %s', queue, qos)

    setport = client.set_port(host_ip, ovs_port, 'qos', qos)
    logging.info('Set QoS: %s on port: %s ! Result = %s', qos, ovs_port, setport)
class Host(object):
    def __init__(self, hypervisor):
        self.hypervisor = hypervisor
        self.ovsclient = OVSClient()
        self.cloud = Cloud()

    def list_interfaces_hypervisor(self, hypervisors):
        interfaces = {}
        for k, v in hypervisors.items():
            if k == self.hypervisor:
                for k_inner, v_inner in v.items():
                    if k_inner == 'ip':
                        ip = v_inner
                        interfaces = self.ovsclient.list_interfaces(ip)
        logging.info('Getting OVS interfaces %s :', interfaces)
        return interfaces

    """
    Format:
    {"data":[["em1",["set",[]],["set",[]]],["vxlan-c0a8292c",["set",[]],["set",[]]],["tapab31b81d-9f",["set",[]],1],["br-int",["set",[]],["set",[]]],["br-ex",["set",[]],["set",[]]],["br-tun",["set",[]],["set",[]]],["patch-tun",["set",[]],["set",[]]],["patch-int",["set",[]],["set",[]]],["qg-f2e6bee1-e7",["set",[]],["set",[]]],["qr-23d740b1-74",["set",[]],1],["qvob3f1896b-c8",["uuid","f807a246-4064-4577-806e-76dfe623dda0"],1],["qvo365ba3c3-14",["uuid","9cd461b3-f9b6-49b9-bac4-c71c19d1d2d0"],1]],"headings":["name","qos","tag"]}
    """
    def list_ports_hypervisor(self, hypervisor_ip):
        ports = self.ovsclient.list_ports(hypervisor_ip)
        logging.info('Getting OVS ports: %s for Hypervisor IP: %s', ports, hypervisor_ip)
        return ports

    def list_qos_hypervisor(self, hypervisor_ip):
        qos_raw = json.loads(self.ovsclient.list_qoss(hypervisor_ip))
        hypervisor_qos = []

        for q in qos_raw.get('data'):
            hypervisor_qos.append(q)

        logging.info('Getting final OVS QoS %s for IP %s', hypervisor_qos, hypervisor_ip)
        return hypervisor_qos

    def list_port_desc_hypervisor(self, hypervisor_ip):
        port_desc_raw = self.ovsclient.list_port_desc(hypervisor_ip)
        logging.info('Getting final Port Desc %s for IP %s', port_desc_raw, hypervisor_ip)
        return port_desc_raw

    def list_queues_hypervisor(self, hypervisor_ip):
        queues = []
        queues_raw = json.loads(self.ovsclient.list_queues(hypervisor_ip))
        logging.info('Queues for comparison for %s : %s ... ', hypervisor_ip, queues_raw)

        for q in queues_raw.get('data'):
            queues.append(q)
        logging.info('Queues from queue list: %s', queues)
        return queues

    def get_port_qos(self, hypervisor_ip, ovs_port):
        ports_raw = json.loads(self.list_ports_hypervisor(hypervisor_ip))
        logging.info('Ports for %s: %s', hypervisor_ip, ports_raw)
        qos_id = []
        for port in ports_raw.get('data'):
            isvm = 0
            for port_key in port:
                if type(port_key) == unicode and port_key == ovs_port:
                    isvm = 1
                if type(port_key) == list and isvm:
                    isvm = 0
                    for pv in port_key:
                        if pv != 'uuid':
                            qos_id = pv
        logging.info('QoS ID for port: %s is: %s', ovs_port, qos_id)
        return qos_id

    def set_qos_vm(self, server_interface, interfaces, vm_id):
        qos_status = {}
        queues = {}
        queue_string = "queues="
        found_match = 0
        queue_cfgs = ""
        server_ips = self.cloud.get_server_ips()
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)

        for ks, vs in server_ips.items():
            if ks == vm_id:
                for ip in vs:
                    if ip == server_interface.get('ip'):
                        found_match = 1
                        neutron_port = self.cloud.get_neutron_port(ip)
                        ovs_port_id = self.cloud.get_port_id(interfaces, neutron_port)[0]
                        logging.info('OVS port ID for Server %s: %s', ks, ovs_port_id)
                        qos_tmp = server_interface.get('qos')
                        for rate in qos_tmp.get('queues'):
                            internal_rate = rate.get('rates')
                            logging.info('Server %s gets Min-rate %s Max-rate %s', ks, rate.get('min-rate'), rate.get('max-rate'))
                            #queues[index] = self.ovsclient.create_queue(hypervisor_ip, int(qos_rate.get('min-rate')), int(qos_rate.get('max-rate')))
                            queue_cfgs += "-- --id=@q" + rate.get('id') + " create queue other-config:min-rate=" + internal_rate.get('min-rate') +\
                                          " other-config:max-rate=" + internal_rate.get('max-rate') + " "
                            queues[rate.get('id')] = "q"+ rate.get('id')
                            logging.info('Queue ID for Server %s: %s', ks, queue_cfgs)
                        for idx, qid in queues.items():
                            queue_string += "%s=@" %idx + "%s," % qid

                        logging.info('Queue String for server %s: %s%s', ks, queue_string, queue_cfgs)
                        return_ids = self.ovsclient.create_qos(hypervisor_ip, queue_string, queue_cfgs)
                        logging.info('QoS ID %s', return_ids.split('\n'))
                        qos_id = return_ids.split('\n')
                        qos_status = self.ovsclient.set_port(hypervisor_ip, ovs_port_id, 'qos', qos_id[0])

                        qos_tmp['qos_uuid'] = qos_id[0]

                        # logging.debug("Current qoses_ids %s",qos_id)

                        for num_id,ids in enumerate(qos_id):
                            for rate in qos_tmp.get('queues'):
                                if int(rate.get('id')) == num_id:
                                    rate['queue_uuid'] = ids

                        server_interface['qos'] = qos_tmp


                        if not qos_status:
                            qos_status = server_interface
                        logging.info('QoS status for Server %s: %s', ks, qos_status)
        if not found_match:
            return qos_status
        else:
            return qos_status

    def dell_qos(self,qos_id):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        resp = {}
        resp['queues'] = []
        queues = self.get_queues(qos_id)
        logging.debug("Queues found: %s",queues)

        logging.info('OVS destroy all queue on : %s and deleteting qos %s',hypervisor_ip,qos_id)

        if queues:
            for current_queue in queues:
                queue_del = {}
                queue_del['status'] = self.del_queue(current_queue.get('number'),current_queue.get('id'),qos_id)
                queue_del['number'] = current_queue.get('number')

                resp['queues'].append(queue_del)

        qos_del = self.ovsclient.del_qos(hypervisor_ip,qos_id)

        resp['qos_id'] = qos_id
        if qos_del == None:
            resp['status'] = "success"
        else:
            resp['status'] = qos_del

        return resp


    def get_queues(self,qos_id):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        queue_json = self.ovsclient.list_qosandqueue(hypervisor_ip,qos_id)
        queues = json.loads(queue_json)
        logging.debug("Queues %s", queues)
        val = []

        for item in queues.get('data'):
            logging.debug("Item %s", item)
            for inner in item:
                logging.debug("Inner %s", inner)
                if type(inner) == list:
                    for li in inner:
                        logging.debug("Li %s",li)
                        if type(li) == list:
                            if not li:
                                return val

                            if len(li) > 1:
                                for lu in li:
                                    queue = {}
                                    logging.debug("LU[0] %s",lu[0])
                                    queue['number'] = lu[0]
                                    logging.debug("LU[1] %s",lu[1][1])
                                    queue['id'] = lu[1][1]
                                    val.append(queue)
                            else:
                                single_queue = {}
                                logging.debug("Li[0] %s",li[0])
                                single_queue['number'] = li[0][0]
                                logging.debug("Li[0][1][1] %s",li[0][1][1])
                                single_queue['id'] = li[0][1][1]
                                val.append(single_queue)
        return val


    def del_queue(self,queue_number,queue_id,qos_id):
        logging.info('OVS destroy queue number %s with id %s',queue_number,queue_id)
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        ret = {}
        val = self.ovsclient.del_queue(hypervisor_ip,qos_id,queue_id,queue_number)

        # queues = self.get_queues(qos_id)
        # ret['qos_uuid'] = qos_id
        # ret['queues'] = []
        #
        # for queue in queues:
        #     ret_queue = {}
        #     ret_queue['queue_uuid'] = queue.get('id')
        #     ret_queue['id'] = queue.get('number')
        #     ret_queue['rates'] = self.get_queue_rates(queue.get('id'))
        #     ret['queues'].append(ret_queue)

        ret['queue_uuid'] = queue_id
        ret['queue_id'] = queue_number
        ret['qos_uuid'] = qos_id

        if val is 0:
            ret['status'] = 'success'
        else:
            ret['status'] = val

        return ret

    def set_flow_vm(self, flow_config):
        flow_status = []


        logging.info('Flow config %s',flow_config)

        for qv in flow_config:

            current_flow = {}
            logging.info('Setting flow on Queue: ip %s', qv.get("src_ipv4"))
            hypervisor_ip = self.cloud.get_hypervisor_ip(qv.get("dest_hyp"))
            current_flow = self.ovsclient.add_flow_to_queue(hypervisor_ip, qv.get('src_ipv4'),qv.get("dest_ipv4"), qv.get("protocol"),
                                                           qv.get('priority'), qv.get('ovs_port_number'), qv.get("queue_number"))

            flow_status.append(current_flow)

        return flow_status

    def remove_flow(self,ip,protocol):

        result = {}
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        logging.debug("REMOVE FLOW hypervisor ip %s",hypervisor_ip)
        self.ovsclient.remove_flow_dest(hypervisor_ip,ip,protocol)

        current_flows = self.ovsclient.dump_flows()
        logging.debug("REMOVE FLOW current flows %s", current_flows)
        if ("nw_dst=" + ip in current_flows) == True:
            result = "failed"
        else:
            self.ovsclient.remove_flow_src(hypervisor_ip,ip,protocol)
            current_flows = self.ovsclient.dump_flows()
            if ("nw_src=" + ip in current_flows) == True:
                result = "failed"
            else:
                result = "success"

        return result

    def create_queue(self,min_rate,max_rate):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)
        queue_id = self.ovsclient.create_queue(hypervisor_ip,min_rate,max_rate)
        return queue_id

    def link_queue_to_qos(self,qos_id,queue_id,queue_number):
        hypervisor_ip = self.cloud.get_hypervisor_ip(self.hypervisor)

        ret = self.ovsclient.set_qos_ovs(hypervisor_ip,qos_id,queue_id,queue_number)
        return ret
示例#5
0
class Host(object):
    def __init__(self, hypervisor):
        self.hypervisor = hypervisor
        self.ovsclient = OVSClient()
        self.cloud = Cloud()

    def list_interfaces_hypervisor(self, hypervisors):
        interfaces = {}
        for k, v in hypervisors.items():
            if k == self.hypervisor:
                for k_inner, v_inner in v.items():
                    if k_inner == 'ip':
                        ip = v_inner
                        interfaces = self.ovsclient.list_interfaces(ip)
        logging.info('Getting OVS interfaces %s :', interfaces)
        return interfaces

    def list_ports_hypervisor(self, hypervisor_ip):
        ports = self.ovsclient.list_ports(hypervisor_ip)
        logging.info('Getting OVS ports: %s for Hypervisor IP: %s', ports,
                     hypervisor_ip)
        return ports

    def list_qos_hypervisor(self, hypervisor_ip):
        qos_raw = json.loads(self.ovsclient.list_qos(hypervisor_ip))
        hypervisor_qos = []

        for q in qos_raw.get('data'):
            hypervisor_qos.append(q)

        logging.info('Getting final OVS QoS %s for IP %s', hypervisor_qos,
                     hypervisor_ip)
        return hypervisor_qos

    def list_queues_hypervisor(self, hypervisor_ip):
        queues = []
        queues_raw = json.loads(self.ovsclient.list_queue(hypervisor_ip))
        logging.info('Queues for comparison for %s : %s ... ', hypervisor_ip,
                     queues_raw)

        for q in queues_raw.get('data'):
            queues.append(q)
        logging.info('Queues from queue list: %s', queues)
        return queues

    def get_port_qos(self, hypervisor_ip, ovs_port):
        ports_raw = json.loads(self.list_ports_hypervisor(hypervisor_ip))
        logging.info('Ports for %s: %s', hypervisor_ip, ports_raw)
        qos_id = '0'
        for port in ports_raw.get('data'):
            isvm = 0
            for port_key in port:
                if type(port_key) == unicode and port_key == ovs_port:
                    isvm = 1
                if type(port_key) == list and isvm:
                    isvm = 0
                    for pv in port_key:
                        if pv != 'uuid':
                            qos_id = pv
                            logging.info('QoS ID for port: %s is: %s',
                                         ovs_port, qos_id)
        return qos_id

    def set_qos_vm(self, hypervisor_ip, interfaces, vm_id, qos_rates):
        qos_status = {}
        found_match = 0
        server_ips = self.cloud.get_server_ips()

        for ks, vs in server_ips.items():
            if ks == vm_id:
                found_match = 1
                neutron_port = self.cloud.get_neutron_port(vs[0])
                ovs_port_id = self.cloud.get_port_id(interfaces,
                                                     neutron_port)[0]
                logging.info('OVS port ID for Server %s: %s', ks, ovs_port_id)
                logging.info('Server %s gets Min-rate %s Max-rate %s', ks,
                             qos_rates.get('min-rate'),
                             qos_rates.get('max-rate'))
                queue_id = self.ovsclient.create_queue(
                    hypervisor_ip, int(qos_rates.get('min-rate')),
                    int(qos_rates.get('max-rate')))
                logging.info('Queue ID for Server %s: %s', ks, queue_id)
                qos_id = self.ovsclient.create_qos(hypervisor_ip, queue_id)
                qos_status = self.ovsclient.set_port(hypervisor_ip,
                                                     ovs_port_id, 'qos',
                                                     qos_id)
                if not qos_status:
                    qos_status = qos_rates
                logging.info('QoS status for Server %s: %s', ks, qos_status)
        if not found_match:
            return 0
        else:
            return qos_status
class Host(object):
    def __init__(self, hypervisor):
        self.hypervisor = hypervisor
        self.ovsclient = OVSClient()
        self.cloud = Cloud()

    def list_interfaces_hypervisor(self, hypervisors):
        interfaces = {}
        for k, v in hypervisors.items():
            if k == self.hypervisor:
                for k_inner, v_inner in v.items():
                    if k_inner == 'ip':
                        ip = v_inner
                        interfaces = self.ovsclient.list_interfaces(ip)
        logging.info('Getting OVS interfaces %s :', interfaces)
        return interfaces

    def list_ports_hypervisor(self, hypervisor_ip):
        ports = self.ovsclient.list_ports(hypervisor_ip)
        logging.info('Getting OVS ports: %s for Hypervisor IP: %s', ports, hypervisor_ip)
        return ports

    def list_qos_hypervisor(self, hypervisor_ip):
        qos_raw = json.loads(self.ovsclient.list_qos(hypervisor_ip))
        hypervisor_qos = []

        for q in qos_raw.get('data'):
            hypervisor_qos.append(q)

        logging.info('Getting final OVS QoS %s for IP %s', hypervisor_qos, hypervisor_ip)
        return hypervisor_qos

    def list_queues_hypervisor(self, hypervisor_ip):
        queues = []
        queues_raw = json.loads(self.ovsclient.list_queue(hypervisor_ip))
        logging.info('Queues for comparison for %s : %s ... ', hypervisor_ip, queues_raw)

        for q in queues_raw.get('data'):
            queues.append(q)
        logging.info('Queues from queue list: %s', queues)
        return queues

    def get_port_qos(self, hypervisor_ip, ovs_port):
        ports_raw = json.loads(self.list_ports_hypervisor(hypervisor_ip))
        logging.info('Ports for %s: %s', hypervisor_ip, ports_raw)
        qos_id = '0'
        for port in ports_raw.get('data'):
            isvm = 0
            for port_key in port:
                if type(port_key) == unicode and port_key == ovs_port:
                    isvm = 1
                if type(port_key) == list and isvm:
                    isvm = 0
                    for pv in port_key:
                        if pv != 'uuid':
                            qos_id = pv
                            logging.info('QoS ID for port: %s is: %s', ovs_port, qos_id)
        return qos_id

    def set_qos_vm(self, hypervisor_ip, interfaces, vm_id, qos_rates):
        qos_status = {}
        found_match = 0
        server_ips = self.cloud.get_server_ips()

        for ks, vs in server_ips.items():
            if ks == vm_id:
                found_match = 1
                neutron_port = self.cloud.get_neutron_port(vs[0])
                ovs_port_id = self.cloud.get_port_id(interfaces, neutron_port)[0]
                logging.info('OVS port ID for Server %s: %s', ks, ovs_port_id)
                logging.info('Server %s gets Min-rate %s Max-rate %s', ks, qos_rates.get('min-rate'), qos_rates.get('max-rate'))
                queue_id = self.ovsclient.create_queue(hypervisor_ip, int(qos_rates.get('min-rate')), int(qos_rates.get('max-rate')))
                logging.info('Queue ID for Server %s: %s', ks, queue_id)
                qos_id = self.ovsclient.create_qos(hypervisor_ip, queue_id)
                qos_status = self.ovsclient.set_port(hypervisor_ip, ovs_port_id, 'qos', qos_id)
                if not qos_status:
                    qos_status = qos_rates
                logging.info('QoS status for Server %s: %s', ks, qos_status)
        if not found_match:
            return 0
        else:
            return qos_status