def QueryPacketStat(self, request, context):
        statistics_items = []

        for mac_addr in request.mac_addrs:
            statistics = NicManager_pb2.PacketStat()
            statistics_items.append(statistics)

            nic_name = NicManager.MacAddr2NicName(mac_addr)
            if nic_name == "":
                statistics.errno = ErrNo_pb2.SYS_FAIL
                continue

            class_net_path = os.path.join(util.SYS_CLASS_NET, nic_name)
            if not os.path.exists(class_net_path):
                statistics.errno = ErrNo_pb2.SYS_FAIL
                continue

            try:
                nic_object = NicManager.NicHandle(nic_name)
                statistics.tx_bytes = nic_object.get_tx_bytes()
                statistics.tx_packets = nic_object.get_tx_packets()
                statistics.tx_dropped = nic_object.get_tx_dropped()
                statistics.tx_errors = nic_object.get_tx_errors()
                statistics.rx_bytes = nic_object.get_rx_bytes()
                statistics.rx_packets = nic_object.get_rx_packets()
                statistics.rx_dropped = nic_object.get_rx_dropped()
                statistics.rx_errors = nic_object.get_rx_errors()

                statistics.errno = ErrNo_pb2.SYS_OK
            except net_agentd_exception.NetAgentException:
                statistics.errno = ErrNo_pb2.SYS_FAIL
                logging.error("%s QueryPacketStat failed" % nic_name)

        return NicManager_pb2.PacketStatQueryReply(
                                   packet_stats=statistics_items)
    def QueryNicInfo(self, request, context):
        nic_infos = []

        for nic_name in request.nic_names:
            class_net_path = os.path.join(util.SYS_CLASS_NET, nic_name)
            if os.path.exists(class_net_path):
                try:
                    nic_object = NicManager.NicHandle(nic_name)
                    nic_info = NicManager_pb2.NicInfo()

                    nic_info.nic_name = nic_name
                    nic_info.description = nic_object.get_description()
                    nic_info.mac_address = nic_object.get_mac_address()
                    nic_info.speed = nic_object.get_speed()
                    nic_info.operstate = nic_object.get_operstate()
                    nic_info.mtu = nic_object.get_mtu()
                    nic_info.driver_name = nic_object.get_driver_name()
                    nic_info.numa = nic_object.get_numa()
                    nic_info.nic_address = nic_object.get_nic_address()
                    nic_info.carrier = nic_object.get_carrier()
                    nic_info.duplex = nic_object.get_duplex()

                    nic_info.errno = ErrNo_pb2.SYS_OK
                except net_agentd_exception.NetAgentException:
                    nic_info.errno = ErrNo_pb2.SYS_FAIL
                    logging.error("%s QueryNicList failed" % nic_name)

                nic_infos.append(nic_info)

            else:
                logging.error("%s doesnt exist" % nic_name)

        return NicManager_pb2.NicInfoQueryReply(nic_infos=nic_infos)
 def test_ModifyNic(self):
     query_nic_info_response = self.nic_stub.QueryNicInfo(
         NicManager_pb2.NicInfoQueryRequest(nic_names=[self.nic]))
     driver_name = query_nic_info_response.nic_infos[0].driver_name
     mtu = "1899"
     response = self.nic_stub.ModifyNic(
         NicManager_pb2.NicModifyRequest(nic_name=self.nic,
                                         driver_name=driver_name,
                                         mtu=mtu))
     assert response.errno == ErrNo_pb2.SYS_OK
    def test_QueryPacketStat(self):
        query_nic_info_response = self.nic_stub.QueryNicInfo(
            NicManager_pb2.NicInfoQueryRequest(nic_names=[self.nic]))
        mac_addr = query_nic_info_response.nic_infos[0].mac_address
        response = self.nic_stub.QueryPacketStat(
            NicManager_pb2.PacketStatQueryRequest(mac_addrs=[mac_addr]))
        assert response.packet_stats[0].errno == ErrNo_pb2.SYS_OK

        response = self.nic_stub.QueryPacketStat(
            NicManager_pb2.PacketStatQueryRequest(
                mac_addrs=["Z6:33:db:0f:e0:4c"]))
        assert response.packet_stats[0].errno == ErrNo_pb2.SYS_FAIL
    def test_QueryNicList(self):
        response = self.nic_stub.QueryNicList(
            NicManager_pb2.NicListQueryRequest(
                host_name='you',
                nic_type=NicManager_pb2.NicListQueryRequest.ALL_PHY_NIC))
        assert response.errno == ErrNo_pb2.SYS_OK

        response = self.nic_stub.QueryNicList(
            NicManager_pb2.NicListQueryRequest(
                host_name='you',
                nic_type=NicManager_pb2.NicListQueryRequest.FREE_PHY_NIC))
        assert response.errno == ErrNo_pb2.SYS_OK

        response = self.nic_stub.QueryNicList(
            NicManager_pb2.NicListQueryRequest(
                host_name='you',
                nic_type=NicManager_pb2.NicListQueryRequest.VIRTUAL_NIC))
        assert response.errno == ErrNo_pb2.SYS_OK
    def ModifyNic(self, request, context):
        errno = ErrNo_pb2.SYS_FAIL

        class_net_path = os.path.join(util.SYS_CLASS_NET, request.nic_name)
        if request.nic_name == "":
            errno = ErrNo_pb2.SYS_FAIL
            return NicManager_pb2.NicModifyReply(errno=errno)
        else:
            if not os.path.exists(class_net_path):
                errno = ErrNo_pb2.SYS_FAIL
                return NicManager_pb2.NicModifyReply(errno=errno)

        nic_object = NicManager.NicHandle(request.nic_name)

        if request.driver_name != "":
            try:
                raw_device = nic_object.get_raw_device()
                new_driver_path = nic_object.get_driver_path_by_driver_name(request.driver_name)
                nic_object.detach_driver()
                nic_object.attach_driver(new_driver_path, raw_device)
                errno = ErrNo_pb2.SYS_OK
            except net_agentd_exception.NetAgentException:
                logging.error(traceback.format_exc())
                logging.error('changer driver failed %s' % request.nic_name)
                errno = ErrNo_pb2.SYS_FAIL
            time.sleep(1)

        if request.mtu != "":
            try:
                nic_object.set_mtu(request.mtu)
                new_mtu = nic_object.get_mtu()
                if new_mtu == request.mtu:
                    errno = ErrNo_pb2.SYS_OK
                else:
                    errno = ErrNo_pb2.SYS_FAIL

            except:
                logging.error(traceback.format_exc())
                logging.error('set mtu failed %s' % request.nic_name)
                errno = ErrNo_pb2.SYS_FAIL

        return NicManager_pb2.NicModifyReply(errno=errno)
    def StopNic(self, request, context):
        errno = ErrNo_pb2.SYS_FAIL

        try:
            nic_object = NicManager.NicHandle(request.nic_name)
            nic_object.stop_nic()
            errno = ErrNo_pb2.SYS_OK
        except:
            logging.error(traceback.format_exc())
            logging.error("StopNic failed")

        return NicManager_pb2.NicStopReply(errno=errno)
    def test_AttachQos_DetachQos(self):
        response = self.nic_stub.QueryNicInfo(
            NicManager_pb2.NicInfoQueryRequest(nic_names=[self.qos_nic]))
        assert response.nic_infos[0].errno == ErrNo_pb2.SYS_OK
        mac_addr = response.nic_infos[0].mac_address

        response = self.qos_stub.AttachQos(
            QosManager_pb2.QosAttachRequest(vswitch_name=self.switch_name,
                                            tap_name=self.qos_nic,
                                            qos_name=self.qos_name))

        assert response.errno == ErrNo_pb2.SYS_OK

        response = self.qos_stub.DetachQos(
            QosManager_pb2.QosDetachRequest(vswitch_name=self.switch_name,
                                            tap_name=self.qos_nic,
                                            qos_name=self.qos_name))

        assert response.errno == ErrNo_pb2.SYS_OK
    def QueryNicList(self, request, context):
        errno = ErrNo_pb2.SYS_FAIL
        nic_names = []
        type = [NicManager_pb2.NicListQueryRequest.ALL_PHY_NIC,
                NicManager_pb2.NicListQueryRequest.FREE_PHY_NIC,
                NicManager_pb2.NicListQueryRequest.VIRTUAL_NIC]

        while True:
            if request.nic_type not in type:
                break

            try:
                nic_names = NicManager.get_nic_list(request.nic_type)
                errno = ErrNo_pb2.SYS_OK
            except net_agentd_exception.NetAgentException:
                logging.error(traceback.format_exc())
                logging.error("QueryNicList failed")

            break

        return NicManager_pb2.NicListQueryReply(nic_names=nic_names, errno=errno)
    def test_AttachAcl_DetachAcl(self):
        response = self.nic_stub.QueryNicInfo(
            NicManager_pb2.NicInfoQueryRequest(nic_names=[self.acl_nic]))
        assert response.nic_infos[0].errno == ErrNo_pb2.SYS_OK
        mac_addr = response.nic_infos[0].mac_address

        response = self.acl_stub.AttachAcl(
            AclManager_pb2.AclAttachRequest(vswitch_name=self.switch_name,
                                            tap_name=self.acl_nic,
                                            mac_addr=mac_addr,
                                            filter_name=self.filter_name))

        assert response.errno == ErrNo_pb2.SYS_OK

        response = self.acl_stub.DetachAcl(
            AclManager_pb2.AclDetachRequest(vswitch_name=self.switch_name,
                                            tap_name=self.acl_nic,
                                            mac_addr=mac_addr,
                                            filter_name=self.filter_name))

        assert response.errno == ErrNo_pb2.SYS_OK
 def test_StopNic(self):
     response = self.nic_stub.StopNic(
         NicManager_pb2.NicModifyRequest(nic_name=self.nic))
     assert response.errno == ErrNo_pb2.SYS_OK
 def test_QueryNicInfo(self):
     response = self.nic_stub.QueryNicInfo(
         NicManager_pb2.NicInfoQueryRequest(nic_names=[self.nic]))
     assert response.nic_infos[0].errno == ErrNo_pb2.SYS_OK